Flow-control structures: Difference between revisions

(added D information)
Line 19:
end loop;
end loop;</lang>
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Standard - except the Refinement Preprocessor is an extension}}
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} <!-- with version's of a68g up to mk18 the GSL library has problems with VEC and MAT with LWB 0 , and transposes of non square MAT, hence we include source code of replacement OPerators here -->
<!-- {{does not work with|ELLA ALGOL 68|Any (with appropriate job cards AND formatted transput statements removed) - tested with release 1.8.8d.fc9.i386 - ELLA has no FORMATted transput}} -->
===One common use of a laberl in '''ALGOL 68''' is to break out of nested loops.===
<lang algol>(
FOR j TO 1000 DO
FOR i TO j-1 DO
IF random > 0.999 THEN
printf(($"Exited when: i="g(0)", j="g(0)l$,i,j));
done
FI
# etc. #
OD
OD;
done: EMPTY
);</lang>
===Multi way jump using labels and '''EXIT''' to return result ===
<lang algol>STRING medal = (
[]PROC VOID award = (gold,silver,bronze);
 
award[ 1 + ENTIER (random*3)];
 
gold: "Gold" EXIT
silver: "Silver" EXIT
bronze: "Bronze"
 
);
 
print(("Medal awarded: ",medal, new line));</lang>
===Another use is to implement finite state machines ===
<lang algol>STRING final state = (
INT condition;
PROC do something = VOID: condition := 1 + ENTIER (3 * random);
state1:
do something;
CASE condition IN
state 1, state 2
OUT
state n
ESAC
EXIT
state 2:
"State Two"
EXIT
state n:
"State N"
);
print(("Final state: ",final state, new line));</lang>
===ALGOL 68G implements a Refinement Preprocessor to aid with top down code development ===
<lang algol># example from: http://www.xs4all.nl/~jmvdveer/algol.html - GPL #
determine first generation;
WHILE can represent next generation
DO calculate next generation;
print next generation
OD.
 
determine first generation:
INT previous := 1, current := 3.
 
can represent next generation:
current <= max int - previous.
 
calculate next generation:
INT new = current + previous;
previous := current;
current := new.
 
print next generation:
printf (($lz","3z","3z","2z-d$, current,
$xz","3z","3z","2z-d$, previous,
$xd.n(real width - 1)d$, current / previous)).</lang>
Sample output:
<pre>
Exited when: i=13, j=53
Medal awarded: Gold
Final state: State Two
 
4 3 1.33333333333333
7 4 1.75000000000000
11 7 1.57142857142857
etc...
</pre>
 
=={{header|C}}==