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;">procedurewithout</span> <span style="color: #000000008080;">pjs</span> <span style="color: #0000FF000080;font-style:italic;">-- (<spanno style="color:goto #0000FF;">in JavaScript)</span>
<span style="color: #008080;">gotoprocedure</span> <span style="color: #0000FF000000;">:p</span><span style="color: #0000000000FF;">but_print()</span>
<span style="color: #7060A8;">putsgoto</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: #0000FF000000;">)but_print</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"This will not be printed...\n"</span><span style="color: #0000000000FF;">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: #0000FF000000;">)but_print</span>
<span style="color: #0080807060A8;">forputs</span> <span style="color: #0000000000FF;">i(</span style="color: #0000FF;">=<span style="color: #000000;">1</span> <span style="color: #0080800000FF;">to,</span> <span style="color: #000000008000;">100"...but this will\n"</span> <span style="color: #0080800000FF;">do)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">p</span><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.
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{without jmp :label }js
#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{without call :%label }js
#ilASM{ call :%label }
...
#ilASM{ jmp :skip
Line 1,649 ⟶ 1,652:
a hll goto.
&lt;/it was claimed&gt;
 
===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;">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;">100with</span> <span style="color: #008080;">dojavascript_semantics</span>
<span style="color: #008080;">iffor</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: #008080000000;">then1</span> <span style="color: #008080;">continueto</span> <span style="color: #008080000000;">end100</span> <span style="color: #008080;">ifdo</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;">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 style="color: #0000FF;">]<span style="color: #0000FF;">]!=</span><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</span>
<!--</lang>-->
 
===returnexit===
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.
<!--</lang Phix>(phixonline)-->
<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 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</span>
<!--</lang Phix>-->
 
===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.
 
===exitreturn===
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, - however when one thread is stalled,
perhaps waiting for a network response, the other threads are unaffected.
 
7,803

edits