Flow-control structures: Difference between revisions

Content added Content deleted
(adding MUMPS information)
m (→‎{{header|PureBasic}}: Some more meat on the bone)
Line 790: Line 790:
Remark: Pop11 does not perform "tail call optimization", one has to explicitly use chain.
Remark: Pop11 does not perform "tail call optimization", one has to explicitly use chain.
=={{header|PureBasic}}==
=={{header|PureBasic}}==
===goto===
===Goto===
Transfers control to the label referenced. It is not a safe way to exit loops.
Transfers control to the label referenced. It is not a safe way to exit loops.
<lang PureBasic>If OpenConsole()
<lang PureBasic>If OpenConsole()
Line 804: Line 804:
CloseConsole()
CloseConsole()
EndIf </lang>
EndIf </lang>
===Gosub & Return===
Gosub stands for 'Go to sub routine'. A label must be specified after Gosub where the program execution continues and will do so until encountering a Return. When a return is reached, the program execution is then transferred immediately below the Gosub.
Gosub is useful when building fast structured code with very low overhead.
<lang PureBasic>X=1: Y=2
Gosub Calc
;X will now equal 7
End

Calc:
X+3*Y
Return ; Returns to the point in the code where the Gosub jumped from</lang>
===FakeReturn===
<lang PureBasic>Gosub MySub

Lable2:
; The program will jump here, then 'end'
End

MySub:
If #PI>3
FakeReturn ; This will simulate the function of a normal "Return".
Goto Lable2
EndIf
Return</lang>
If the command Goto is used within the body of a sub routine, FakeReturn must be used to correct the stack or the program will crash.
===OnErrorGoto===
This will transferee the program execution to the defined label if an error accrue.
<lang PureBasic>OnErrorGoto(?MyExitHandler)

X=1: Y=0
z= X/Y
; = a illegal division with zero
Debug "This line should never be reached"
End

MyExitHandler:
MessageRequester("Error", ErrorMessage())
End</lang>
===OnErrorCall===
Similar to OnErrorGoto() but procedural instead.
<lang PureBasic>Procedure MyErrorHandler()
;All open files etc can be closed here
MessageRequester("Error", ErrorMessage())
End
EndProcedure

OnErrorCall(MyErrorHandler())
X=1: Y=0
Z= X/Y
;This line should never be reached</lang>


=={{header|Python}}==
=={{header|Python}}==