Jump to content

Flow-control structures: Difference between revisions

(→‎{{header|COBOL}}: Added ALTER, but only because it exists, not to encourage its use)
Line 1,273:
<lang perl6>TOWN: goto TOWN;</lang>
Labels that have not been defined yet must be enclosed in quotes.
 
=={{header|Phix}}==
===goto===
There is no hll goto statement in Phix. However in the very rare cases a goto is genuinely needed in hll code, the following
construct(s) may be used:
<lang Phix>#ilASM{ jmp :label }
...
#ilASM{ ::label }</lang>
In top level code, label scope is restricted to a single ilASM construct,
but within a routine, the scope is across all the ilasm in that routine.
 
There is quite deliberately no support for jumping from the middle of one
routine into another: without a frame, then quite simply parameters and
local variables have not been allocated and cannot be used/referenced.
 
It is also possible to declare global labels, which are superficially similar:
<lang Phix>#ilASM{ call :%label }
...
#ilASM{ jmp :skip
:%label
ret
::skip }</lang>
Global labels cannot be declared inside a routine, and as shown (almost always)
require a skip construct. It is up to the programmer to ensure global labels
are unique across the entire application.
Note that global labels are both declared and referenced with ":%", whereas
local labels are declared with "::" but referenced with ":".
 
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.
 
===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>for i=1 to 100 do
if a[i]=x then
location = i
exit
end if
end for</lang>
 
===continue===
causes the next interation of the immediately surrounding loop to begin immediately, with any condition evaluated normally, eg:
<lang Phix>for i=1 to 100 do
if a[i]=0 then continue end if
...
end for</lang>
 
===return===
exits the current routine. Needs a value to return if used inside a function or type.
 
===break===
terminate a switch statement. fallthrough is the opposite, overriding an implicit break between cases.
 
===abort===
terminate the entire application immediately.
 
===tasks and threads===
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.
 
=={{header|PHP}}==
7,820

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.