Flow-control structures: Difference between revisions

m
→‎{{header|Ada}}: added 'return'
m (→‎{{header|Ada}}: added 'return')
 
(5 intermediate revisions by 4 users not shown)
Line 1:
{{Task|Control Structures}}
{{Control Structures}}
[[Category:Flow control]]
 
;Task:
Line 136 ⟶ 137:
 
===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:
<syntaxhighlight lang="ada">Outer:
loop
-- do something else
loop
--if doFinished somethingthen
loop
-- do something else
exit Outer; -- exits both the inner and outer loops
end loopif;
-- do something else
end loop;</syntaxhighlight>
end loop;
end loop Outer;</syntaxhighlight>
or, more idiomatically,
<syntaxhighlight lang="ada">Outer:
loop
-- do something
loop
exit Outer when Finished;
-- do something else
end loop;
end loop Outer;</syntaxhighlight>
 
===return===
A procedure can be exited early, if there’s no more to be done.
<syntaxhighlight lang="ada">procedure Foo is
begin
-- do something
if Nothing_More_To_Do then
return;
end if;
-- do more
end loopFoo;</syntaxhighlight>
 
===asynchronous transfer of control===
A sequence of operation can be aborted with an asynchronous transfer of control to an alternative:
Line 786 ⟶ 810:
 
[[Category:E examples needing attention]] <!-- Needs runnable examples, description of escape-catch, and maybe links to other flow control pages -->
 
=={{header|EasyLang}}==
 
With '''break <n>''' you can break out of a nested loop
 
<syntaxhighlight>
sum = 80036
for i = 0 to 50
for j = 0 to 50
if i * i + j * j * j = sum
print i & "² + " & j & "³ = " & sum
break 2
.
.
.
</syntaxhighlight>
{{out}}
<pre>
23² + 43³ = 80036
</pre>
 
=={{header|Erlang}}==
Line 3,184 ⟶ 3,228:
 
The following code demonstrates each of the above apart from '''Fiber.suspend''' which simply exits a CLI script.
<syntaxhighlight lang="ecmascriptwren">var func = Fn.new { |n|
var i = 1
while (true) {
5

edits