Flow-control structures: Difference between revisions
m
→{{header|Phix}}: added js compatibility tags, reordered for clarity
m (→{{header|Z80 Assembly}}: corrected an opening </lang) |
m (→{{header|Phix}}: added js compatibility tags, reordered for clarity) |
||
Line 1,597:
In 0.8.4+ Phix finally has a goto statement:
<!--<lang Phix>-->
<span style="color: #008080;">
<span style="color: #7060A8;">
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">p</span><span style="color: #0000FF;">()</span
<!--</lang>-->
Imposing a self-policed rule that all jumps must be forward (or equivalently all backward, but never mixed) is recommended.
Line 1,620 ⟶ 1,621:
Previous versions had no hll goto statement, however the folowing work around was (and still is) available:
<lang Phix>
#ilASM{ jmp :label }
...
#ilASM{ ::label }</lang>
Line 1,631 ⟶ 1,633:
It is also possible to declare global labels, which are superficially similar:
<lang Phix>
#ilASM{ call :%label }
...
#ilASM{ jmp :skip
Line 1,649 ⟶ 1,652:
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>▼
<span style="color: #008080;">end</span> <span style="color: #008080;">for▼
<!--</lang>-->▼
===continue===
Personally I must agree with Douglas Crockford who says "I have never seen a piece of code that was not improved by refactoring it to remove the continue statement".<br>
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>(phixonline)-->
<span style="color: #008080;">
<span style="color: #008080;">if</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</span><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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><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><span style="color: #0000FF;">[</span><span style="color: #000000;">i</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</span>
<!--</lang>-->
===
▲causes immediate termination of the immediately surrounding for or while loop, with control passing to the first statement after the loop, eg:
Exits the current routine. Needs a value to return if used inside a function or type.▼
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><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><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</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>
▲ <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
===break===
Terminate a switch statement. fallthrough is the opposite, overriding an implicit break between cases.<br>
Note that JavaScript uses break for both switch and loop constructs, therefore pwa/p2js imposes additional rules to ensure compatibility, ie sufficiently nested/anything JavaScript can do is fine, but while desktop/Phix allows a loop to directly exit a switch and vice versa, the transpiler terminates in error when it detects any such attempts.
▲Exits the current routine. Needs a value to return if used inside a function or type.
===abort, crash, throw===
Line 1,689 ⟶ 1,696:
Phix supports both multitasking and multithreading. In multitasking, at most one task is currently running, so no locking is
required, and the application explicitly invokes task_yield to indicate when it is safe to switch between tasks. Multithreading
is potentially much trickier, everything that could be accessed concurrently must be locked
perhaps waiting for a network response, the other threads are unaffected.
|