Flow-control structures: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (corrected goto header)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 76:
BXH 3,4,LOOPI r3=r3+r4; if r3>r5 then loop
</lang>
 
 
=={{header|6502 Assembly}}==
Line 312 ⟶ 311:
return 0;
}</lang>
 
=={{header|C sharp}}==
===return===
terminates the function and returns control to the caller.
<lang csharp>int GetNumber() {
return 5;
}</lang>
===throw===
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.
<lang csharp>try {
if (someCondition) {
throw new Exception();
}
} catch (Exception ex) {
LogException(ex);
throw;
} finally {
cleanUp();
}</lang>
===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.
<lang csharp>public static void Main() {
foreach (int n in Numbers(i => i >= 2) {
Console.WriteLine("Got " + n);
}
}
 
IEnumerable<int> Numbers(Func<int, bool> predicate) {
for (int i = 0; ; i++) {
if (predicate(i)) yield break;
Console.WriteLine("Yielding " + i);
yield return i;
}
}
</lang>
{{out}}
<pre>
Yielding 0
Got 0
Yielding 1
Got 1</pre>
===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.
<lang csharp>async Task DoStuffAsync() {
DoSomething();
await someOtherTask;//returns control to caller if someOtherTask is not yet finished.
DoSomethingElse();
}
</lang>
===break and continue===
<code>continue</code> causes the closest enclosing loop to skip the current iteration and start the next iteration immediately.<br/>
<code>break</code> terminates the closest enclosing loop or <code>switch</code> statement. Control is passed to the statement that follows the terminated statement.
===<code>goto</code>===
<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.
<lang csharp>while (conditionA) {
for (int i = 0; i < 10; i++) {
if (conditionB) goto NextSection;
DoSomething(i);
}
}
NextSection: DoOtherStuff();</lang>
 
=={{header|C++}}==
Line 402 ⟶ 464:
}
}</lang>
 
=={{header|C sharp}}==
===return===
terminates the function and returns control to the caller.
<lang csharp>int GetNumber() {
return 5;
}</lang>
===throw===
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.
<lang csharp>try {
if (someCondition) {
throw new Exception();
}
} catch (Exception ex) {
LogException(ex);
throw;
} finally {
cleanUp();
}</lang>
===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.
<lang csharp>public static void Main() {
foreach (int n in Numbers(i => i >= 2) {
Console.WriteLine("Got " + n);
}
}
 
IEnumerable<int> Numbers(Func<int, bool> predicate) {
for (int i = 0; ; i++) {
if (predicate(i)) yield break;
Console.WriteLine("Yielding " + i);
yield return i;
}
}
</lang>
{{out}}
<pre>
Yielding 0
Got 0
Yielding 1
Got 1</pre>
===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.
<lang csharp>async Task DoStuffAsync() {
DoSomething();
await someOtherTask;//returns control to caller if someOtherTask is not yet finished.
DoSomethingElse();
}
</lang>
===break and continue===
<code>continue</code> causes the closest enclosing loop to skip the current iteration and start the next iteration immediately.<br/>
<code>break</code> terminates the closest enclosing loop or <code>switch</code> statement. Control is passed to the statement that follows the terminated statement.
===<code>goto</code>===
<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.
<lang csharp>while (conditionA) {
for (int i = 0; i < 10; i++) {
if (conditionB) goto NextSection;
DoSomething(i);
}
}
NextSection: DoOtherStuff();</lang>
 
=={{header|COBOL}}==
Line 1,253 ⟶ 1,252:
end
</lang>
 
 
=={{header|Maxima}}==
Line 1,511 ⟶ 1,509:
# some code
goto FORK;</lang>
=={{header|Perl 6}}==
===Control exceptions===
Control flow is extensible in Perl 6; most abnormal control flow (including the standard loop and switch exits) is managed by throwing control exceptions that are caught by the code implementing the construct in question. Warnings are also handled via control exceptions, and turn into control flow if the dynamic context chooses not to resume after the warning. See [[http://perlcabal.org/syn/S04.html#Control_Exceptions S04/Control exceptions]] for more information.
===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 [[http://perlcabal.org/syn/S04.html#Phasers S04/Phasers]].
===goto===
<lang perl6>TOWN: goto TOWN;</lang>
Labels that have not been defined yet must be enclosed in quotes.
 
=={{header|Phix}}==
Line 1,778 ⟶ 1,768:
 
Remark: Pop11 does not perform "tail call optimization", one has to explicitly use chain.
 
=={{header|PureBasic}}==
===Goto===
Line 2,023 ⟶ 2,014:
...
1
 
 
=={{header|Racket}}==
Line 2,077 ⟶ 2,067:
Given that Racket has macros, and continuations, and a zillion other features, it is easy to implement
new control flow expressions, so any list will not be exhaustive.
 
=={{header|Raku}}==
(formerly Perl 6)
===Control exceptions===
Control flow is extensible in Perl 6; most abnormal control flow (including the standard loop and switch exits) is managed by throwing control exceptions that are caught by the code implementing the construct in question. Warnings are also handled via control exceptions, and turn into control flow if the dynamic context chooses not to resume after the warning. See [[http://perlcabal.org/syn/S04.html#Control_Exceptions S04/Control exceptions]] for more information.
===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 [[http://perlcabal.org/syn/S04.html#Phasers S04/Phasers]].
===goto===
<lang perl6>TOWN: goto TOWN;</lang>
Labels that have not been defined yet must be enclosed in quotes.
 
=={{header|REBOL}}==
10,333

edits