Flow-control structures: Difference between revisions

Content added Content deleted
m (→‎Exceptions: Added note on usage)
m (Speling/grammar/aesthetics)
Line 1: Line 1:
{{Task}}
{{Task}}
{{Control Structures}}
{{Control Structures}}
In this task, we document common flow-control structures. One common example of a flow-control structure is the <code>goto</code> construct. Note that [[Conditional Structures]] and [[Loop Structures]] have their own articles.


In this task, we document common flow-control structures. One common example of a flow-control structure is the <code>goto</code> construct. Note that [[Conditional Structures]] and [[Loop Structures]] have their own articles.


=={{header|Ada}}==
=={{header|Ada}}==
Line 204: Line 202:
===goto===
===goto===


Goto is typically looked down upon by most perl programmers
Goto is typically looked down upon by most Perl programmers


FORK:
FORK:
Line 228: Line 226:
===goto===
===goto===


goto l transfers control to the label l. goto may be used to exit from
goto l transfers control to the label l. goto may be used to exit from
nested loops:
nested loops:


Line 263: Line 261:
</pre>
</pre>


Pop11 goto is a nonlocal one, so "jump out" from a chain of procedure
Pop11 goto is a nonlocal one, so "jump out" from a chain of procedure calls:
calls:


<pre>
<pre>
Line 279: Line 276:
</pre>
</pre>


This is useful to exit early from succesful recursive search, and for
This is useful to exit early from successful recursive search, and for exception handling.
exception handling.


===go_on===
===go_on===
Line 290: Line 286:
</pre>
</pre>


If expression has value K the above will jump to label labK, if
If expression has value K the above will jump to label labK, if expression is not an integer, or if it outside range from 1 to N, then control passes to label elselab. The else part may be omitted (then out of range values of expression cause an exception).
expression is not an ingeger, or if it ouside range from 1 to N,
then control passes to label elselab. The else part may be
omitted (then out of range values of expression cause an exception).


There is a more structured variant of go_on:
There is a more structured variant of go_on:
Line 303: Line 296:
endgo_on;
endgo_on;


where lab is a prefix choosen by the user.
where lab is a prefix chosen by the user.


===return===
===return===


return ends execution of current function. In simplest form
return ends execution of current function. In simplest form
it is just:
it is just:


Line 322: Line 315:
===chain===
===chain===


chain has effect of "tail call" but is not nececcarly in tail position
chain has effect of "tail call" but is not necessarily in tail position. More precisely inside proc1.
More precisely inside proc1


<pre>
<pre>
Line 329: Line 321:
</pre>
</pre>


finishes execution of proc1 and transfers control to
finishes execution of proc1 and transfers control to the proc2 passing it x1, x2, and x3 as arguments. On return from proc2 control passes to caller of proc1.
the proc2 passing it x1, x2, and x3 as arguments.
On return from proc2 control passes to caller of proc1.


Remark: Pop11 does not perform "tail call optimization", one has to
Remark: Pop11 does not perform "tail call optimization", one has to explicitly use chain.
explicitely use chain.


=={{header|Python}}==
=={{header|Python}}==
Line 466: Line 455:
===The "with" statement===
===The "with" statement===
'''Interpreter:''' [[Python]] 2.6
'''Interpreter:''' [[Python]] 2.6
[[Category:Python]]
See [[http://www.python.org/peps/pep-0343.html PEP 0343, The "with" statement]]
See [[http://www.python.org/peps/pep-0343.html PEP 0343, The "with" statement]]
>>> with open("some_file"): # file ``some_file`` is closed after ``with`` block in any case whether an exception is raised or not
>>> with open("some_file"): # file ``some_file`` is closed after ``with`` block in any case whether an exception is raised or not
Line 472: Line 460:
===Yield expressions===
===Yield expressions===
'''Interpreter:''' [[Python]] 2.5
'''Interpreter:''' [[Python]] 2.5
[[Category:Python]]
See [[http://www.python.org/peps/pep-0342.html PEP 0342, Coroutines via Enhanced Generators]]
See [[http://www.python.org/peps/pep-0342.html PEP 0342, Coroutines via Enhanced Generators]]
>>> value = 1
>>> value = 1