Flow-control structures: Difference between revisions
m
→{{header|Ada}}: added 'return'
(Added Quackery.) |
m (→{{header|Ada}}: added 'return') |
||
(6 intermediate revisions by 5 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
loop
loop▼
▲ -- do something else
exit Outer; -- exits both the inner and outer loops
end
-- do something else
end loop;</syntaxhighlight>▼
end loop;
end loop Outer;</syntaxhighlight>
or, more idiomatically,
<syntaxhighlight lang="ada">Outer:
loop
-- do something
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
===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 2,274 ⟶ 2,318:
=={{header|Quackery}}==
A Quackery program is a dynamic array ('''''nest''''') of numbers (bigints) operators (opcodes or primitives) and nests (named or explicit). It is evaluated by a depth first traversal of the structure, placing numbers on a data stack, and keeping track of the evaluation with a return stack. Flow control is achieved with meta-control flow operators, which modify the return stack during evaluation. The naming convention for meta-control flow operators is to wrap them in reversed brackets. They are <code>]again[ ]done[ ]if[ ]iff[ ]else[ ]'[ ]do[ ]this[</code> and <code>]bailby[</code>.
The first five, <code>]done[ ]again[ ]if[ ]iff[ ]else[</code>, are used to create a mix and match set of control flow words.
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="
var i = 1
while (true) {
|