Flow-control structures: Difference between revisions
m
→{{header|Phix}}: added syntax colouring the hard way, phix/basics
(Replaced "ReadIOEffect" by "IOError" to make the program compile.) |
m (→{{header|Phix}}: added syntax colouring the hard way, phix/basics) |
||
Line 1,511:
=={{header|Phix}}==
{{libheader|Phix/basics}}
===goto===
In 0.8.4+ Phix finally has a goto statement:
<!--<lang Phix>-->
<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;">)
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 }
...
Line 1,537 ⟶ 1,561:
local labels are declared with "::" but referenced with ":".
<it was claimed>
Making "goto" somewhat more difficult to type in this manner ensures that
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
a hll goto.
</it was claimed>
===exit===
causes immediate termination of the immediately surrounding for or while loop, with control passing to the first statement after the loop, eg:
<!--<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>
<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>
<span style="color: #000000;">location</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
<span style="color: #008080;">exit</span>
<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
===continue===
<!--<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>
<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===
===break===
===abort, crash, throw===
Technically it is possible to use these to effect control flow, albeit in a grossly inefficient manner.
===tasks and threads===
|