Flow-control structures: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 20: Line 20:
===Loops===
===Loops===
11l supports ''L.break'' and ''L.continue'' to exit from a loop early or short circuit the rest of a loop's body and "continue" on to the next loop iteration.
11l supports ''L.break'' and ''L.continue'' to exit from a loop early or short circuit the rest of a loop's body and "continue" on to the next loop iteration.
<lang 11l>V n = 10
<syntaxhighlight lang="11l">V n = 10
Int result
Int result


Line 31: Line 31:
L.was_no_break
L.was_no_break
result = -1
result = -1
print(‘No odd factors found’)</lang>
print(‘No odd factors found’)</syntaxhighlight>
In addition, as shown in the foregoing example, 11l loops support an ''L.was_no_break'' suite which can be used to handle cases when the loop was intended to search for something, where the code would break out of the loop upon finding its target.
In addition, as shown in the foregoing example, 11l loops support an ''L.was_no_break'' suite which can be used to handle cases when the loop was intended to search for something, where the code would break out of the loop upon finding its target.


Line 38: Line 38:
===Unconditional Branch (B)===
===Unconditional Branch (B)===
To perform a 'goto'.
To perform a 'goto'.
<lang 360asm>
<syntaxhighlight lang="360asm">
B TESTPX goto label TESTPX
B TESTPX goto label TESTPX
BR 14 goto to the address found in register 14
BR 14 goto to the address found in register 14
</syntaxhighlight>
</lang>
===Branch and Link (BAL)===
===Branch and Link (BAL)===
To perform a 'call' to a subroutine. The first register at execution time is the next sequential address to allow a 'return'.
To perform a 'call' to a subroutine. The first register at execution time is the next sequential address to allow a 'return'.
<lang 360asm>
<syntaxhighlight lang="360asm">
LA 15,SINUSX load in reg15 address of function SINUSX
LA 15,SINUSX load in reg15 address of function SINUSX
BALR 14,15 call the subroutine SINUX and place address RETPNT in reg14
BALR 14,15 call the subroutine SINUX and place address RETPNT in reg14
Line 52: Line 52:
...
...
BR 14 return to caller
BR 14 return to caller
</syntaxhighlight>
</lang>


===Conditional Branch (BC)===
===Conditional Branch (BC)===
Fistly a compare instruction set the condition code (cc), secondly a conditional branch is performed.
Fistly a compare instruction set the condition code (cc), secondly a conditional branch is performed.
<lang 360asm>
<syntaxhighlight lang="360asm">
L 4,A Load A in register 4
L 4,A Load A in register 4
C 4,B Compare A with B
C 4,B Compare A with B
Line 65: Line 65:
BNL TESTGE Branch on Not Low if A>=B then goto TESTGE
BNL TESTGE Branch on Not Low if A>=B then goto TESTGE
BNE TESTNE Branch on Not Equal if A<>B then goto TESTNE
BNE TESTNE Branch on Not Equal if A<>B then goto TESTNE
</syntaxhighlight>
</lang>
===Branch on Count (BCT)===
===Branch on Count (BCT)===
To perform unconditional loops.
To perform unconditional loops.
<lang 360asm>
<syntaxhighlight lang="360asm">
LA 3,8 r3 loop counter
LA 3,8 r3 loop counter
LOOP EQU *
LOOP EQU *
... loop 8 times (r3=8,7,...,2,1)
... loop 8 times (r3=8,7,...,2,1)
BCT 3,LOOP r3=r3-1 ; if r3<>0 then loop
BCT 3,LOOP r3=r3-1 ; if r3<>0 then loop
</syntaxhighlight>
</lang>
===Branch on Index (BX.)===
===Branch on Index (BX.)===
BXLE to perform loops in old Fortran style with 3 registers.
BXLE to perform loops in old Fortran style with 3 registers.
<lang 360asm>
<syntaxhighlight lang="360asm">
* do i=1 to 8 by 2
* do i=1 to 8 by 2
L 3,1 r3 index and start value 1
L 3,1 r3 index and start value 1
Line 84: Line 84:
... loop 4 times (r3=1,3,5,7)
... loop 4 times (r3=1,3,5,7)
BXLE 3,4,LOOPI r3=r3+r4; if r3<=r5 then loop
BXLE 3,4,LOOPI r3=r3+r4; if r3<=r5 then loop
</syntaxhighlight>
</lang>
BXH to perform backward loops with 3 registers.
BXH to perform backward loops with 3 registers.
<lang 360asm>
<syntaxhighlight lang="360asm">
* do i=8 to 1 by -2
* do i=8 to 1 by -2
L 3,1 r3 index and start value 8
L 3,1 r3 index and start value 8
Line 94: Line 94:
... loop 4 times (r3=8,6,4,2)
... loop 4 times (r3=8,6,4,2)
BXH 3,4,LOOPI r3=r3+r4; if r3>r5 then loop
BXH 3,4,LOOPI r3=r3+r4; if r3>r5 then loop
</syntaxhighlight>
</lang>


=={{header|6502 Assembly}}==
=={{header|6502 Assembly}}==
Line 100: Line 100:
===JMP===
===JMP===
The jump instruction immediately jumps to any address:
The jump instruction immediately jumps to any address:
<lang 6502asm> JMP $8000 ;immediately JuMP to $8000 and begin executing
<syntaxhighlight lang="6502asm"> JMP $8000 ;immediately JuMP to $8000 and begin executing
;instructions there.</lang>
;instructions there.</syntaxhighlight>
The indirect jump instruction immediately jumps to the address contained in the address:
The indirect jump instruction immediately jumps to the address contained in the address:
<lang 6502asm> JMP ($8000) ;immediately JuMP to the address in memory locations
<syntaxhighlight lang="6502asm"> JMP ($8000) ;immediately JuMP to the address in memory locations
;$8000 and $8001</lang>
;$8000 and $8001</syntaxhighlight>


===JSR===
===JSR===
The jump to subroutine instruction pushes the address of the next instruction minus one onto the stack and jumps to any address:
The jump to subroutine instruction pushes the address of the next instruction minus one onto the stack and jumps to any address:
<lang 6502asm> JSR $8000 ;Jump to SubRoutine</lang>
<syntaxhighlight lang="6502asm"> JSR $8000 ;Jump to SubRoutine</syntaxhighlight>
A return from subroutine instruction pops the return address off the stack, adds one, and jumps to that location:
A return from subroutine instruction pops the return address off the stack, adds one, and jumps to that location:
<lang 6502asm> RTS ;ReTurn from Subroutine</lang>
<syntaxhighlight lang="6502asm"> RTS ;ReTurn from Subroutine</syntaxhighlight>


===NMI===
===NMI===
Line 131: Line 131:


===goto===
===goto===
<lang ada><<Top>>
<syntaxhighlight lang="ada"><<Top>>
Put_Line("Hello, World");
Put_Line("Hello, World");
goto Top;</lang>
goto Top;</syntaxhighlight>


===exit===
===exit===
Exit is used to break out of loops. Exit can be used with a label to break out of an inner loop to an outer loop and its enclosing outer loop
Exit is used to break out of loops. Exit can be used with a label to break out of an inner loop to an outer loop and its enclosing outer loop
<lang ada>Outer:
<syntaxhighlight lang="ada">Outer:
loop
loop
-- do something
-- do something
Line 144: Line 144:
exit Outer; -- exits both the inner and outer loops
exit Outer; -- exits both the inner and outer loops
end loop;
end loop;
end loop;</lang>
end loop;</syntaxhighlight>
===asynchronous transfer of control===
===asynchronous transfer of control===
A sequence of operation can be aborted with an asynchronous transfer of control to an alternative:
A sequence of operation can be aborted with an asynchronous transfer of control to an alternative:
<lang ada>select
<syntaxhighlight lang="ada">select
delay 10.0;
delay 10.0;
Put_Line ("Cannot finish this in 10s");
Put_Line ("Cannot finish this in 10s");
Line 153: Line 153:
-- do some lengthy calculation
-- do some lengthy calculation
...
...
end select;</lang>
end select;</syntaxhighlight>
The alternative can be a delay statement or else an entry point call followed by a sequence of operations. The statement blocks at the delay or entry call and executes the sequence of the operation introduced by '''then abort'''. If blocking is lifted before completion of the sequence, the sequence is aborted and the control is transferred there.
The alternative can be a delay statement or else an entry point call followed by a sequence of operations. The statement blocks at the delay or entry call and executes the sequence of the operation introduced by '''then abort'''. If blocking is lifted before completion of the sequence, the sequence is aborted and the control is transferred there.


