Flow-control structures: Difference between revisions

Added Oz.
(add JavaScript)
(Added Oz.)
Line 406:
* loop control with <code>'''break''' [label]</code> ([http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/break]) and <code>'''continue''' [label]</code> ([http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/continue])
* exceptions with <code>'''throw'''</code> ([http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/throw]) and <code>'''try ... catch ... finally ...'''</code> ([http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/try...catch])
 
=={{header|Oz}}==
Exception handling is documented in other tasks: [[Exceptions#Oz]], [[Exceptions Through Nested Calls#Oz]].
 
The <code>case</code> statement can be used for [[Pattern Matching]], but also like a switch statement in C:
<lang oz>case {OS.rand} mod 3
of 0 then {Foo}
[] 1 then {Bar}
[] 2 then {Buzz}
end</lang>
 
The Lisp-influenced [http://www.mozart-oz.org/home/doc/loop/index.html for-loop] is very powerful and convenient to use.
 
As a constraint programming language, Oz has a number of flow control structures which target logic programming. They are typically used to implement new constraint search engines. However, it is also possible to use them for general logic programming.
 
* <code>or</code>: speculatively executes a number of alternative conditions and blocks until at most one alternative remains valid. Then either fails or commits to the remaining alternative if there is one.
 
* <code>cond</code>: evaluates a number of conditions in parallel (or in undefined order) and commits to the first alternative that succeeds.
 
* <code>dis</code>: depreciated
 
* <code>choice</code>: creates a non-deterministic choice point. In other words, the statement provisionally chooses an alternatives. If the choice turns out to be wrong or if additional solutions to a puzzle are searched, another alternative is chosen.
 
As an example for <code>choice</code>, a simple, but stupid way to solve the equation 2*X=18. We assume that the solution is somewhere in the interval 8-10, but we do not quite know what exactly it is.
 
<lang oz>declare
proc {Stupid X}
choice
X = 8
{System.showInfo "choosing 8"}
[] X = 9
{System.showInfo "choosing 9"}
[] X = 10
{System.showInfo "choosing 10"}
end
2 * X = 18
end
in
{Show {SearchOne Stupid}}</lang>
 
Output:
<pre>
choosing 8
choosing 9
[9]
</pre>
 
=={{header|Perl}}==
Line 819 ⟶ 866:
<lang tcl>after 1000 {myroutine x}</lang>
 
which will call "<tt>myroutine</tt>" with parameter "<tt>x</tt>" 1000ms from 'now'; no matter what other code might be running at the time (i.e. "<tt>after</tt>"; schedules the execution, then returns and continues program flow with the following code).
 
The scheduled task can be removed from the scheduler for example with
Anonymous user