Conditional structures/ALGOL 68
IF ~ THEN ~ ELSE ~ FI clause
main: (
INT input := 2;
IF 3 = input THEN
# Do something #
FI;
IF 3 = input THEN
# Do something #
ELSE
# Do something ELSE #
FI
)
One line predicates do not any braces:
IF cond THEN
~
FI;
IF cond THEN
~
ELSE
~
FI;
And these may be mixed:
IF cond THEN
~
IF cond THEN
# multiple expressions #
ELSE
~
FI;
~
FI
Or merged:
IF cond1 THEN
~
ELIF cond2 THEN
# multiple expressions #
ELSE
~
FI # etc #
Short-circuit conditionals
The short-circuit evaluation of the ANDIF and ORELSE boolean operators are also often used for control flow.
IF node /= NULL ANDIF next OF node /= NULL ANDIF guarded_test(node) THEN
next OF node := next OF next OF node
FI
Note that these are not part of the standard library and had to be defined by the user.
Neither the assignment nor guarded_test() will be called IF the previous two conditions aren't met. Other languages such as Pascal don't make that guarantee, and they certainly could not be user defined.
CASE ~ IN ~ OUT ~ ESAC clause
main: (
INT input := 42;
CASE input IN
~ # Do something, because input = 1 #
,
~ # Do something, because input = 2 #
,
~ # Do something, because input = 3 #
,
# fall through to the next statement IF there is no "OUT" #
OUT
~ # Do something ELSE. #
CASE
)
The "CASE ~ IN ~ OUT ~ ESAC" is directly interchangeable with the "( ~ | ~ | ~ )", hence the above may be written as follows:
main: (
INT input := 42;
( input |
~ # Do something, because input = 1 #
,
~ # Do something, because input = 2 #
,
~ # Do something, because input = 3 #
# fall through to the next statement IF there is no "OUT" #
|
~ # Do something ELSE. #
)
)
Case statements may also be merged:
main:(
INT a1, a2, b1, b2, c99;
CASE 99 IN
a1,
a2
OUSE 99 IN
b1,
b2
OUT
c99
ESAC := 333
)
In this case a1, a2, b1 and b2 remain undefined, but c99 ends up being half-evil.
Finally labels, and an EXIT "completion symbol" can be used to engineer the more familiar "switch" statement.
STRING food := (
CASE 666 IN a,b,c OUT else ESAC;
a:
"Apple"
EXIT
b:
"Banana"
EXIT
c:
"Carrot"
EXIT
else:
"Baked beans"
);
print((food))
The EXIT "completion symbol" here serves to yield the found value.
Ternary ( ~ | ~ | ~ )
Conditionals in C can also be done with the ternary operator, ?:. ALGOL 68 has the "brief" equivalents ( ~ | ~ | ~ )
main : (
INT input := 2;
INT output := (input = 2 | 42 | 4711); # sets output to 42 #
INT output2 := (input = 3 | 42 | 4711); # sets output2 to 4711 #
PROC do_something = INT: random;
PROC do_something_else = INT: random;
input := (TRUE | do_something | do_something_else); # only calls do_something() #
)
The "( ~ | ~ | ~ )" is directly interchangeable with the "IF ~ THEN ~ ELSE ~ FI" syntax. Hence the above could be written:
main : (
INT input := 2;
INT output := IF input = 2 THEN 42 ELSE 4711 FI; # sets output to 42 #
INT output2 := IF input = 3 THEN 42 ELSE 4711 FI; # sets output2 to 4711 #
PROC do_something = INT: random;
PROC do_something_else = INT: random;
input := IF TRUE THEN do_something ELSE do_something_else FI; # only calls do_something() #
)
A more complex example created by combining the above:
main: (
INT x,y,z;
CASE 2 IN x,y OUT z ESAC := IF 1+2=2 THEN 333 ELSE 666 FI
)
In the result y becomes fully-evil, with x and z remaining undefined.