Line 164: Line 164:
See also [[Exceptions#ALGOL_68|Exceptions]] to see how '''ALGOL 68''' handles ''transput'' events.
See also [[Exceptions#ALGOL_68|Exceptions]] to see how '''ALGOL 68''' handles ''transput'' events.
===One common use of a label in '''ALGOL 68''' is to break out of nested loops.===
===One common use of a label in '''ALGOL 68''' is to break out of nested loops.===
<lang algol68>(
<syntaxhighlight lang="algol68">(
FOR j TO 1000 DO
FOR j TO 1000 DO
FOR i TO j-1 DO
FOR i TO j-1 DO
Line 175: Line 175:
OD;
OD;
done: EMPTY
done: EMPTY
);</lang>
);</syntaxhighlight>
===Multi way jump using labels and '''EXIT''' to return result ===
===Multi way jump using labels and '''EXIT''' to return result ===
<lang algol68>STRING medal = (
<syntaxhighlight lang="algol68">STRING medal = (
[]PROC VOID award = (gold,silver,bronze);
[]PROC VOID award = (gold,silver,bronze);


Line 188: Line 188:
);
);


print(("Medal awarded: ",medal, new line));</lang>
print(("Medal awarded: ",medal, new line));</syntaxhighlight>
===Another use is to implement finite state machines ===
===Another use is to implement finite state machines ===
<lang algol68>STRING final state = (
<syntaxhighlight lang="algol68">STRING final state = (
INT condition;
INT condition;
PROC do something = VOID: condition := 1 + ENTIER (3 * random);
PROC do something = VOID: condition := 1 + ENTIER (3 * random);
Line 210: Line 210:
"State N"
"State N"
);
);
print(("Final state: ",final state, new line));</lang>
print(("Final state: ",final state, new line));</syntaxhighlight>
===ALGOL 68G implements a Refinement Preprocessor to aid with top down code development ===
===ALGOL 68G implements a Refinement Preprocessor to aid with top down code development ===
<lang algol68># example from: http://www.xs4all.nl/~jmvdveer/algol.html - GPL #
<syntaxhighlight lang="algol68"># example from: http://www.xs4all.nl/~jmvdveer/algol.html - GPL #
determine first generation;
determine first generation;
WHILE can represent next generation
WHILE can represent next generation
Line 233: Line 233:
printf (($lz","3z","3z","2z-d$, current,
printf (($lz","3z","3z","2z-d$, current,
$xz","3z","3z","2z-d$, previous,
$xz","3z","3z","2z-d$, previous,
$xd.n(real width - 1)d$, current / previous)).</lang>
$xd.n(real width - 1)d$, current / previous)).</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 248: Line 248:
=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
As well as structured flow-control structures (loops, if-then-else, etc.) Algol W has a goto statement. A goto can lead out of the current procedure, which can be used for error handling. Goto can be written as "goto" or "go to".
As well as structured flow-control structures (loops, if-then-else, etc.) Algol W has a goto statement. A goto can lead out of the current procedure, which can be used for error handling. Goto can be written as "goto" or "go to".
<lang algolw>begin
<syntaxhighlight lang="algolw">begin
integer i;
integer i;
integer procedure getNumber ;
integer procedure getNumber ;
Line 265: Line 265:
writeon( "negative" );
writeon( "negative" );
endProgram:
endProgram:
end.</lang>
end.</syntaxhighlight>


=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
<lang ARM Assembly>SWI n ;software system call
<syntaxhighlight lang="arm assembly">SWI n ;software system call
B label ;Branch. Just "B" is a branch always, but any condition code can be added for a conditional branch.
B label ;Branch. Just "B" is a branch always, but any condition code can be added for a conditional branch.
;In fact, almost any instruction can be made conditional to avoid branching.
;In fact, almost any instruction can be made conditional to avoid branching.
Line 279: Line 279:


addeq R0,R0,#1 ;almost any instruction can be made conditional. If the flag state doesn't match the condition code, the instruction
addeq R0,R0,#1 ;almost any instruction can be made conditional. If the flag state doesn't match the condition code, the instruction
;has no effect on registers or memory.</lang>
;has no effect on registers or memory.</syntaxhighlight>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>MsgBox, calling Label1
<syntaxhighlight lang="autohotkey">MsgBox, calling Label1
Gosub, Label1
Gosub, Label1
MsgBox, Label1 subroutine finished
MsgBox, Label1 subroutine finished
Line 295: Line 295:
Label2:
Label2:
MsgBox, Label2 will not return to calling routine
MsgBox, Label2 will not return to calling routine
Return</lang>
Return</syntaxhighlight>


=={{header|AWK}}==
=={{header|AWK}}==
Line 301: Line 301:
The [awk] programming language is data driven. However, Awk has ''break'' and ''continue'' for loop control, as in C.
The [awk] programming language is data driven. However, Awk has ''break'' and ''continue'' for loop control, as in C.


<lang awk>$ awk 'BEGIN{for(i=1;;i++){if(i%2)continue; if(i>=10)break; print i}}'
<syntaxhighlight lang="awk">$ awk 'BEGIN{for(i=1;;i++){if(i%2)continue; if(i>=10)break; print i}}'
2
2
4
4
6
6
8</lang>
8</syntaxhighlight>




=={{header|BASIC256}}==
=={{header|BASIC256}}==
<syntaxhighlight lang="basic256">
<lang BASIC256>
gosub subrutina
gosub subrutina


Line 322: Line 322:
return
return
end
end
</syntaxhighlight>
</lang>




Line 328: Line 328:
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
BBC BASIC has '''GOSUB''' and '''GOTO''' but they are deprecated.
BBC BASIC has '''GOSUB''' and '''GOTO''' but they are deprecated.
<lang bbcbasic> GOSUB subroutine
<syntaxhighlight lang="bbcbasic"> GOSUB subroutine
(loop)
(loop)
Line 338: Line 338:
PRINT "In subroutine"
PRINT "In subroutine"
WAIT 100
WAIT 100
RETURN</lang>
RETURN</syntaxhighlight>


=={{header|Bracmat}}==
=={{header|Bracmat}}==
In Bracmat, the thing that comes closest to a GOTO construct is evaluation of a variable that contains some code, ending with an evaluation of the same variable. Due to tail recursion optimization this can run forever. Example:
In Bracmat, the thing that comes closest to a GOTO construct is evaluation of a variable that contains some code, ending with an evaluation of the same variable. Due to tail recursion optimization this can run forever. Example:
<lang bracmat> ( LOOP
<syntaxhighlight lang="bracmat"> ( LOOP
= out$"Hi again!"
= out$"Hi again!"
& !LOOP
& !LOOP
)
)
& out$Hi!
& out$Hi!
& !LOOP</lang>
& !LOOP</syntaxhighlight>
{{out}}
{{out}}
<pre>Hi!
<pre>Hi!
Line 359: Line 359:
===goto===
===goto===
One common use of goto in C is to break out of nested loops.
One common use of goto in C is to break out of nested loops.
<lang c>int main()
<syntaxhighlight lang="c">int main()
{
{
int i,j;
int i,j;
Line 371: Line 371:
out:
out:
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
===return===
===return===
terminates the function and returns control to the caller.
terminates the function and returns control to the caller.
<lang csharp>int GetNumber() {
<syntaxhighlight lang="csharp">int GetNumber() {
return 5;
return 5;
}</lang>
}</syntaxhighlight>
===throw===
===throw===
throws (or rethrows) an exception. Control is transferred to the nearest catch block capable of catching the exception.<br/>
throws (or rethrows) an exception. Control is transferred to the nearest catch block capable of catching the exception.<br/>
A <code>finally</code> block is always executed before control leaves the <code>try</code> block.
A <code>finally</code> block is always executed before control leaves the <code>try</code> block.
<lang csharp>try {
<syntaxhighlight lang="csharp">try {
if (someCondition) {
if (someCondition) {
throw new Exception();
throw new Exception();
Line 391: Line 391:
} finally {
} finally {
cleanUp();
cleanUp();
}</lang>
}</syntaxhighlight>
===yield return and yield break===
===yield return and yield break===
In a generator method, <code>yield return</code> causes the method to return elements one at a time. To make this work, the compiler creates a state machine behind the scenes. <code>yield break</code> terminates the iteration.
In a generator method, <code>yield return</code> causes the method to return elements one at a time. To make this work, the compiler creates a state machine behind the scenes. <code>yield break</code> terminates the iteration.
<lang csharp>public static void Main() {
<syntaxhighlight lang="csharp">public static void Main() {
foreach (int n in Numbers(i => i >= 2) {
foreach (int n in Numbers(i => i >= 2) {
Console.WriteLine("Got " + n);
Console.WriteLine("Got " + n);
Line 407: Line 407:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 416: Line 416:
===await===
===await===
is used to wait for an asynchronous operation (usually a Task) to complete. If the operation is already completed when <code>await</code> is encountered, the method will simply continue to execute. If the operation is not completed yet, the method will be suspended. A continuation will be set up to execute the rest of the method at a later time. Then, control will be returned to the caller.
is used to wait for an asynchronous operation (usually a Task) to complete. If the operation is already completed when <code>await</code> is encountered, the method will simply continue to execute. If the operation is not completed yet, the method will be suspended. A continuation will be set up to execute the rest of the method at a later time. Then, control will be returned to the caller.
<lang csharp>async Task DoStuffAsync() {
<syntaxhighlight lang="csharp">async Task DoStuffAsync() {
DoSomething();
DoSomething();
await someOtherTask;//returns control to caller if someOtherTask is not yet finished.
await someOtherTask;//returns control to caller if someOtherTask is not yet finished.
DoSomethingElse();
DoSomethingElse();
}
}
</syntaxhighlight>
</lang>
===break and continue===
===break and continue===
<code>continue</code> causes the closest enclosing loop to skip the current iteration and start the next iteration immediately.<br/>
<code>continue</code> causes the closest enclosing loop to skip the current iteration and start the next iteration immediately.<br/>
Line 428: Line 428:
<code>goto Label;</code> will cause control to jump to the statement with the corresponding label. This can be a <code>case</code> label inside a <code>switch</code>.<br/>
<code>goto Label;</code> will cause control to jump to the statement with the corresponding label. This can be a <code>case</code> label inside a <code>switch</code>.<br/>
Because the label must be in scope, <code>goto</code> cannot jump inside of a loop.
Because the label must be in scope, <code>goto</code> cannot jump inside of a loop.
<lang csharp>while (conditionA) {
<syntaxhighlight lang="csharp">while (conditionA) {
for (int i = 0; i < 10; i++) {
for (int i = 0; i < 10; i++) {
if (conditionB) goto NextSection;
if (conditionB) goto NextSection;
Line 434: Line 434:
}
}
}
}
NextSection: DoOtherStuff();</lang>
NextSection: DoOtherStuff();</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
=== goto ===
=== goto ===
{{works with|GCC|3.3.4}}
{{works with|GCC|3.3.4}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>


int main()
int main()
Line 446: Line 446:
std::cout << "Hello, World!\n";
std::cout << "Hello, World!\n";
goto LOOP;
goto LOOP;
}</lang>
}</syntaxhighlight>


Note that "goto" may also be used in conjunction with other forms of branching.
Note that "goto" may also be used in conjunction with other forms of branching.
Line 454: Line 454:


Exceptions are a way to give control back to a direct or indirect caller in case of an error. Note that throwing exceptions is usually very expensive, therefore they generally should only be used for exceptional situations.
Exceptions are a way to give control back to a direct or indirect caller in case of an error. Note that throwing exceptions is usually very expensive, therefore they generally should only be used for exceptional situations.
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <ostream>
#include <ostream>


Line 524: Line 524:
<< "inside foobar(). Thus this catch-all block never gets invoked.\n";
<< "inside foobar(). Thus this catch-all block never gets invoked.\n";
}
}
}</lang>
}</syntaxhighlight>


=={{header|COBOL}}==
=={{header|COBOL}}==
Line 546: Line 546:
=== GO TO ===
=== GO TO ===
Basic use:
Basic use:
<lang cobol> PROGRAM-ID. Go-To-Example.
<syntaxhighlight lang="cobol"> PROGRAM-ID. Go-To-Example.


PROCEDURE DIVISION.
PROCEDURE DIVISION.
Line 553: Line 553:


GO TO Foo
GO TO Foo
.</lang>
.</syntaxhighlight>


A <code>GO TO</code> can take a <code>DEPENDING ON</code> clause which will cause program flow to go to a certain paragraph/section depending on a certain value.
A <code>GO TO</code> can take a <code>DEPENDING ON</code> clause which will cause program flow to go to a certain paragraph/section depending on a certain value.
<lang cobol> GO TO First-Thing Second-Thing Third-Thing
<syntaxhighlight lang="cobol"> GO TO First-Thing Second-Thing Third-Thing
DEPENDING ON Thing-To-Do
DEPENDING ON Thing-To-Do


* *> Handle invalid thing...</lang>
* *> Handle invalid thing...</syntaxhighlight>
The previous example is equivalent to:
The previous example is equivalent to:
<lang cobol> EVALUATE Thing-To-Do
<syntaxhighlight lang="cobol"> EVALUATE Thing-To-Do
WHEN 1
WHEN 1
* *> Do first thing...
* *> Do first thing...
Line 573: Line 573:
WHEN OTHER
WHEN OTHER
* *> Handle invalid thing...
* *> Handle invalid thing...
END-EVALUATE</lang>
END-EVALUATE</syntaxhighlight>


=== ALTER ===
=== ALTER ===
Line 585: Line 585:


{{works with|GnuCOBOL}}
{{works with|GnuCOBOL}}
<syntaxhighlight lang="cobol">
<lang COBOL>
identification division.
identification division.
program-id. altering.
program-id. altering.
Line 624: Line 624:
*> fall through to the exit
*> fall through to the exit
exit program.
exit program.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 638: Line 638:
=== PERFORM ===
=== PERFORM ===
The <code>PERFORM</code> statement can be used to transfer program flow to the specified sections/paragraphs in the subprogram, with control being returned when the end of the last paragraph/section or a relevant <code>EXIT</code> statement is reached.
The <code>PERFORM</code> statement can be used to transfer program flow to the specified sections/paragraphs in the subprogram, with control being returned when the end of the last paragraph/section or a relevant <code>EXIT</code> statement is reached.
<lang cobol> PROGRAM-ID. Perform-Example.
<syntaxhighlight lang="cobol"> PROGRAM-ID. Perform-Example.


PROCEDURE DIVISION.
PROCEDURE DIVISION.
Line 660: Line 660:
Moo.
Moo.
DISPLAY "Moo"
DISPLAY "Moo"
.</lang>
.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 673: Line 673:
=={{header|Comal}}==
=={{header|Comal}}==
===Call a procedure===
===Call a procedure===
<lang Comal>myprocedure
<syntaxhighlight lang="comal">myprocedure
END // End of main program
END // End of main program
PROC myprocedure
PROC myprocedure
PRINT "Hello, this is a procedure"
PRINT "Hello, this is a procedure"
ENDPROC myprocedure</lang>
ENDPROC myprocedure</syntaxhighlight>


===Exit a loop===
===Exit a loop===
<lang Comal>LOOP
<syntaxhighlight lang="comal">LOOP
PRINT "I'm in a loop!"
PRINT "I'm in a loop!"
EXIT
EXIT
ENDLOOP
ENDLOOP
PRINT "But i somehow got out of it."</lang>
PRINT "But i somehow got out of it."</syntaxhighlight>


===Conditional exit===
===Conditional exit===
<lang Comal>PRINT "I'm in a loop!"
<syntaxhighlight lang="comal">PRINT "I'm in a loop!"
LOOP
LOOP
INPUT "Do you want to exit?":answer$
INPUT "Do you want to exit?":answer$
EXIT WHEN answer$="y"
EXIT WHEN answer$="y"
ENDLOOP
ENDLOOP
PRINT "You got out of it."</lang>
PRINT "You got out of it."</syntaxhighlight>


===Goto===
===Goto===
<lang Comal>PRINT "Hello world"
<syntaxhighlight lang="comal">PRINT "Hello world"
GOTO label
GOTO label
PRINT "This line will never be output"
PRINT "This line will never be output"
label:
label:
PRINT "This program will end thanks to the evil GOTO statement"
PRINT "This program will end thanks to the evil GOTO statement"
END</lang>
END</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==
=== goto ===
=== goto ===
<lang d>import std.stdio;
<syntaxhighlight lang="d">import std.stdio;


void main() {
void main() {
Line 710: Line 710:
writeln("I'm in your infinite loop.");
writeln("I'm in your infinite loop.");
goto label1;
goto label1;
}</lang>
}</syntaxhighlight>


=== Exceptions ===
=== Exceptions ===
D supports the try/catch/finally mechanism:
D supports the try/catch/finally mechanism:
<lang d>import std.stdio;
<syntaxhighlight lang="d">import std.stdio;


class DerivedException : Exception {
class DerivedException : Exception {
Line 735: Line 735:
writeln("finished (exception or none).");
writeln("finished (exception or none).");
}
}
}</lang>
}</syntaxhighlight>


=== Scope guards ===
=== Scope guards ===
Line 743: Line 743:


For instance:
For instance:
<lang d>import std.stdio;
<syntaxhighlight lang="d">import std.stdio;


void main(string[] args) {
void main(string[] args) {
Line 755: Line 755:
writeln("Gone, but we passed the first" ~
writeln("Gone, but we passed the first" ~
" chance to throw an exception.");
" chance to throw an exception.");
}</lang>
}</syntaxhighlight>


If the exception is thrown, then the only text that is written to the screen is "gone". If no exception is thrown, both calls to writeln occur.
If the exception is thrown, then the only text that is written to the screen is "gone". If no exception is thrown, both calls to writeln occur.
Line 785: Line 785:
===CATCH-THROW===
===CATCH-THROW===
Some Forth implementations have goto, but not the standard. It does have an exception mechanism.
Some Forth implementations have goto, but not the standard. It does have an exception mechanism.
<lang forth>: checked-array
<syntaxhighlight lang="forth">: checked-array
CREATE ( size -- ) DUP , CELLS ALLOT
CREATE ( size -- ) DUP , CELLS ALLOT
DOES> ( i -- a+i )
DOES> ( i -- a+i )
Line 797: Line 797:


: safe-access ( i -- a[i] )
: safe-access ( i -- a[i] )
['] myarray CATCH 1 = IF ." Out of bounds!" 0 THEN ;</lang>
['] myarray CATCH 1 = IF ." Out of bounds!" 0 THEN ;</syntaxhighlight>


=={{header|Fortran}}==
=={{header|Fortran}}==
Line 806: Line 806:
A compiler may offer the "assigned GO TO" facility, with statements such as <code>ASSIGN 120 TO THENCE</code> scattered about: 120 is a statement label, not an integer, and any statement label may be assigned to variable THENCE (which is an integer variable) as execution proceeds. A relatively restrained usage would be to select the label of a suitable FORMAT statement to use in a READ or WRITE statement in place of a fixed label, without affecting the flow of control. But <code>GO TO THENCE</code> will cause a GO TO for the current address held in THENCE... Should you yield to temptations such as <code>THENCE = THENCE - 6</code> (treating it as an ordinary integer), a subsequent <code>GO TO THENCE</code> may end execution with an error message, or something else...
A compiler may offer the "assigned GO TO" facility, with statements such as <code>ASSIGN 120 TO THENCE</code> scattered about: 120 is a statement label, not an integer, and any statement label may be assigned to variable THENCE (which is an integer variable) as execution proceeds. A relatively restrained usage would be to select the label of a suitable FORMAT statement to use in a READ or WRITE statement in place of a fixed label, without affecting the flow of control. But <code>GO TO THENCE</code> will cause a GO TO for the current address held in THENCE... Should you yield to temptations such as <code>THENCE = THENCE - 6</code> (treating it as an ordinary integer), a subsequent <code>GO TO THENCE</code> may end execution with an error message, or something else...


Aside from facilitating the production of spaghetti code, this sort of behaviour actually can be put to a positive use to handle the situation where in a large programme there may be portions that could be employed from a number of locations, and one does not wish to repeat that code each time - apart from the tedium of punching additional cards, each replication would demand its own unique set of statement labels. Further, such replication increases the total code size and memory is limited... <lang Fortran> ...
Aside from facilitating the production of spaghetti code, this sort of behaviour actually can be put to a positive use to handle the situation where in a large programme there may be portions that could be employed from a number of locations, and one does not wish to repeat that code each time - apart from the tedium of punching additional cards, each replication would demand its own unique set of statement labels. Further, such replication increases the total code size and memory is limited... <syntaxhighlight lang="fortran"> ...
ASSIGN 1101 to WHENCE !Remember my return point.
ASSIGN 1101 to WHENCE !Remember my return point.
GO TO 1000 !Dive into a "subroutine"
GO TO 1000 !Dive into a "subroutine"
Line 817: Line 817:
Common code, far away.
Common code, far away.
1000 do something !This has all the context available.
1000 do something !This has all the context available.
GO TO WHENCE !Return whence I came.</lang>
GO TO WHENCE !Return whence I came.</syntaxhighlight>
Since Algol in the 1960s it has been possible to define a routine within a larger routine that has access to all the context of the larger routine and so can be a convenient service routine for it, but Fortran does not allow a subroutine (or function) to be defined within a larger subroutine, except for the arithmetic statement function. One must write separate subroutines and struggle over providing access to context via COMMON and parameters. However, F90 introduced the MODULE arrangement whereby a collection of variables may all be referenced by a group of subroutines in the module without each having COMMON statements in common. Further, it allows a subroutine (or function) to use the CONTAINS feature, after which such a contained routine may be placed. Alas, it may not itself invoke CONTAINS even though Algol allows nesting as desired. And oddly, the contained routine must be at the ''end'' of the containing routine. So much for definition before usage. With such a facility, the possibility arises of perpetrating a GO TO from a contained routine to somewhere in its parent, however the F90 compilers are required to disallow access to outside labels, even those of FORMAT statements - rather a pity for that. Such escapes would have to copy whatever de-allocation steps were needed for a normal exit, which is simple enough on a stack-oriented design such as the B6700. However, its Algol compiler rejected attempts to jump from one routine ''into'' another (!) with the message "Bad GOTO. Too bad." Assembler programmers can do what they want, but for once, Fortran's designers show some restraint.
Since Algol in the 1960s it has been possible to define a routine within a larger routine that has access to all the context of the larger routine and so can be a convenient service routine for it, but Fortran does not allow a subroutine (or function) to be defined within a larger subroutine, except for the arithmetic statement function. One must write separate subroutines and struggle over providing access to context via COMMON and parameters. However, F90 introduced the MODULE arrangement whereby a collection of variables may all be referenced by a group of subroutines in the module without each having COMMON statements in common. Further, it allows a subroutine (or function) to use the CONTAINS feature, after which such a contained routine may be placed. Alas, it may not itself invoke CONTAINS even though Algol allows nesting as desired. And oddly, the contained routine must be at the ''end'' of the containing routine. So much for definition before usage. With such a facility, the possibility arises of perpetrating a GO TO from a contained routine to somewhere in its parent, however the F90 compilers are required to disallow access to outside labels, even those of FORMAT statements - rather a pity for that. Such escapes would have to copy whatever de-allocation steps were needed for a normal exit, which is simple enough on a stack-oriented design such as the B6700. However, its Algol compiler rejected attempts to jump from one routine ''into'' another (!) with the message "Bad GOTO. Too bad." Assembler programmers can do what they want, but for once, Fortran's designers show some restraint.


Once started on this path, many opportunities beckon: perhaps not just action "A" (achieved by "subroutine" 1000) is of use, there may be an action "B", and so on. One can then prepare the equivalent of a "to-do" list via something like<lang Fortran> ASSIGN 2000 TO WHENCE !Deviant "return" from 1000 to invoke 2000.
Once started on this path, many opportunities beckon: perhaps not just action "A" (achieved by "subroutine" 1000) is of use, there may be an action "B", and so on. One can then prepare the equivalent of a "to-do" list via something like<syntaxhighlight lang="fortran"> ASSIGN 2000 TO WHENCE !Deviant "return" from 1000 to invoke 2000.
ASSIGN 1103 TO THENCE !Desired return from 2000.
ASSIGN 1103 TO THENCE !Desired return from 2000.
GO TO 1000
GO TO 1000
1103 CONTINUE</lang>
1103 CONTINUE</syntaxhighlight>
So that "subroutine" 1000 would be invoked, which then invokes subroutine 2000, which returns via THENCE. And, instead of using simple variables such as THENCE and WHENCE, one could use an array and treat it like a stack... Those familiar with LISP or FORTH and similar languages will recognise a struggle to create new "verbs" from existing verbs, and their resulting usage in compound expressions. This is Philip Greenspun's "tenth" rule of programming.
So that "subroutine" 1000 would be invoked, which then invokes subroutine 2000, which returns via THENCE. And, instead of using simple variables such as THENCE and WHENCE, one could use an array and treat it like a stack... Those familiar with LISP or FORTH and similar languages will recognise a struggle to create new "verbs" from existing verbs, and their resulting usage in compound expressions. This is Philip Greenspun's "tenth" rule of programming.


Line 835: Line 835:


===Deviant RETURN===
===Deviant RETURN===
Similar possibilities arise with alternate returns from subroutines and functions, for instance to handle error conditions it might wish to report as with the READ statement. Thus, <code>CALL FRED(THIS,*123,*THENCE)</code> invokes a subroutine FRED with three parameters: THIS, then two oddities. The leading * (or &) signifies that these are no ordinary integers (or expressions) but instead are the labels of statements somewhere within the calling routine. Subroutine FRED might return in the normal way so that execution continues with the following statement, or, it may instead return with a GO TO for one of the labels...<lang Fortran> SUBROUTINE FRED(X,*,*) !With placeholders for unusual parameters.
Similar possibilities arise with alternate returns from subroutines and functions, for instance to handle error conditions it might wish to report as with the READ statement. Thus, <code>CALL FRED(THIS,*123,*THENCE)</code> invokes a subroutine FRED with three parameters: THIS, then two oddities. The leading * (or &) signifies that these are no ordinary integers (or expressions) but instead are the labels of statements somewhere within the calling routine. Subroutine FRED might return in the normal way so that execution continues with the following statement, or, it may instead return with a GO TO for one of the labels...<syntaxhighlight lang="fortran"> SUBROUTINE FRED(X,*,*) !With placeholders for unusual parameters.
...
...
RETURN !Normal return from FRED.
RETURN !Normal return from FRED.
...
...
RETURN 2 !Return to the second label.
RETURN 2 !Return to the second label.
END</lang>
END</syntaxhighlight>
More delicate souls prefer to see an integer parameter whose value will be set by FRED according to the desired condition, and every call to FRED would be followed by a computed GO TO on that value. Except that this statement is also disapproved of, so one is encouraged to code IF, or CASE, ''etc.'' and enjoy the repetition.
More delicate souls prefer to see an integer parameter whose value will be set by FRED according to the desired condition, and every call to FRED would be followed by a computed GO TO on that value. Except that this statement is also disapproved of, so one is encouraged to code IF, or CASE, ''etc.'' and enjoy the repetition.


Line 850: Line 850:
Similarly to escaping from a subroutine, within a DO-loop, a GO TO might jump out of the loop(s) - perhaps for good reason. More interesting is the possibility of jumping ''into'' a DO-loop's scope, possibly after jumping out - who knows what its index variable might have been changed to. This is considered poor form by others not writing such code and some compilers will reject any attempts. With the F77 introduction of IF ... THEN ... ELSE ... END IF constructions, jumping out of a block is still acceptable but jumping in is frowned on (even if only from the THEN clause to some part of its ELSE clause) and may be prevented.
Similarly to escaping from a subroutine, within a DO-loop, a GO TO might jump out of the loop(s) - perhaps for good reason. More interesting is the possibility of jumping ''into'' a DO-loop's scope, possibly after jumping out - who knows what its index variable might have been changed to. This is considered poor form by others not writing such code and some compilers will reject any attempts. With the F77 introduction of IF ... THEN ... ELSE ... END IF constructions, jumping out of a block is still acceptable but jumping in is frowned on (even if only from the THEN clause to some part of its ELSE clause) and may be prevented.


F90 offers a more decorous means for exiting DO-loops, including the additional DO WHILE loop, via the statements CYCLE and EXIT - the text "GO TO" does not appear as such, but the effect is the same. The CYCLE option means abandoning further statements within the block to test afresh the iteration condition, while EXIT means ending the iteration as if it had completed. Further syntax allows some compiler checking, as follows: <lang Fortran> XX:DO WHILE(condition)
F90 offers a more decorous means for exiting DO-loops, including the additional DO WHILE loop, via the statements CYCLE and EXIT - the text "GO TO" does not appear as such, but the effect is the same. The CYCLE option means abandoning further statements within the block to test afresh the iteration condition, while EXIT means ending the iteration as if it had completed. Further syntax allows some compiler checking, as follows: <syntaxhighlight lang="fortran"> XX:DO WHILE(condition)
statements...
statements...
NN:DO I = 1,N
NN:DO I = 1,N
Line 858: Line 858:
statements...
statements...
END DO NN
END DO NN
END DO XX </lang>
END DO XX </syntaxhighlight>
A DO-loop can be given a label such as XX (which is ''not'' in the numeric-only label area of fixed source format Fortran, and the syntax highlghter has missed yet another trick of Fortran syntax) and its corresponding END DO can be given a label also: the compiler checks that they match and some programmer errors might thereby be caught. With such labels in use, the CYCLE and EXIT statements can name the loop they are intended for, so that CYCLE NN steps to the next iteration for <code>I</code> (as if it were a GO TO the END DO having its label as a suffix) while the EXIT XX exits both the numeric DO-LOOP and the DO-WHILE loop - without such labels only the innermost loop is affected and one can lose track. These labels must not be the name of any other entity in the source, and specifically not the name of the variable of the DO-LOOP concerned. Thus, if there are many DO I = 1,N loops, each must have its own label. There is unfortunately no equivalent to <code>NEXT I</code> as in BASIC instead of <code>END DO</code>so as to be clear just which DO-LOOP is being ended and for which index variable.
A DO-loop can be given a label such as XX (which is ''not'' in the numeric-only label area of fixed source format Fortran, and the syntax highlghter has missed yet another trick of Fortran syntax) and its corresponding END DO can be given a label also: the compiler checks that they match and some programmer errors might thereby be caught. With such labels in use, the CYCLE and EXIT statements can name the loop they are intended for, so that CYCLE NN steps to the next iteration for <code>I</code> (as if it were a GO TO the END DO having its label as a suffix) while the EXIT XX exits both the numeric DO-LOOP and the DO-WHILE loop - without such labels only the innermost loop is affected and one can lose track. These labels must not be the name of any other entity in the source, and specifically not the name of the variable of the DO-LOOP concerned. Thus, if there are many DO I = 1,N loops, each must have its own label. There is unfortunately no equivalent to <code>NEXT I</code> as in BASIC instead of <code>END DO</code>so as to be clear just which DO-LOOP is being ended and for which index variable.


Line 883: Line 883:
Still, they are available when using the -lang qb dialect.
Still, they are available when using the -lang qb dialect.
This dialect provides the best support for the older QuickBASIC code.
This dialect provides the best support for the older QuickBASIC code.
<lang freebasic>
<syntaxhighlight lang="freebasic">
'$lang: "qb"
'$lang: "qb"


Line 898: Line 898:
Return
Return
Sleep
Sleep
</syntaxhighlight>
</lang>




=={{header|Gambas}}==
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=64e4e68b1c6ce73341d08ba2d9333c07 Click this link to run this code]'''
'''[https://gambas-playground.proko.eu/?gist=64e4e68b1c6ce73341d08ba2d9333c07 Click this link to run this code]'''
<lang gambas>Public Sub Main()
<syntaxhighlight lang="gambas">Public Sub Main()
Dim siCount As Short
Dim siCount As Short


Line 913: Line 913:
Goto LoopIt
Goto LoopIt


End</lang>
End</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 927: Line 927:
===Goto===
===Goto===
Go has goto and labels. The following is an infinite loop:
Go has goto and labels. The following is an infinite loop:
<lang go>func main() {
<syntaxhighlight lang="go">func main() {
inf:
inf:
goto inf
goto inf
}</lang>
}</syntaxhighlight>
Gotos can jump forward or backward within a function but they have some restrictions. They cannot jump into any block from outside the block, and they cannot cause any variable to come into scope.
Gotos can jump forward or backward within a function but they have some restrictions. They cannot jump into any block from outside the block, and they cannot cause any variable to come into scope.


Line 939: Line 939:


The defer statement sets a function or method to be executed upon return from the enclosing function. This is useful when a function has multiple returns. The classic example is closing a file:
The defer statement sets a function or method to be executed upon return from the enclosing function. This is useful when a function has multiple returns. The classic example is closing a file:
<lang go>import "os"
<syntaxhighlight lang="go">import "os"


func processFile() {
func processFile() {
Line 956: Line 956:
// more processing
// more processing
// f.Close() will get called here too
// f.Close() will get called here too
}</lang>
}</syntaxhighlight>


===Goroutines===
===Goroutines===
Line 962: Line 962:


The following program prints a mix of 1’s and 0’s.
The following program prints a mix of 1’s and 0’s.
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 977: Line 977:
fmt.Println("0")
fmt.Println("0")
}
}
}</lang>
}</syntaxhighlight>


A goroutine terminates upon return from the function called in the go statement. Unlike with a regular function call however, it cannot return a value--the calling goroutine has long continued and there is nothing waiting for a return value.
A goroutine terminates upon return from the function called in the go statement. Unlike with a regular function call however, it cannot return a value--the calling goroutine has long continued and there is nothing waiting for a return value.


Goroutines may not be able to communicate by returning values, but they have other ways. Principal is passing data through channels. Channel operations affect execution when they yield the processor, allowing other goroutines to run, but this does not normally alter flow of execution. The one exception is when channel operations are used in a select statement. A simple use,
Goroutines may not be able to communicate by returning values, but they have other ways. Principal is passing data through channels. Channel operations affect execution when they yield the processor, allowing other goroutines to run, but this does not normally alter flow of execution. The one exception is when channel operations are used in a select statement. A simple use,
<lang go>func answer(phone1, phone2 chan int) {
<syntaxhighlight lang="go">func answer(phone1, phone2 chan int) {
select {
select {
case <-phone1:
case <-phone1:
Line 989: Line 989:
// talk on phone two
// talk on phone two
}
}
}</lang>
}</syntaxhighlight>
Syntax is strongly reminiscent of the switch statement, but rules for flow control are very different. Select will block if no channel operation is possible. If one is possible, it will execute that case. If multiple operations are possible, it will pick one at random.
Syntax is strongly reminiscent of the switch statement, but rules for flow control are very different. Select will block if no channel operation is possible. If one is possible, it will execute that case. If multiple operations are possible, it will pick one at random.
===Process initialization===
===Process initialization===
Line 995: Line 995:


=={{header|GW-BASIC}}==
=={{header|GW-BASIC}}==
<lang qbasic>10 LET a=1
<syntaxhighlight lang="qbasic">10 LET a=1
20 IF a=2 THEN PRINT "This is a conditional statement"
20 IF a=2 THEN PRINT "This is a conditional statement"
30 IF a=1 THEN GOTO 50: REM a conditional jump
30 IF a=1 THEN GOTO 50: REM a conditional jump
Line 1,001: Line 1,001:
50 PRINT ("Hello" AND (1=2)): REM This does not print
50 PRINT ("Hello" AND (1=2)): REM This does not print
100 PRINT "Endless loop"
100 PRINT "Endless loop"
110 GOTO 100:REM an unconditional jump</lang>
110 GOTO 100:REM an unconditional jump</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
Line 1,007: Line 1,007:
In the context of normal, functional-style code, there are no flow-control statements, because explicit flow control is imperative. A monad may offer flow control; what kinds are available depends on the monad. For example, the [http://haskell.org/haskellwiki/New_monads/MonadExit <code>ExitT</code> monad transformer] lets you use the <code>exitWith</code> function to jump out a block of statements at will.
In the context of normal, functional-style code, there are no flow-control statements, because explicit flow control is imperative. A monad may offer flow control; what kinds are available depends on the monad. For example, the [http://haskell.org/haskellwiki/New_monads/MonadExit <code>ExitT</code> monad transformer] lets you use the <code>exitWith</code> function to jump out a block of statements at will.


<lang haskell>import Control.Monad
<syntaxhighlight lang="haskell">import Control.Monad
import Control.Monad.Trans
import Control.Monad.Trans
import Control.Monad.Exit
import Control.Monad.Exit
Line 1,018: Line 1,018:
when (x == 3 && y == 2) $
when (x == 3 && y == 2) $
exitWith ()
exitWith ()
putStrLn "Done."</lang>
putStrLn "Done."</syntaxhighlight>


=={{header|HicEst}}==
=={{header|HicEst}}==
[http://www.HicEst.com More on HicEst's ALARM function]
[http://www.HicEst.com More on HicEst's ALARM function]
<lang hicest>1 GOTO 2 ! branch to label
<syntaxhighlight lang="hicest">1 GOTO 2 ! branch to label


2 READ(FIle=name, IOStat=ios, ERror=3) something ! on error branch to label 3
2 READ(FIle=name, IOStat=ios, ERror=3) something ! on error branch to label 3
Line 1,038: Line 1,038:
8 y = LOG( 0 , *9) ! on error branch to label 9
8 y = LOG( 0 , *9) ! on error branch to label 9


9 ALARM( 999 ) ! quit HicEst immediately</lang>
9 ALARM( 999 ) ! quit HicEst immediately</syntaxhighlight>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
Line 1,053: Line 1,053:
'''break expr'''<br>
'''break expr'''<br>
Default value of ''expr'' is the null value ''&amp;null''. This operator breaks out of the enclosing loop, yielding the expression as the result of the loop. Normally loops yield a failure ie no result, so you can write code like this:
Default value of ''expr'' is the null value ''&amp;null''. This operator breaks out of the enclosing loop, yielding the expression as the result of the loop. Normally loops yield a failure ie no result, so you can write code like this:
<lang icon>
<syntaxhighlight lang="icon">
if x := every i := 1 to *container do { # * is the 'length' operator
if x := every i := 1 to *container do { # * is the 'length' operator
if container[i] ~== y then
if container[i] ~== y then
Line 1,063: Line 1,063:
else
else
write("did not find an item")
write("did not find an item")
</syntaxhighlight>
</lang>
The expression given to ''break'' can be another ''break'', which effectively lets you break out of two levels of loop. Finally, the expression given to break can be the ''next'' command; for example
The expression given to ''break'' can be another ''break'', which effectively lets you break out of two levels of loop. Finally, the expression given to break can be the ''next'' command; for example
<lang icon>
<syntaxhighlight lang="icon">
break break next
break break next
</syntaxhighlight>
</lang>
breaks out of two levels of loop and re-enters the top of the third-level enclosing loop.
breaks out of two levels of loop and re-enters the top of the third-level enclosing loop.


Line 1,075: Line 1,075:
'''fail'''<br>
'''fail'''<br>
Causes the the enclosing procedure to terminate without returning value. This is different from returning void or a null value that many other languages do when the code does not return an actual value. For example, in
Causes the the enclosing procedure to terminate without returning value. This is different from returning void or a null value that many other languages do when the code does not return an actual value. For example, in
<lang icon>
<syntaxhighlight lang="icon">
x := ftn()
x := ftn()
</syntaxhighlight>
</lang>
The value of x will not be replaced if ftn() issues the ''fail'' command. If ftn fails, then Goal-Directed Evaluation will also fail the assignment, therefore x is not assigned a new value. If the flow of control through a procedure falls off the end, the procedure implicitly fails.
The value of x will not be replaced if ftn() issues the ''fail'' command. If ftn fails, then Goal-Directed Evaluation will also fail the assignment, therefore x is not assigned a new value. If the flow of control through a procedure falls off the end, the procedure implicitly fails.


Line 1,090: Line 1,090:
'''error trapping'''<br>
'''error trapping'''<br>
The keyword &amp;error is normally zero, but if set to a positive value, this sets the number of fatal errors that are tolerated and converted to expression failure; the value of &amp;error is decremented if this happens. Therefore the now-common TRY-CATCH behaviour can be written as:
The keyword &amp;error is normally zero, but if set to a positive value, this sets the number of fatal errors that are tolerated and converted to expression failure; the value of &amp;error is decremented if this happens. Therefore the now-common TRY-CATCH behaviour can be written as:
<lang icon>
<syntaxhighlight lang="icon">
&error := 1
&error := 1
mayErrorOut()
mayErrorOut()
Line 1,099: Line 1,099:
handleError(&errornumber, &errortext, &errorvalue) # keyword values containing facts about the failure
handleError(&errornumber, &errortext, &errorvalue) # keyword values containing facts about the failure
}
}
</syntaxhighlight>
</lang>
Various idiomatic simplifications can be applied depending on your needs.
Various idiomatic simplifications can be applied depending on your needs.


'''error throwing'''<br>
'''error throwing'''<br>
Errors can be thrown using the function
Errors can be thrown using the function
<lang icon>
<syntaxhighlight lang="icon">
runerr(errnumber, errorvalue) # choose an error number and supply the offending value
runerr(errnumber, errorvalue) # choose an error number and supply the offending value
</syntaxhighlight>
</lang>


=={{header|IDL}}==
=={{header|IDL}}==
Line 1,112: Line 1,112:
===goto===
===goto===


<lang idl>test:
<syntaxhighlight lang="idl">test:
..some code here
..some code here
goto, test</lang>
goto, test</syntaxhighlight>


(This is almost never used)
(This is almost never used)
Line 1,120: Line 1,120:
===on_error===
===on_error===


<lang idl>on_error, test</lang>
<syntaxhighlight lang="idl">on_error, test</syntaxhighlight>


(This resumes at the label <tt>test</tt> if an error is encountered)
(This resumes at the label <tt>test</tt> if an error is encountered)
Line 1,126: Line 1,126:
===on_ioerror===
===on_ioerror===


<lang idl>on_ioerror, test</lang>
<syntaxhighlight lang="idl">on_ioerror, test</syntaxhighlight>


(Same as <tt>on_error</tt>, but for EOFs and read-errors and such)
(Same as <tt>on_error</tt>, but for EOFs and read-errors and such)
Line 1,132: Line 1,132:
===break===
===break===


<lang idl>break</lang>
<syntaxhighlight lang="idl">break</syntaxhighlight>


immediately terminates the innermost current loop (or <tt>if</tt> or <tt>case</tt> etc)
immediately terminates the innermost current loop (or <tt>if</tt> or <tt>case</tt> etc)
Line 1,138: Line 1,138:
===continue===
===continue===


<lang idl>continue</lang>
<syntaxhighlight lang="idl">continue</syntaxhighlight>


immediately starts the next iteration of the current innermost loop
immediately starts the next iteration of the current innermost loop
Line 1,147: Line 1,147:


For example, here's an example of a program which loops over a sequence of integers, multiplying them by two (j's default prompt is 3 spaces, which makes line-at-a-time copy-and-paste simple, and the result here is displayed on the following line):
For example, here's an example of a program which loops over a sequence of integers, multiplying them by two (j's default prompt is 3 spaces, which makes line-at-a-time copy-and-paste simple, and the result here is displayed on the following line):
<lang j> 2 * 1 2 3
<syntaxhighlight lang="j"> 2 * 1 2 3
2 4 6</lang>
2 4 6</syntaxhighlight>


That said, J's control structures are documented at http://www.jsoftware.com/help/dictionary/ctrl.htm So, if you want to perform this same operation using a while loop, or a goto, you can do so. It's just... often not a good idea (but sometimes they are indispensable).
That said, J's control structures are documented at http://www.jsoftware.com/help/dictionary/ctrl.htm So, if you want to perform this same operation using a while loop, or a goto, you can do so. It's just... often not a good idea (but sometimes they are indispensable).
Line 1,160: Line 1,160:
The <tt>break</tt> statement can be used to terminate a <tt>case</tt> clause in a <tt>switch</tt> statement and to terminate a <tt>for</tt>, <tt>while</tt> or <tt>do-while</tt> loop. In loops, a <tt>break</tt> can be ''labeled'' or ''unlabeled''.
The <tt>break</tt> statement can be used to terminate a <tt>case</tt> clause in a <tt>switch</tt> statement and to terminate a <tt>for</tt>, <tt>while</tt> or <tt>do-while</tt> loop. In loops, a <tt>break</tt> can be ''labeled'' or ''unlabeled''.


<lang Java>switch (xx) {
<syntaxhighlight lang="java">switch (xx) {
case 1:
case 1:
case 2:
case 2:
Line 1,192: Line 1,192:
}
}
...
...
} while (thisCondition);</lang>
} while (thisCondition);</syntaxhighlight>


===continue===
===continue===
The <tt>continue</tt> statement skips the current iteration of a <tt>for</tt>, <tt>while</tt>, or <tt>do-while</tt> loop. As with <tt>break</tt> the <tt>continue</tt> statement can be ''labeled'' or ''unlabeled'' to allow iterating a loop level other than the current one in nested loops.
The <tt>continue</tt> statement skips the current iteration of a <tt>for</tt>, <tt>while</tt>, or <tt>do-while</tt> loop. As with <tt>break</tt> the <tt>continue</tt> statement can be ''labeled'' or ''unlabeled'' to allow iterating a loop level other than the current one in nested loops.


<lang Java>while (condition) {
<syntaxhighlight lang="java">while (condition) {
...
...
if (someCondition) { continue; /* skip to beginning of this loop */ }
if (someCondition) { continue; /* skip to beginning of this loop */ }
Line 1,221: Line 1,221:
}
}
....
....
}</lang>
}</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
Line 1,240: Line 1,240:


Here is an example from the standard library:
Here is an example from the standard library:
<lang jq># Emit at most one item from the stream generated by g:
<syntaxhighlight lang="jq"># Emit at most one item from the stream generated by g:
def first(g): label $out | g | ., break $out;</lang>
def first(g): label $out | g | ., break $out;</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
Julia provides the @goto and @label macros for goto within functions. In addition, the "break" keyword is used for jumping out of a single loop, throw() of an exception can be used to jump out of a try() statement's code, and the assert() and exit() functions can be used to terminate a program.
Julia provides the @goto and @label macros for goto within functions. In addition, the "break" keyword is used for jumping out of a single loop, throw() of an exception can be used to jump out of a try() statement's code, and the assert() and exit() functions can be used to terminate a program.
<lang julia>
<syntaxhighlight lang="julia">
function example()
function example()
println("Hello ")
println("Hello ")
Line 1,253: Line 1,253:
println("world")
println("world")
end
end
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
Line 1,262: Line 1,262:


Here are some examples:
Here are some examples:
<lang scala>// version 1.0.6
<syntaxhighlight lang="scala">// version 1.0.6


fun main(args: Array<String>) {
fun main(args: Array<String>) {
Line 1,275: Line 1,275:
if (args.isNotEmpty()) throw IllegalArgumentException("No command line arguments should be supplied")
if (args.isNotEmpty()) throw IllegalArgumentException("No command line arguments should be supplied")
println("Goodbye!") // won't be executed
println("Goodbye!") // won't be executed
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,290: Line 1,290:
=={{header|Lua}}==
=={{header|Lua}}==
Lua has the <code>break</code>-command to exit loops.
Lua has the <code>break</code>-command to exit loops.
<lang lua>i = 0
<syntaxhighlight lang="lua">i = 0
while true do
while true do
i = i + 1
i = i + 1
if i > 10 then break end
if i > 10 then break end
end</lang>
end</syntaxhighlight>
===Tail calls as GOTOs===
===Tail calls as GOTOs===
The following code - though obviously a useless infinite loop - will '''not''' cause a stack overflow:
The following code - though obviously a useless infinite loop - will '''not''' cause a stack overflow:
<lang lua>function func1 ()
<syntaxhighlight lang="lua">function func1 ()
return func2()
return func2()
end
end
Line 1,305: Line 1,305:
end
end


func1()</lang>
func1()</syntaxhighlight>
This is because Lua supports proper tail recursion. This means that because something being returned is necessarily the last action in a function, the interpreter treats a function call in this 'tail' position much like a GOTO in other languages and does not create a new stack level.
This is because Lua supports proper tail recursion. This means that because something being returned is necessarily the last action in a function, the interpreter treats a function call in this 'tail' position much like a GOTO in other languages and does not create a new stack level.


Line 1,330: Line 1,330:


=={{header|MATLAB}} / {{header|Octave}}==
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang="matlab">
<lang Matlab>
try
try
% do some stuff
% do some stuff
Line 1,336: Line 1,336:
% in case of error, continue here
% in case of error, continue here
end
end
</syntaxhighlight>
</lang>


=={{header|Maxima}}==
=={{header|Maxima}}==
<lang maxima>/* goto */
<syntaxhighlight lang="maxima">/* goto */
block(..., label, ..., go(label), ...);
block(..., label, ..., go(label), ...);


Line 1,346: Line 1,346:


/* error trapping */
/* error trapping */
errcatch(..., error("Bad luck!"), ...);</lang>
errcatch(..., error("Bad luck!"), ...);</syntaxhighlight>


=={{header|MUMPS}}==
=={{header|MUMPS}}==
===GOTO / G===
===GOTO / G===
<p>The GOTO command jumps to a label. If the label is not in the current routine, it is necessary to include the circumflex and routine name. <lang MUMPS>GOTO LABEL^ROUTINE</lang>. This does not affect the subroutine stack, only the program pointer.</p><lang MUMPS>GOTO THERE</lang>
<p>The GOTO command jumps to a label. If the label is not in the current routine, it is necessary to include the circumflex and routine name. <syntaxhighlight lang="mumps">GOTO LABEL^ROUTINE</syntaxhighlight>. This does not affect the subroutine stack, only the program pointer.</p><syntaxhighlight lang="mumps">GOTO THERE</syntaxhighlight>


===HALT / H===
===HALT / H===
<p>Halt and Hang have the same abbreviation, i.e. "H" but (as a mnemonic) Halt takes no arguments. Halt stops the current process, and clears all Locks and devices in Use.
<p>Halt and Hang have the same abbreviation, i.e. "H" but (as a mnemonic) Halt takes no arguments. Halt stops the current process, and clears all Locks and devices in Use.
On the Cache variant of MUMPS, there is a $HALT special variable that can be set, the value of the $HALT special variable is a routine that is called before cleaning up (in effect, a specialized final error trap).</p>
On the Cache variant of MUMPS, there is a $HALT special variable that can be set, the value of the $HALT special variable is a routine that is called before cleaning up (in effect, a specialized final error trap).</p>
<lang MUMPS> Read "Do you really wish to halt (Y/N)?",Q#1
<syntaxhighlight lang="mumps"> Read "Do you really wish to halt (Y/N)?",Q#1
IF Q="Y"!Q="y" HALT</lang>
IF Q="Y"!Q="y" HALT</syntaxhighlight>


===JOB / J===
===JOB / J===
<p> The JOB command starts another MUMPS job starting at a label. If the label is not in the current routine, it is necessary to include the circumflex and routine name. <lang MUMPS>JOB LABEL^ROUTINE</lang>. <lang MUMPS>JOB THERE</lang> This does not affect the subroutine stack, nor the program pointer in the current job. Since MUMPS is a multi-processing (rather than multi-threading) language, the new job is independent of the current job.</p>
<p> The JOB command starts another MUMPS job starting at a label. If the label is not in the current routine, it is necessary to include the circumflex and routine name. <syntaxhighlight lang="mumps">JOB LABEL^ROUTINE</syntaxhighlight>. <syntaxhighlight lang="mumps">JOB THERE</syntaxhighlight> This does not affect the subroutine stack, nor the program pointer in the current job. Since MUMPS is a multi-processing (rather than multi-threading) language, the new job is independent of the current job.</p>
<lang MUMPS> JOB LABEL^ROUTINE</lang>
<syntaxhighlight lang="mumps"> JOB LABEL^ROUTINE</syntaxhighlight>




===QUIT / Q===
===QUIT / Q===
<p>Exits a loop, or routine. It decreases the stack level. It can return a value to a calling routine if there is a value after it.</p><p>Quit is one of the commands that requires two spaces after it if it is followed in a line by more commands.</p>
<p>Exits a loop, or routine. It decreases the stack level. It can return a value to a calling routine if there is a value after it.</p><p>Quit is one of the commands that requires two spaces after it if it is followed in a line by more commands.</p>
<lang MUMPS>FOR I=1:1:1 QUIT:NoLoop DO YesLoop
<syntaxhighlight lang="mumps">FOR I=1:1:1 QUIT:NoLoop DO YesLoop
QUIT Returnvalue</lang>
QUIT Returnvalue</syntaxhighlight>
===XECUTE / X===
===XECUTE / X===
<p>eXecute acts as if it were a one line Do command. Its argument must be a string of valid MUMPS code, and it performs that code in a new stack level. There is an implied Quit at the end of each eXecute's argument string.</p><lang MUMPS> SET A="SET %=$INCREMENT(I)"
<p>eXecute acts as if it were a one line Do command. Its argument must be a string of valid MUMPS code, and it performs that code in a new stack level. There is an implied Quit at the end of each eXecute's argument string.</p><syntaxhighlight lang="mumps"> SET A="SET %=$INCREMENT(I)"
SET I=0
SET I=0
XECUTE A
XECUTE A
WRITE I</lang>
WRITE I</syntaxhighlight>
The above block will output "1".
The above block will output "1".
<math>Insert formula here</math>
<math>Insert formula here</math>
Line 1,388: Line 1,388:
The <tt>LEAVE</tt> instruction causes immediate exit from one or more <tt>DO</tt>, <tt>SELECT</tt> or <tt>LOOP</tt> constructs.
The <tt>LEAVE</tt> instruction causes immediate exit from one or more <tt>DO</tt>, <tt>SELECT</tt> or <tt>LOOP</tt> constructs.


<lang NetRexx>loop xx = 1 to 10
<syntaxhighlight lang="netrexx">loop xx = 1 to 10
if xx = 1 then leave -- loop terminated by leave
if xx = 1 then leave -- loop terminated by leave
say 'unreachable'
say 'unreachable'
end</lang>
end</syntaxhighlight>


A ''<tt>name</tt>'' parameter can be provided to direct <tt>LEAVE</tt> to a specific end of block (as defined by a <tt>LABEL</tt> option or in the case of a controlled <tt>LOOP</tt> the control variable of the loop.
A ''<tt>name</tt>'' parameter can be provided to direct <tt>LEAVE</tt> to a specific end of block (as defined by a <tt>LABEL</tt> option or in the case of a controlled <tt>LOOP</tt> the control variable of the loop.


<lang NetRexx>loop xx = 1 to 10 -- xx is the control variable
<syntaxhighlight lang="netrexx">loop xx = 1 to 10 -- xx is the control variable
...
...
loop yy = 1 to 10 -- yy is the control variable
loop yy = 1 to 10 -- yy is the control variable
Line 1,437: Line 1,437:
otherwise do; say 'nl selection'; say '...'; end
otherwise do; say 'nl selection'; say '...'; end
end selecting
end selecting
end vv</lang>
end vv</syntaxhighlight>


===ITERATE===
===ITERATE===
Line 1,444: Line 1,444:


As with <tt>LEAVE</tt> an optional ''<tt>name</tt>'' parameter can be supplied to direct the instruction to a loop level outside the current level.
As with <tt>LEAVE</tt> an optional ''<tt>name</tt>'' parameter can be supplied to direct the instruction to a loop level outside the current level.
<lang NetRexx>loop fff = 0 to 9
<syntaxhighlight lang="netrexx">loop fff = 0 to 9
...
...
loop xx = 1 to 3
loop xx = 1 to 3
Line 1,452: Line 1,452:
end
end
...
...
end fff</lang>
end fff</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
===Labeled Break & Continue===
===Labeled Break & Continue===
Break and continue can be used with block labels to jump out of multiple loops:
Break and continue can be used with block labels to jump out of multiple loops:
<lang nim>block outer:
<syntaxhighlight lang="nim">block outer:
for i in 0..1000:
for i in 0..1000:
for j in 0..1000:
for j in 0..1000:
if i + j == 3:
if i + j == 3:
break outer</lang>
break outer</syntaxhighlight>


===Try-Except-Finally===
===Try-Except-Finally===
<lang nim>var f = open "input.txt"
<syntaxhighlight lang="nim">var f = open "input.txt"
try:
try:
var s = readLine f
var s = readLine f
Line 1,470: Line 1,470:
echo "An error occurred!"
echo "An error occurred!"
finally:
finally:
close f</lang>
close f</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 1,476: Line 1,476:
An OCaml user can simulate flow control using exceptions:
An OCaml user can simulate flow control using exceptions:


<lang ocaml>exception Found of int
<syntaxhighlight lang="ocaml">exception Found of int


let () =
let () =
Line 1,485: Line 1,485:
print_endline "nothing found"
print_endline "nothing found"
with Found res ->
with Found res ->
Printf.printf "found %d\n" res</lang>
Printf.printf "found %d\n" res</syntaxhighlight>


=={{header|Oforth}}==
=={{header|Oforth}}==
Line 1,492: Line 1,492:


break allows to break the current loop :
break allows to break the current loop :
<lang Oforth>break</lang>
<syntaxhighlight lang="oforth">break</syntaxhighlight>


continue allows to immediately start a new iteration :
continue allows to immediately start a new iteration :
<lang Oforth>continue</lang>
<syntaxhighlight lang="oforth">continue</syntaxhighlight>


perform is a method that transfer execution to the runnable on top of the stack, then returns :
perform is a method that transfer execution to the runnable on top of the stack, then returns :
<lang Oforth>perform</lang>
<syntaxhighlight lang="oforth">perform</syntaxhighlight>


=={{header|Oz}}==
=={{header|Oz}}==
Line 1,504: Line 1,504:


The <code>case</code> statement can be used for [[Pattern Matching]], but also like a switch statement in C:
The <code>case</code> statement can be used for [[Pattern Matching]], but also like a switch statement in C:
<lang oz>case {OS.rand} mod 3
<syntaxhighlight lang="oz">case {OS.rand} mod 3
of 0 then {Foo}
of 0 then {Foo}
[] 1 then {Bar}
[] 1 then {Bar}
[] 2 then {Buzz}
[] 2 then {Buzz}
end</lang>
end</syntaxhighlight>


The Lisp-influenced [http://www.mozart-oz.org/home/doc/loop/index.html for-loop] is very powerful and convenient to use.
The Lisp-influenced [http://www.mozart-oz.org/home/doc/loop/index.html for-loop] is very powerful and convenient to use.
Line 1,524: Line 1,524:
As an example for <code>choice</code>, a simple, but stupid way to solve the equation 2*X=18. We assume that the solution is somewhere in the interval 8-10, but we do not quite know what exactly it is.
As an example for <code>choice</code>, a simple, but stupid way to solve the equation 2*X=18. We assume that the solution is somewhere in the interval 8-10, but we do not quite know what exactly it is.


<lang oz>declare
<syntaxhighlight lang="oz">declare
proc {Stupid X}
proc {Stupid X}
choice
choice
Line 1,538: Line 1,538:
end
end
in
in
{Show {SearchOne Stupid}}</lang>
{Show {SearchOne Stupid}}</syntaxhighlight>


{{out}}
{{out}}
Line 1,553: Line 1,553:
=={{header|Pascal}}==
=={{header|Pascal}}==
===goto===
===goto===
<lang pascal>label
<syntaxhighlight lang="pascal">label
jumpto;
jumpto;
begin
begin
Line 1,562: Line 1,562:
goto jumpto;
goto jumpto;
...
...
end;</lang>
end;</syntaxhighlight>


===exception===
===exception===
<lang pascal>try
<syntaxhighlight lang="pascal">try
Z := DoDiv (X,Y);
Z := DoDiv (X,Y);
except
except
on EDivException do Z := 0;
on EDivException do Z := 0;
end;</lang>
end;</syntaxhighlight>


===Halt===
===Halt===
Halt stops program execution and returns control to the calling program. The optional argument
Halt stops program execution and returns control to the calling program. The optional argument
Errnum specifies an exit value. If omitted, zero is returned.
Errnum specifies an exit value. If omitted, zero is returned.
<lang pascal>procedure halt(errnum: Byte);</lang>
<syntaxhighlight lang="pascal">procedure halt(errnum: Byte);</syntaxhighlight>


===Exit===
===Exit===
Line 1,581: Line 1,581:
The optional argument X allows to specify a return value, in the case Exit
The optional argument X allows to specify a return value, in the case Exit
is invoked in a function. The function result will then be equal to X.
is invoked in a function. The function result will then be equal to X.
<lang pascal>procedure exit(const X: TAnyType)</lang>
<syntaxhighlight lang="pascal">procedure exit(const X: TAnyType)</syntaxhighlight>


Calls of functions/procedures as well as breaks and continues in loops are described in the corresponding tasks.
Calls of functions/procedures as well as breaks and continues in loops are described in the corresponding tasks.
Line 1,591: Line 1,591:
Goto is typically looked down upon by most Perl programmers
Goto is typically looked down upon by most Perl programmers


<lang perl>FORK:
<syntaxhighlight lang="perl">FORK:
# some code
# some code
goto FORK;</lang>
goto FORK;</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
Line 1,599: Line 1,599:
===goto===
===goto===
In 0.8.4+ Phix finally has a goto statement:
In 0.8.4+ Phix finally has a goto statement:
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (no goto in JavaScript)</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (no goto in JavaScript)</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">()</span>
Line 1,608: Line 1,608:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">p</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">p</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
Imposing a self-policed rule that all jumps must be forward (or equivalently all backward, but never mixed) is recommended.
Imposing a self-policed rule that all jumps must be forward (or equivalently all backward, but never mixed) is recommended.


Line 1,624: Line 1,624:


Previous versions had no hll goto statement, however the following work around was (and still is) available:
Previous versions had no hll goto statement, however the following work around was (and still is) available:
<lang Phix>without js
<syntaxhighlight lang="phix">without js
#ilASM{ jmp :label }
#ilASM{ jmp :label }
...
...
#ilASM{ ::label }</lang>
#ilASM{ ::label }</syntaxhighlight>
In top level code, label scope is restricted to a single ilASM construct,
In top level code, label scope is restricted to a single ilASM construct,
but within a routine, the scope is across all the ilasm in that routine.
but within a routine, the scope is across all the ilasm in that routine.
Line 1,636: Line 1,636:


It is also possible to declare global labels, which are superficially similar:
It is also possible to declare global labels, which are superficially similar:
<lang Phix>without js
<syntaxhighlight lang="phix">without js
#ilASM{ call :%label }
#ilASM{ call :%label }
...
...
Line 1,642: Line 1,642:
:%label
:%label
ret
ret
::skip }</lang>
::skip }</syntaxhighlight>
Global labels cannot be declared inside a routine, and as shown (almost always)
Global labels cannot be declared inside a routine, and as shown (almost always)
require a skip construct. It is up to the programmer to ensure global labels
require a skip construct. It is up to the programmer to ensure global labels
Line 1,659: Line 1,659:
Personally I must agree with Douglas Crockford who says "I have never seen a piece of code that was not improved by refactoring it to remove the continue statement".<br>
Personally I must agree with Douglas Crockford who says "I have never seen a piece of code that was not improved by refactoring it to remove the continue statement".<br>
Causes the next interation of the immediately surrounding loop to begin immediately, with any condition evaluated normally. The following two loops behave identically:
Causes the next interation of the immediately surrounding loop to begin immediately, with any condition evaluated normally. The following two loops behave identically:
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">100</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">100</span> <span style="color: #008080;">do</span>
Line 1,671: Line 1,671:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


===exit===
===exit===
causes immediate termination of the immediately surrounding for or while loop, with control passing to the first statement after the loop, eg:
causes immediate termination of the immediately surrounding for or while loop, with control passing to the first statement after the loop, eg:
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">100</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">100</span> <span style="color: #008080;">do</span>
Line 1,683: Line 1,683:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


===break===
===break===
Line 1,708: Line 1,708:
Introduced in PHP 5.3, PHP now has a goto flow-control structure, even though most PHP programmers see it as a bad habbit (may cause spaghetti-code).
Introduced in PHP 5.3, PHP now has a goto flow-control structure, even though most PHP programmers see it as a bad habbit (may cause spaghetti-code).


<lang php><?php
<syntaxhighlight lang="php"><?php
goto a;
goto a;
echo 'Foo';
echo 'Foo';
Line 1,714: Line 1,714:
a:
a:
echo 'Bar';
echo 'Bar';
?></lang>
?></syntaxhighlight>
{{out}}
{{out}}
<pre>Bar</pre>
<pre>Bar</pre>
Line 1,764: Line 1,764:


=={{header|PL/I}}==
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
LEAVE
LEAVE
The LEAVE statement terminates execution of a loop.
The LEAVE statement terminates execution of a loop.
Line 1,794: Line 1,794:
labelled statements. (This form is superseded by SELECT, above.)
labelled statements. (This form is superseded by SELECT, above.)
[GO TO can also be spelled as GOTO].
[GO TO can also be spelled as GOTO].
</syntaxhighlight>
</lang>


=={{header|Pop11}}==
=={{header|Pop11}}==
Line 1,801: Line 1,801:
quitloop with argument exits from nested loops:
quitloop with argument exits from nested loops:


<lang pop11>while condition1 do
<syntaxhighlight lang="pop11">while condition1 do
while condition2 do
while condition2 do
if condition3 then
if condition3 then
Line 1,807: Line 1,807:
endif;
endif;
endwhile;
endwhile;
endwhile;</lang>
endwhile;</syntaxhighlight>


above quitloop(2) exits from both loops.
above quitloop(2) exits from both loops.
Line 1,816: Line 1,816:
nested loops:
nested loops:


<lang pop11>while condition1 do
<syntaxhighlight lang="pop11">while condition1 do
while condition2 do
while condition2 do
if condition3 then
if condition3 then
Line 1,823: Line 1,823:
endwhile;
endwhile;
endwhile;
endwhile;
l:;</lang>
l:;</syntaxhighlight>


Another use is to implement finite state machines:
Another use is to implement finite state machines:


<lang pop11>state1:
<syntaxhighlight lang="pop11">state1:
DO_SOMETHING();
DO_SOMETHING();
if condition1 then
if condition1 then
Line 1,842: Line 1,842:
...
...
stateN:
stateN:
....</lang>
....</syntaxhighlight>


Pop11 goto is a nonlocal one, so "jump out" from a chain of procedure calls:
Pop11 goto is a nonlocal one, so "jump out" from a chain of procedure calls:


<lang pop11>define outer();
<syntaxhighlight lang="pop11">define outer();
define inner(n);
define inner(n);
if n = 0 then
if n = 0 then
Line 1,855: Line 1,855:
inner(5);
inner(5);
final:;
final:;
enddefine;</lang>
enddefine;</syntaxhighlight>


This is useful to exit early from successful recursive search, and for exception handling.
This is useful to exit early from successful recursive search, and for exception handling.
Line 1,863: Line 1,863:
go_on is a multiway jump
go_on is a multiway jump


<lang pop11>go_on expression to lab1, lab2, ..., labN else elselab ;</lang>
<syntaxhighlight lang="pop11">go_on expression to lab1, lab2, ..., labN else elselab ;</syntaxhighlight>


If expression has value K the above will jump to label labK, if expression is not an integer, or if it outside range from 1 to N, then control passes to label elselab. The else part may be omitted (then out of range values of expression cause an exception).
If expression has value K the above will jump to label labK, if expression is not an integer, or if it outside range from 1 to N, then control passes to label elselab. The else part may be omitted (then out of range values of expression cause an exception).
Line 1,882: Line 1,882:
it is just:
it is just:


<lang pop11>return;</lang>
<syntaxhighlight lang="pop11">return;</syntaxhighlight>


but it is also possible to specify one or more return values:
but it is also possible to specify one or more return values:


<lang pop11>return(val1, val2, val3);</lang>
<syntaxhighlight lang="pop11">return(val1, val2, val3);</syntaxhighlight>


===chain===
===chain===
Line 1,892: Line 1,892:
chain has effect of "tail call" but is not necessarily in tail position. More precisely inside proc1.
chain has effect of "tail call" but is not necessarily in tail position. More precisely inside proc1.


<lang pop11>chain proc2(x1, x2, x3);</lang>
<syntaxhighlight lang="pop11">chain proc2(x1, x2, x3);</syntaxhighlight>


finishes execution of proc1 and transfers control to the proc2 passing it x1, x2, and x3 as arguments. On return from proc2 control passes to caller of proc1.
finishes execution of proc1 and transfers control to the proc2 passing it x1, x2, and x3 as arguments. On return from proc2 control passes to caller of proc1.
Line 1,901: Line 1,901:
===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()
<syntaxhighlight lang="purebasic">If OpenConsole()
top:
top:
i = i + 1
i = i + 1
Line 1,912: Line 1,912:
Input()
Input()
CloseConsole()
CloseConsole()
EndIf </lang>
EndIf </syntaxhighlight>
===Gosub & Return===
===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 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.
Gosub is useful when building fast structured code with very low overhead.
<lang PureBasic>X=1: Y=2
<syntaxhighlight lang="purebasic">X=1: Y=2
Gosub Calc
Gosub Calc
;X will now equal 7
;X will now equal 7
Line 1,923: Line 1,923:
Calc:
Calc:
X+3*Y
X+3*Y
Return ; Returns to the point in the code where the Gosub jumped from</lang>
Return ; Returns to the point in the code where the Gosub jumped from</syntaxhighlight>
===FakeReturn===
===FakeReturn===
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.
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.
<lang PureBasic>Gosub MySub
<syntaxhighlight lang="purebasic">Gosub MySub


Lable2:
Lable2:
Line 1,937: Line 1,937:
Goto Lable2
Goto Lable2
EndIf
EndIf
Return</lang>
Return</syntaxhighlight>


===OnErrorGoto===
===OnErrorGoto===
This will transferee the program execution to the defined label if an error accrue.
This will transferee the program execution to the defined label if an error accrue.
<lang PureBasic>OnErrorGoto(?MyExitHandler)
<syntaxhighlight lang="purebasic">OnErrorGoto(?MyExitHandler)


X=1: Y=0
X=1: Y=0
Line 1,951: Line 1,951:
MyExitHandler:
MyExitHandler:
MessageRequester("Error", ErrorMessage())
MessageRequester("Error", ErrorMessage())
End</lang>
End</syntaxhighlight>
===OnErrorCall===
===OnErrorCall===
Similar to OnErrorGoto() but procedural instead.
Similar to OnErrorGoto() but procedural instead.
<lang PureBasic>Procedure MyErrorHandler()
<syntaxhighlight lang="purebasic">Procedure MyErrorHandler()
;All open files etc can be closed here
;All open files etc can be closed here
MessageRequester("Error", ErrorMessage())
MessageRequester("Error", ErrorMessage())
Line 1,963: Line 1,963:
X=1: Y=0
X=1: Y=0
Z= X/Y
Z= X/Y
;This line should never be reached</lang>
;This line should never be reached</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
===Loops===
===Loops===
Python supports ''break'' and ''continue'' to exit from a loop early or short circuit the rest of a loop's body and "continue" on to the next loop iteration.
Python supports ''break'' and ''continue'' to exit from a loop early or short circuit the rest of a loop's body and "continue" on to the next loop iteration.
<lang python># Search for an odd factor of a using brute force:
<syntaxhighlight lang="python"># Search for an odd factor of a using brute force:
for i in range(n):
for i in range(n):
if (n%2) == 0:
if (n%2) == 0:
Line 1,977: Line 1,977:
else:
else:
result = None
result = None
print "No odd factors found"</lang>
print "No odd factors found"</syntaxhighlight>
In addition, as shown in the foregoing example, Python loops support an ''else:'' suite which can be used to handle cases when the loop was intended to search for something, where the code would break out of the loop upon finding its target. In that situation the ''else:'' suite can be used to handle the failure. (In most other languages one is forced to use a "sentinel value" or a special flag variable ... typically set to "False" before the loop and conditionally set to "True" within the loop to handle situations for which the Python ''else:'' on loops is intended).
In addition, as shown in the foregoing example, Python loops support an ''else:'' suite which can be used to handle cases when the loop was intended to search for something, where the code would break out of the loop upon finding its target. In that situation the ''else:'' suite can be used to handle the failure. (In most other languages one is forced to use a "sentinel value" or a special flag variable ... typically set to "False" before the loop and conditionally set to "True" within the loop to handle situations for which the Python ''else:'' on loops is intended).


Line 1,990: Line 1,990:
A custom Exception class is normally declared with the ''pass'' statement as no methods of the parent class are over-ridden, no additional functionality is defined and no attributes need be set. Example:
A custom Exception class is normally declared with the ''pass'' statement as no methods of the parent class are over-ridden, no additional functionality is defined and no attributes need be set. Example:


<lang python>class MyException(Exception): pass</lang>
<syntaxhighlight lang="python">class MyException(Exception): pass</syntaxhighlight>


One normally would choose the most similar existing class. For example if MyException was going to be raised for some situation involving an invalid value it might be better to make it a subclass of ValueError; if it was somehow related to issues with inappropriate objects being passed around then one might make it a subclass of TypeError.
One normally would choose the most similar existing class. For example if MyException was going to be raised for some situation involving an invalid value it might be better to make it a subclass of ValueError; if it was somehow related to issues with inappropriate objects being passed around then one might make it a subclass of TypeError.
Line 1,998: Line 1,998:
To create a "virtual base class" (one which is not intended to be directly instantiated, but exists solely to provide an inheritance to it's derived classes) one normally defines the requisite methods to raise "NotImplementedError" like so:
To create a "virtual base class" (one which is not intended to be directly instantiated, but exists solely to provide an inheritance to it's derived classes) one normally defines the requisite methods to raise "NotImplementedError" like so:


<lang python>class MyVirtual(object):
<syntaxhighlight lang="python">class MyVirtual(object):
def __init__(self):
def __init__(self):
raise NotImplementedError</lang>
raise NotImplementedError</syntaxhighlight>


It then becomes necessary for any descendants of this class to over-ride the ''__init__()'' method. Any attempt to instantiate a "MyVirtual" object directly will raise an exception.
It then becomes necessary for any descendants of this class to over-ride the ''__init__()'' method. Any attempt to instantiate a "MyVirtual" object directly will raise an exception.
Line 2,006: Line 2,006:


'''Case 1 - Try, Except'''
'''Case 1 - Try, Except'''
<lang python>try:
<syntaxhighlight lang="python">try:
temp = 0/0
temp = 0/0
# 'except' catches any errors that may have been raised between the code of 'try' and 'except'
# 'except' catches any errors that may have been raised between the code of 'try' and 'except'
except: # Note: catch all handler ... NOT RECOMMENDED
except: # Note: catch all handler ... NOT RECOMMENDED
print "An error occurred."
print "An error occurred."
# Output : "An error occurred"</lang>
# Output : "An error occurred"</syntaxhighlight>


'''Case 2 - Try, Except'''
'''Case 2 - Try, Except'''
<lang python>try:
<syntaxhighlight lang="python">try:
temp = 0/0
temp = 0/0
# here, 'except' catches a specific type of error raised within the try block.
# here, 'except' catches a specific type of error raised within the try block.
except ZeroDivisionError:
except ZeroDivisionError:
print "You've divided by zero!"
print "You've divided by zero!"
# Output : "You've divided by zero!"</lang>
# Output : "You've divided by zero!"</syntaxhighlight>


'''Case 3 - Try, Except, Finally'''
'''Case 3 - Try, Except, Finally'''
<lang python>try:
<syntaxhighlight lang="python">try:
temp = 0/0
temp = 0/0
except:
except:
Line 2,032: Line 2,032:
# Output :
# Output :
# An error occurred
# An error occurred
# End of 'try' block...</lang>
# End of 'try' block...</syntaxhighlight>


Note: Prior to version 2.5 a ''try:'' statement could contain either series of ''except:'' clauses '''or''' a ''finally:'' clause but '''not both.'''
Note: Prior to version 2.5 a ''try:'' statement could contain either series of ''except:'' clauses '''or''' a ''finally:'' clause but '''not both.'''
It was thus necessary to nest the exception handling in an enclosing ''try:''...''finally:'' loop like so:
It was thus necessary to nest the exception handling in an enclosing ''try:''...''finally:'' loop like so:


<lang python>try:
<syntaxhighlight lang="python">try:
try:
try:
pass
pass
Line 2,044: Line 2,044:
except SomeOtherException:
except SomeOtherException:
finally:
finally:
do_some_cleanup() # run in any case, whether any exceptions were thrown or not</lang>
do_some_cleanup() # run in any case, whether any exceptions were thrown or not</syntaxhighlight>


'''Case 4 - Try, Except, Else'''
'''Case 4 - Try, Except, Else'''
<lang python>try:
<syntaxhighlight lang="python">try:
temp = 1/1 # not a division by zero error
temp = 1/1 # not a division by zero error
except ZeroDivisionError: # so... it is not caught
except ZeroDivisionError: # so... it is not caught
Line 2,055: Line 2,055:
print "No apparent error occurred."
print "No apparent error occurred."
# Output :
# Output :
# No apparent error occurred.</lang>
# No apparent error occurred.</syntaxhighlight>


'''Case 5 - Try, Except, break, continue'''
'''Case 5 - Try, Except, break, continue'''
<lang python>i = 0
<syntaxhighlight lang="python">i = 0
while 1: # infinite loop
while 1: # infinite loop
try:
try:
Line 2,077: Line 2,077:
# Output :
# Output :
# You've divided by zero. Decrementing i and continuing...
# You've divided by zero. Decrementing i and continuing...
# Imaginary Number! Breaking out of loop</lang>
# Imaginary Number! Breaking out of loop</syntaxhighlight>


'''Case 6 - Creating your own custom exceptions, raise'''
'''Case 6 - Creating your own custom exceptions, raise'''
<lang python># Let's call our custom error "StupidError"; it inherits from the Exception class
<syntaxhighlight lang="python"># Let's call our custom error "StupidError"; it inherits from the Exception class


class StupidError(Exception): pass
class StupidError(Exception): pass
Line 2,092: Line 2,092:


# Output :
# Output :
# Something stupid occurred: Segfault</lang>
# Something stupid occurred: Segfault</syntaxhighlight>


===continue, else in "for" loop===
===continue, else in "for" loop===
<lang python> i = 101
<syntaxhighlight lang="python"> i = 101
for i in range(4): # loop 4 times
for i in range(4): # loop 4 times
print "I will always be seen."
print "I will always be seen."
Line 2,114: Line 2,114:
if(__name__ == "__main__"):
if(__name__ == "__main__"):
main()</lang>
main()</syntaxhighlight>


===The "with" statement===
===The "with" statement===
Line 2,120: Line 2,120:
See [[http://www.python.org/peps/pep-0343.html PEP 0343, The "with" statement]]
See [[http://www.python.org/peps/pep-0343.html PEP 0343, The "with" statement]]


<lang python>class Quitting(Exception): pass
<syntaxhighlight lang="python">class Quitting(Exception): pass
max = 10
max = 10
with open("some_file") as myfile:
with open("some_file") as myfile:
Line 2,128: Line 2,128:
if exit_counter > max:
if exit_counter > max:
raise Quitting
raise Quitting
print line,</lang>
print line,</syntaxhighlight>


The ''with'' statement allows classes to encapsulate "final" (clean-up) code which will automatically be executed regardless of exceptions that occur when working "with" these objects. Thus, for the foregoing example, the file will be closed regardless of whether it's more than 10 lines long. Many built-in and standard library classes have "context managers" which facilitate their use in ''with:'' code. In addition it's possible to define special __enter__() and __exit__() methods in one's own classes which will be implicitly called by the interpreter when an object is used within a ''with:'' statement.
The ''with'' statement allows classes to encapsulate "final" (clean-up) code which will automatically be executed regardless of exceptions that occur when working "with" these objects. Thus, for the foregoing example, the file will be closed regardless of whether it's more than 10 lines long. Many built-in and standard library classes have "context managers" which facilitate their use in ''with:'' code. In addition it's possible to define special __enter__() and __exit__() methods in one's own classes which will be implicitly called by the interpreter when an object is used within a ''with:'' statement.
Line 2,152: Line 2,152:
Racket doesn't have a <tt>goto</tt>, but like other implementations of Scheme, it adopts the mantra of "Lambda: the Ultimate GOTO" by having all tail calls optimized. This allows writing code that is no different from your average assembly code -- for example, here's a direct translation of [[Greatest_common_divisor#x86_Assembly]] into a Racket function:
Racket doesn't have a <tt>goto</tt>, but like other implementations of Scheme, it adopts the mantra of "Lambda: the Ultimate GOTO" by having all tail calls optimized. This allows writing code that is no different from your average assembly code -- for example, here's a direct translation of [[Greatest_common_divisor#x86_Assembly]] into a Racket function:


<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket


Line 2,173: Line 2,173:
(return %eax))
(return %eax))
(main))
(main))
</syntaxhighlight>
</lang>


=== Exceptions ===
=== Exceptions ===
Line 2,179: Line 2,179:
Racket has exceptions which are used in the usual way, and <tt>with-handlers</tt> to catch them. In fact, any value can be raised, not just exceptions. For example:
Racket has exceptions which are used in the usual way, and <tt>with-handlers</tt> to catch them. In fact, any value can be raised, not just exceptions. For example:


<lang racket>
<syntaxhighlight lang="racket">
(define (list-product l)
(define (list-product l)
(with-handlers ([void identity])
(with-handlers ([void identity])
Line 2,186: Line 2,186:
[(zero? (car l)) (raise 0)]
[(zero? (car l)) (raise 0)]
[else (loop (cdr l) (* r (car l)))]))))
[else (loop (cdr l) (* r (car l)))]))))
</syntaxhighlight>
</lang>


=== Continuations ===
=== Continuations ===
Line 2,204: Line 2,204:
Phasers are blocks that are transparent to the normal control flow but that are automatically called at an appropriate phase of compilation or execution. The current list of phasers may be found in [[https://design.raku.org/S04.html#Phasers S04/Phasers]].
Phasers are blocks that are transparent to the normal control flow but that are automatically called at an appropriate phase of compilation or execution. The current list of phasers may be found in [[https://design.raku.org/S04.html#Phasers S04/Phasers]].
===goto===
===goto===
<lang perl6>TOWN: goto TOWN;</lang>
<syntaxhighlight lang="raku" line>TOWN: goto TOWN;</syntaxhighlight>
Labels that have not been defined yet must be enclosed in quotes.
Labels that have not been defined yet must be enclosed in quotes.


=={{header|REBOL}}==
=={{header|REBOL}}==
<lang REBOL>REBOL [
<syntaxhighlight lang="rebol">REBOL [
Title: "Flow Control"
Title: "Flow Control"
URL: http://rosettacode.org/wiki/Flow_Control_Structures
URL: http://rosettacode.org/wiki/Flow_Control_Structures
Line 2,275: Line 2,275:
print div 10 2
print div 10 2
print div 10 1
print div 10 1
print div 10 0</lang>
print div 10 0</syntaxhighlight>




Line 2,291: Line 2,291:


(Also, see '''function invocation''' and '''signal''' statement below.)
(Also, see '''function invocation''' and '''signal''' statement below.)
<lang rexx>call routineName /*no arguments passed to routine.*/
<syntaxhighlight lang="rexx">call routineName /*no arguments passed to routine.*/
call routineName 50 /*one argument (fifty) passed. */
call routineName 50 /*one argument (fifty) passed. */
call routineName 50,60 /*two arguments passed. */
call routineName 50,60 /*two arguments passed. */
Line 2,301: Line 2,301:
call routineName ,,,,,,,,,,,,,,,,800 /*17 args passed, 16 omitted. */
call routineName ,,,,,,,,,,,,,,,,800 /*17 args passed, 16 omitted. */
call date /*looks for DATE internally first*/
call date /*looks for DATE internally first*/
call 'DATE' /* " " " BIF | externally*/</lang>
call 'DATE' /* " " " BIF | externally*/</syntaxhighlight>
real-life example:
real-life example:
<lang rexx>numeric digits 1000 /*prepare for some gihugeic numbers.*/
<syntaxhighlight lang="rexx">numeric digits 1000 /*prepare for some gihugeic numbers.*/
...
...
n=4
n=4
Line 2,315: Line 2,315:
!=!*j
!=!*j
end /*j*/
end /*j*/
return !</lang>
return !</syntaxhighlight>


===case===
===case===
Line 2,333: Line 2,333:


(Also, see the '''return''' statement below.)
(Also, see the '''return''' statement below.)
<lang rexx>exit
<syntaxhighlight lang="rexx">exit


exit expression</lang>
exit expression</syntaxhighlight>


===function invocation===
===function invocation===
Line 2,347: Line 2,347:


(Also, see the '''call''' statement above.)
(Also, see the '''call''' statement above.)
<lang rexx>numeric digits 1000 /*prepare for some gihugeic numbers.*/
<syntaxhighlight lang="rexx">numeric digits 1000 /*prepare for some gihugeic numbers.*/
...
...
n=4
n=4
Line 2,358: Line 2,358:
!=!*j
!=!*j
end /*j*/
end /*j*/
return !</lang>
return !</syntaxhighlight>


===iterate===
===iterate===
Line 2,364: Line 2,364:


(All indentations in REXX are merely cosmetic and are used for readability.}
(All indentations in REXX are merely cosmetic and are used for readability.}
<lang rexx>sum=0
<syntaxhighlight lang="rexx">sum=0
do j=1 to 1000
do j=1 to 1000
if j//3==0 | j//7==0 then iterate
if j//3==0 | j//7==0 then iterate
Line 2,381: Line 2,381:
end /*m*/
end /*m*/
end /*k*/
end /*k*/
say 'prod=' prod</lang>
say 'prod=' prod</syntaxhighlight>


===go to===
===go to===
Line 2,388: Line 2,388:
===leave===
===leave===
The '''leave''' statement transfer control to the next REXX statement following the &nbsp; '''end''' &nbsp; statement of the current (active) &nbsp; '''do''' &nbsp; loop in which the '''leave''' statement is located. &nbsp; The '''leave''' statement can also specify which '''do''' loop is to be left (terminated) if the '''do''' loop has a named variable.
The '''leave''' statement transfer control to the next REXX statement following the &nbsp; '''end''' &nbsp; statement of the current (active) &nbsp; '''do''' &nbsp; loop in which the '''leave''' statement is located. &nbsp; The '''leave''' statement can also specify which '''do''' loop is to be left (terminated) if the '''do''' loop has a named variable.
<lang rexx> do j=1 to 10
<syntaxhighlight lang="rexx"> do j=1 to 10
say 'j=' j
say 'j=' j
if j>5 then leave
if j>5 then leave
Line 2,405: Line 2,405:
end /*m*/
end /*m*/
end /*k*/
end /*k*/
say 'sum=' sum</lang>
say 'sum=' sum</syntaxhighlight>


===raising conditions===
===raising conditions===
Line 2,416: Line 2,416:


(Also, see the '''signal''' statement below.)
(Also, see the '''signal''' statement below.)
<lang rexx>...
<syntaxhighlight lang="rexx">...
signal on syntax
signal on syntax
...
...
Line 2,447: Line 2,447:
say; say 'error code:' condition('D')
say; say 'error code:' condition('D')
say; say "Moral: don't do that."
say; say "Moral: don't do that."
exit 13</lang>
exit 13</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,480: Line 2,480:
following the instruction/command where the condition was encountered.
following the instruction/command where the condition was encountered.
<br>A short example:
<br>A short example:
<lang rexx>Say 'Interrupt this program after a short while'
<syntaxhighlight lang="rexx">Say 'Interrupt this program after a short while'
Call on halt
Call on halt
Do i=1 To 10000000
Do i=1 To 10000000
Line 2,486: Line 2,486:
End
End
halt: Say i j
halt: Say i j
Return</lang>
Return</syntaxhighlight>


===return===
===return===
Line 2,498: Line 2,498:


(Also, see the '''exit''' statement above.)
(Also, see the '''exit''' statement above.)
<lang rexx>return
<syntaxhighlight lang="rexx">return


return expression</lang>
return expression</syntaxhighlight>


===select===
===select===
The '''select''' statement is used to conditionally test for cases to selectively execute REXX statement(s).
The '''select''' statement is used to conditionally test for cases to selectively execute REXX statement(s).
<lang rexx>...
<syntaxhighlight lang="rexx">...
prod=1
prod=1
a=7 /*or somesuch.*/
a=7 /*or somesuch.*/
Line 2,525: Line 2,525:
end /*select*/
end /*select*/


say 'result for' a op b "=" r</lang>
say 'result for' a op b "=" r</syntaxhighlight>


===signal===
===signal===
Line 2,552: Line 2,552:


(Also, see '''raising conditions''' above.)
(Also, see '''raising conditions''' above.)
<lang rexx>...
<syntaxhighlight lang="rexx">...
signal on error
signal on error
signal on failure
signal on failure
Line 2,585: Line 2,585:
say; say 'REXX variable:' condition('D')
say; say 'REXX variable:' condition('D')
say; say "Moral: shouldn't do that."
say; say "Moral: shouldn't do that."
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,606: Line 2,606:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
i = 1
i = 1
while true
while true
Line 2,613: Line 2,613:
i = i + 1
i = i + 1
end
end
</syntaxhighlight>
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 2,623: Line 2,623:
=== exceptions ===
=== exceptions ===
Use <code>raise</code> to throw an exception. You catch exceptions in the <code>rescue</code> clause of a <code>begin...end</code> block.
Use <code>raise</code> to throw an exception. You catch exceptions in the <code>rescue</code> clause of a <code>begin...end</code> block.
<lang ruby>begin
<syntaxhighlight lang="ruby">begin
# some code that may raise an exception
# some code that may raise an exception
rescue ExceptionClassA => a
rescue ExceptionClassA => a
Line 2,635: Line 2,635:
ensure
ensure
# execute this code always
# execute this code always
end</lang>
end</syntaxhighlight>
There is also a rescue modifier (example from the [http://www.pragprog.com/titles/ruby/programming-ruby Pickaxe book]):
There is also a rescue modifier (example from the [http://www.pragprog.com/titles/ruby/programming-ruby Pickaxe book]):
<lang ruby>values = ["1", "2.3", /pattern/]
<syntaxhighlight lang="ruby">values = ["1", "2.3", /pattern/]
result = values.map {|v| Integer(v) rescue Float(v) rescue String(v)}
result = values.map {|v| Integer(v) rescue Float(v) rescue String(v)}
# => [1, 2.3, "(?-mix:pattern)"]</lang>
# => [1, 2.3, "(?-mix:pattern)"]</syntaxhighlight>


=== catch and throw ===
=== catch and throw ===
<code>break</code> will only break out of a single level of loop. You can surround code in a catch block, and within the block you can throw a string or symbol to jump out to the end of the catch block (Ruby's GOTO, I suppose):
<code>break</code> will only break out of a single level of loop. You can surround code in a catch block, and within the block you can throw a string or symbol to jump out to the end of the catch block (Ruby's GOTO, I suppose):
<lang ruby>def some_method
<syntaxhighlight lang="ruby">def some_method
# ...
# ...
if some_condition
if some_condition
Line 2,659: Line 2,659:
end
end


puts "continuing after catching the throw"</lang>
puts "continuing after catching the throw"</syntaxhighlight>
=== yield ===
=== yield ===
<code>yield</code> passes control from the currently executing method to its code block.
<code>yield</code> passes control from the currently executing method to its code block.


=={{header|SAS}}==
=={{header|SAS}}==
<lang sas>/* GOTO: as in other languages
<syntaxhighlight lang="sas">/* GOTO: as in other languages
STOP: to stop current data step */
STOP: to stop current data step */
data _null_;
data _null_;
Line 2,704: Line 2,704:
8 44
8 44
;
;
run;</lang>
run;</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
{{libheader|Scala}}
{{libheader|Scala}}
<lang Scala>import Goto._
<syntaxhighlight lang="scala">import Goto._
import scala.util.continuations._
import scala.util.continuations._


Line 2,733: Line 2,733:
}
}


}</lang>
}</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
===goto===
===goto===
<lang ruby>say "Hello"
<syntaxhighlight lang="ruby">say "Hello"
goto :world
goto :world
say "Never printed"
say "Never printed"
@:world
@:world
say "World"</lang>
say "World"</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,751: Line 2,751:
=== Indirect absolute jump ===
=== Indirect absolute jump ===
The <tt>000 n to CI</tt> instruction loads the value stored at address <i>n</i> into the Current Instruction register. For instance,
The <tt>000 n to CI</tt> instruction loads the value stored at address <i>n</i> into the Current Instruction register. For instance,
<lang ssem>00101000000000000000000000000000 20 to CI
<syntaxhighlight lang="ssem">00101000000000000000000000000000 20 to CI
...
...
01010000000000000000000000000000 20. 10</lang>
01010000000000000000000000000000 20. 10</syntaxhighlight>
loads the number 10 into CI. Since CI is incremented <i>after</i> the instruction has been executed, rather than before, this fragment will cause execution to jump to address 11.
loads the number 10 into CI. Since CI is incremented <i>after</i> the instruction has been executed, rather than before, this fragment will cause execution to jump to address 11.


=== Indirect relative jump ===
=== Indirect relative jump ===
<tt>100 Add n to CI</tt> increases the number in the CI register by the value stored at address <i>n</i>.
<tt>100 Add n to CI</tt> increases the number in the CI register by the value stored at address <i>n</i>.
<lang ssem>00101000000001000000000000000000 Add 20 to CI
<syntaxhighlight lang="ssem">00101000000001000000000000000000 Add 20 to CI
...
...
01010000000000000000000000000000 20. 10</lang>
01010000000000000000000000000000 20. 10</syntaxhighlight>
adds 10 to CI. Once again, CI is incremented <i>after</i> the instruction has been executed: so the machine actually jumps ahead by 11 instructions.
adds 10 to CI. Once again, CI is incremented <i>after</i> the instruction has been executed: so the machine actually jumps ahead by 11 instructions.


Line 2,769: Line 2,769:
As an example, let's find a Pythagorean triple a,b,c such that a+b+c=n, where n is given. Here goto is used to break the two loops when such a triple is found. A '''[https://www.stata.com/help.cgi?m2_return return]''' can be used in such situations, unless one has to do further computations after the loop.
As an example, let's find a Pythagorean triple a,b,c such that a+b+c=n, where n is given. Here goto is used to break the two loops when such a triple is found. A '''[https://www.stata.com/help.cgi?m2_return return]''' can be used in such situations, unless one has to do further computations after the loop.


<lang stata>mata
<syntaxhighlight lang="stata">mata
function pythagorean_triple(n) {
function pythagorean_triple(n) {
for (a=1; a<=n; a++) {
for (a=1; a<=n; a++) {
Line 2,784: Line 2,784:


pythagorean_triple(1980)
pythagorean_triple(1980)
165 900 915</lang>
165 900 915</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
Line 2,792: Line 2,792:
at some future time asynchronously, like this
at some future time asynchronously, like this


<lang tcl>after 1000 {myroutine x}</lang>
<syntaxhighlight lang="tcl">after 1000 {myroutine x}</syntaxhighlight>


which will call "<tt>myroutine</tt>" with parameter "<tt>x</tt>" 1000ms from 'now';
which will call "<tt>myroutine</tt>" with parameter "<tt>x</tt>" 1000ms from 'now';
Line 2,799: Line 2,799:
The scheduled task can be removed from the scheduler for example with
The scheduled task can be removed from the scheduler for example with


<lang tcl>after cancel myroutine</lang>
<syntaxhighlight lang="tcl">after cancel myroutine</syntaxhighlight>


(other ways are possible).
(other ways are possible).
Line 2,808: Line 2,808:
whose display is updated once a second:
whose display is updated once a second:


<lang tcl>package require Tk
<syntaxhighlight lang="tcl">package require Tk
proc update {} {
proc update {} {
.clockface configure -text [clock format [clock seconds]]
.clockface configure -text [clock format [clock seconds]]
Line 2,815: Line 2,815:
# now just create the 'clockface' and call ;update' once:
# now just create the 'clockface' and call ;update' once:
pack [label .clockface]
pack [label .clockface]
update</lang>
update</syntaxhighlight>


=== loop control ===
=== loop control ===
Line 2,822: Line 2,822:
=== exception ===
=== exception ===
Tcl's <code>catch</code> command can be used to provide a basic exception-handling mechanism:
Tcl's <code>catch</code> command can be used to provide a basic exception-handling mechanism:
<lang tcl>if {[catch { ''... code that might give error ...'' } result]} {
<syntaxhighlight lang="tcl">if {[catch { ''... code that might give error ...'' } result]} {
puts "Error was $result"
puts "Error was $result"
} else {
} else {
''... process $result ...''
''... process $result ...''
}</lang>
}</syntaxhighlight>
Tcl 8.6 also has a <tt>try</tt>…<tt>trap</tt>…<tt>finally</tt> structure for more complex exception handling.
Tcl 8.6 also has a <tt>try</tt>…<tt>trap</tt>…<tt>finally</tt> structure for more complex exception handling.
<lang tcl>try {
<syntaxhighlight lang="tcl">try {
# Just a silly example...
# Just a silly example...
set f [open $filename]
set f [open $filename]
Line 2,837: Line 2,837:
} finally {
} finally {
close $f
close $f
}</lang>
}</syntaxhighlight>


=== custom control structures ===
=== custom control structures ===
A novel aspect of Tcl is that it's relatively easy to create new control structures (more detail at http://wiki.tcl.tk/685).
A novel aspect of Tcl is that it's relatively easy to create new control structures (more detail at http://wiki.tcl.tk/685).
For example, this example defines a command to perform some operation for each line of an input file:
For example, this example defines a command to perform some operation for each line of an input file:
<lang tcl>proc forfilelines {linevar filename code} {
<syntaxhighlight lang="tcl">proc forfilelines {linevar filename code} {
upvar $linevar line ; # connect local variable line to caller's variable
upvar $linevar line ; # connect local variable line to caller's variable
set filechan [open $filename]
set filechan [open $filename]
Line 2,849: Line 2,849:
}
}
close $filechan
close $filechan
}</lang>
}</syntaxhighlight>
Now we can use it to print the length of each line of file "mydata.txt":
Now we can use it to print the length of each line of file "mydata.txt":
<lang tcl>forfilelines myline mydata.txt {
<syntaxhighlight lang="tcl">forfilelines myline mydata.txt {
puts [string length $myline]
puts [string length $myline]
}</lang>
}</syntaxhighlight>


=={{header|Tiny BASIC}}==
=={{header|Tiny BASIC}}==
<lang tinybasic> REM TinyBASIC has only two control flow structures: goto and gosub
<syntaxhighlight lang="tinybasic"> REM TinyBASIC has only two control flow structures: goto and gosub
LET N = 0
LET N = 0
10 LET N = N + 1
10 LET N = N + 1
Line 2,886: Line 2,886:
105 PRINT "55555"
105 PRINT "55555"
LET N = N + 1
LET N = N + 1
RETURN REM one return can serve several gosubs</lang>
RETURN REM one return can serve several gosubs</syntaxhighlight>


=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
Line 2,894: Line 2,894:
This skips the line that changes the value of x to 5.
This skips the line that changes the value of x to 5.


<lang vbnet> Sub bar2()
<syntaxhighlight lang="vbnet"> Sub bar2()
Dim x = 0
Dim x = 0
GoTo label
GoTo label
Line 2,900: Line 2,900:
label:
label:
Console.WriteLine(x)
Console.WriteLine(x)
End Sub</lang>
End Sub</syntaxhighlight>


=== On Error Goto ===
=== On Error Goto ===
Line 2,906: Line 2,906:
This brances in the event of an error. Usually there is an Exit (Sub|Function) to seperate the normal code from the error handling code
This brances in the event of an error. Usually there is an Exit (Sub|Function) to seperate the normal code from the error handling code


<lang vbnet> Sub foo()
<syntaxhighlight lang="vbnet"> Sub foo()
On Error GoTo label
On Error GoTo label
'do something dangerous
'do something dangerous
Line 2,912: Line 2,912:
label:
label:
Console.WriteLine("Operation Failed")
Console.WriteLine("Operation Failed")
End Sub</lang>
End Sub</syntaxhighlight>


''This style of code is rarely used.''
''This style of code is rarely used.''
Line 2,920: Line 2,920:
This performs a sequence of actions. If any action fails, the exception is discarded and next operation is performed.
This performs a sequence of actions. If any action fails, the exception is discarded and next operation is performed.


<lang vbnet>Sub foo2()
<syntaxhighlight lang="vbnet">Sub foo2()
On Error Resume Next
On Error Resume Next
Operation1()
Operation1()
Line 2,926: Line 2,926:
Operation3()
Operation3()
Operation4()
Operation4()
End Sub</lang>
End Sub</syntaxhighlight>


''This style of code is rarely used.''
''This style of code is rarely used.''
Line 2,934: Line 2,934:
This shows the classical and modern syntax for exiting a sub routine early.
This shows the classical and modern syntax for exiting a sub routine early.


<lang vbnet>Sub Foo1()
<syntaxhighlight lang="vbnet">Sub Foo1()
If Not WorkNeeded() Then Exit Sub
If Not WorkNeeded() Then Exit Sub
DoWork()
DoWork()
Line 2,942: Line 2,942:
If Not WorkNeeded() Then Return
If Not WorkNeeded() Then Return
DoWork()
DoWork()
End Sub</lang>
End Sub</syntaxhighlight>


=== Return value / Exit Function ===
=== Return value / Exit Function ===
Line 2,950: Line 2,950:
This variable is write-only.
This variable is write-only.


<lang vbnet>Function Foo3()
<syntaxhighlight lang="vbnet">Function Foo3()
Foo3 = CalculateValue()
Foo3 = CalculateValue()
If Not MoreWorkNeeded() Then Exit Function
If Not MoreWorkNeeded() Then Exit Function
Line 2,960: Line 2,960:
If Not MoreWorkNeeded() Then Return result
If Not MoreWorkNeeded() Then Return result
Return CalculateAnotherValue()
Return CalculateAnotherValue()
End Function</lang>
End Function</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
Line 2,976: Line 2,976:


The following code demonstrates each of the above apart from '''Fiber.suspend''' which simply exits a CLI script.
The following code demonstrates each of the above apart from '''Fiber.suspend''' which simply exits a CLI script.
<lang ecmascript>var func = Fn.new { |n|
<syntaxhighlight lang="ecmascript">var func = Fn.new { |n|
var i = 1
var i = 1
while (true) {
while (true) {
Line 3,001: Line 3,001:
var error = fiber.try() // catch any error
var error = fiber.try() // catch any error
System.print("Caught error: " + error)
System.print("Caught error: " + error)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,015: Line 3,015:
=={{header|Yabasic}}==
=={{header|Yabasic}}==
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}
<lang yabasic>
<syntaxhighlight lang="yabasic">
gosub subrutina
gosub subrutina


Line 3,028: Line 3,028:
return
return
end
end
</syntaxhighlight>
</lang>


=={{header|Z80 Assembly}}==
=={{header|Z80 Assembly}}==
Line 3,039: Line 3,039:
===Breaking out of a loop===
===Breaking out of a loop===
A subroutine that loops is often escaped with a conditional return, or, if it needs to unwind the stack frame, a conditional jump to an unconditional return.
A subroutine that loops is often escaped with a conditional return, or, if it needs to unwind the stack frame, a conditional jump to an unconditional return.
<lang z80>PrintString:
<syntaxhighlight lang="z80">PrintString:
ld a,(hl) ;HL is our pointer to the string we want to print
ld a,(hl) ;HL is our pointer to the string we want to print
cp 0 ;it's better to use OR A to compare A to zero, but for demonstration purposes this is easier to read.
cp 0 ;it's better to use OR A to compare A to zero, but for demonstration purposes this is easier to read.
Line 3,045: Line 3,045:
call PrintChar ;prints accumulator's ascii code to screen - on Amstrad CPC for example this label points to memory address &BB5A
call PrintChar ;prints accumulator's ascii code to screen - on Amstrad CPC for example this label points to memory address &BB5A
inc hl ;next char
inc hl ;next char
jr PrintString ;jump back to the start of the loop. RET Z is our only exit.</lang>
jr PrintString ;jump back to the start of the loop. RET Z is our only exit.</syntaxhighlight>


In the above example, the stack was never modified (besides the CALL pushing the return address) so <code>RET Z</code> was safe to use. Conditional returns are not safe to use if the stack needs to be unwound prior to exiting, since there's no way to conditionally unwind the stack without conditionally jumping to a section of code that does just that. In which case you don't need the return to be conditional anyway. This contrived example shows this in action.
In the above example, the stack was never modified (besides the CALL pushing the return address) so <code>RET Z</code> was safe to use. Conditional returns are not safe to use if the stack needs to be unwound prior to exiting, since there's no way to conditionally unwind the stack without conditionally jumping to a section of code that does just that. In which case you don't need the return to be conditional anyway. This contrived example shows this in action.


<lang z80>foo:
<syntaxhighlight lang="z80">foo:
push af
push af
bar:
bar:
Line 3,060: Line 3,060:
exit:
exit:
pop af
pop af
ret</lang>
ret</syntaxhighlight>


===DJNZ===
===DJNZ===
Line 3,067: Line 3,067:
(Game Boy doesn't have this instruction - you'll have to use a combination of <code>DEC B</code> and <code>JR NZ</code>)
(Game Boy doesn't have this instruction - you'll have to use a combination of <code>DEC B</code> and <code>JR NZ</code>)


<lang z80>loop:
<syntaxhighlight lang="z80">loop:
;your code goes here
;your code goes here
DJNZ loop</lang>
DJNZ loop</syntaxhighlight>


===Block Instructions===
===Block Instructions===
Line 3,077: Line 3,077:


Essentially it's equivalent to the code below, except in the real <code>LDIR</code> the accumulator isn't actually altered in any way.
Essentially it's equivalent to the code below, except in the real <code>LDIR</code> the accumulator isn't actually altered in any way.
<lang z80>_LDIR:
<syntaxhighlight lang="z80">_LDIR:
ld a,(hl)
ld a,(hl)
ld (de),a
ld (de),a
Line 3,085: Line 3,085:
ld a,b
ld a,b
or c ;compare BC to zero
or c ;compare BC to zero
jr nz,_LDIR ;Game Boy doesn't have LDIR so you'll have to use this code instead.</lang>
jr nz,_LDIR ;Game Boy doesn't have LDIR so you'll have to use this code instead.</syntaxhighlight>


There are several others that work in a similar fashion, such as:
There are several others that work in a similar fashion, such as:
Line 3,097: Line 3,097:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>continue; continue(n); // continue nth nested loop
<syntaxhighlight lang="zkl">continue; continue(n); // continue nth nested loop
break; break(n); // break out of nth nested loop
break; break(n); // break out of nth nested loop
try{ ... }catch(exception){ ... } [else{ ... }]
try{ ... }catch(exception){ ... } [else{ ... }]
onExit(fcn); // run fcn when enclosing function exits</lang>
onExit(fcn); // run fcn when enclosing function exits</syntaxhighlight>
zkl has state machines for functional style stream processing. There are some special values that machines can return for flow control:
zkl has state machines for functional style stream processing. There are some special values that machines can return for flow control:
<pre>
<pre>
Line 3,113: Line 3,113:
</pre>
</pre>
As an example, decode URL strings:
As an example, decode URL strings:
<lang zkl>urlText.pump(String,
<syntaxhighlight lang="zkl">urlText.pump(String,
fcn(c){ if(c=="%")return(Void.Read,2); return(Void.Skip,c) },
fcn(c){ if(c=="%")return(Void.Read,2); return(Void.Skip,c) },
fcn(_,b,c){(b+c).toInt(16).toChar()})</lang>
fcn(_,b,c){(b+c).toInt(16).toChar()})</syntaxhighlight>
has two machines. The second machine only runs if "%" is seen.
has two machines. The second machine only runs if "%" is seen.
<lang zkl>urlText:="http%3A%2F%2Ffoo.com%2Fbar";
<syntaxhighlight lang="zkl">urlText:="http%3A%2F%2Ffoo.com%2Fbar";
urlText.pump(...).println();</lang>
urlText.pump(...).println();</syntaxhighlight>
{{out}}<pre>http://foo.com/bar</pre>
{{out}}<pre>http://foo.com/bar</pre>