Flow-control structures: Difference between revisions

Content added Content deleted
(Replaced "ReadIOEffect" by "IOError" to make the program compile.)
m (→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics)
Line 1,511: Line 1,511:


=={{header|Phix}}==
=={{header|Phix}}==
{{libheader|Phix/basics}}
===goto===
===goto===
In 0.8.4+ Phix finally has a goto statement:
There is no hll goto statement in Phix. However in the very rare cases a goto is genuinely needed in hll code, the following
<!--<lang Phix>-->
construct(s) may be used:
<span style="color: #008080;">procedure</span> <span style="color: #000000;">p<span style="color: #0000FF;">(<span style="color: #0000FF;">)</span>
<span style="color: #008080;">goto</span> <span style="color: #0000FF;">:<span style="color: #000000;">but_print</span>
<span style="color: #7060A8;">puts<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #008000;">"This will not be printed...\n"<span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">:<span style="color: #0000FF;">:<span style="color: #000000;">but_print</span>
<span style="color: #7060A8;">puts<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #008000;">"...but this will\n"<span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">p<span style="color: #0000FF;">(<span style="color: #0000FF;">)
<!--</lang>-->
Imposing a self-policed rule that all jumps must be forward (or equivalently all backward, but never mixed) is recommended.

Phix imposes the following limitations on the use of goto statements:<br>
A goto statement must be in the same function as the label it is referring.<br>
The goto statement is not supported in top level code, outside of a routine definition.<br>
There are no computed, assigned, or multiple target forms of the goto statement.<br>
Goto may not be used and labels may not be defined anywhere inside a try/catch statement.<br>
Jumping over variable initialisation will, naturally, leave the variable unassigned.<br>
A goto can optionally refer to label or :label - they mean the same thing.<br>
(Technically the colon-less variant is shorthand for the formal with-colon label reference.)<br>
A label, which adheres to the usual identifier rules, is defined by preceding it with a double colon.

Note that a goto statement, or inline assembly (a goto statement is implemented using fragments of auto-generated inline assembly) will cause the compiler to abandon certain optimisation efforts, in particular type inferencing and constant propagation, which can result in a larger and slower program.

Previous versions had no hll goto statement, however the folowing work around was (and still is) available:
<lang Phix>#ilASM{ jmp :label }
<lang Phix>#ilASM{ jmp :label }
...
...
Line 1,537: Line 1,561:
local labels are declared with "::" but referenced with ":".
local labels are declared with "::" but referenced with ":".


&lt;it was claimed&gt;
Making "goto" somewhat more difficult to type in this manner ensures that
Making "goto" somewhat more difficult to type in this manner ensures that
it is far less likely to be abused, and discourages newbie programmers
it is far less likely to be abused, and discourages newbie programmers
from adopting it as a weapon of choice, as usually(/always) happens with
from adopting it as a weapon of choice, as usually(/always) happens with
a hll goto.
a hll goto.
&lt;/it was claimed&gt;


===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>for i=1 to 100 do
<!--<lang Phix>-->
<span style="color: #008080;">for</span> <span style="color: #000000;">i<span style="color: #0000FF;">=<span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">100</span> <span style="color: #008080;">do</span>
if a[i]=x then
<span style="color: #008080;">if</span> <span style="color: #000000;">a<span style="color: #0000FF;">[<span style="color: #000000;">i<span style="color: #0000FF;">]<span style="color: #0000FF;">=<span style="color: #000000;">x</span> <span style="color: #008080;">then</span>
location = i
<span style="color: #000000;">location</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
exit
<span style="color: #008080;">exit</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for
<!--</lang>-->


===continue===
===continue===
causes the next interation of the immediately surrounding loop to begin immediately, with any condition evaluated normally, eg:
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>for i=1 to 100 do
<!--<lang Phix>-->
<span style="color: #008080;">for</span> <span style="color: #000000;">i<span style="color: #0000FF;">=<span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">100</span> <span style="color: #008080;">do</span>
if a[i]=0 then continue end if
<span style="color: #008080;">if</span> <span style="color: #000000;">a<span style="color: #0000FF;">[<span style="color: #000000;">i<span style="color: #0000FF;">]<span style="color: #0000FF;">=<span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">continue</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
...
<span style="color: #0000FF;">...</span>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i<span style="color: #0000FF;">=<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;">if</span> <span style="color: #000000;">a<span style="color: #0000FF;">[<span style="color: #000000;">i<span style="color: #0000FF;">]<span style="color: #0000FF;">!=<span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">...</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for
<!--</lang>-->


===return===
===return===
exits the current routine. Needs a value to return if used inside a function or type.
Exits the current routine. Needs a value to return if used inside a function or type.


===break===
===break===
terminate a switch statement. fallthrough is the opposite, overriding an implicit break between cases.
Terminate a switch statement. fallthrough is the opposite, overriding an implicit break between cases.


===abort===
===abort, crash, throw===
terminate the entire application immediately.
Terminate the entire application immediately, unless caught by a containing try/catch statement.<br>
Technically it is possible to use these to effect control flow, albeit in a grossly inefficient manner.


===tasks and threads===
===tasks and threads===