Jump to content

Conditional structures/ALGOL 68

From Rosetta Code
Conditional structures/ALGOL 68 is part of Conditional Structures. You may find other members of Conditional Structures at Category:Conditional Structures.

IF ~ THEN ~ ELSE ~ FI clause

<lang algol68>main: (

 INT input := 2;
 IF  3 = input THEN
   # Do something #
 FI;
 IF  3 = input THEN
   # Do something #
 ELSE 
   # Do something ELSE #
 FI

)</lang> One line predicates do not any braces: <lang algol68>IF cond THEN

 ~

FI;

IF cond THEN

 ~

ELSE

 ~

FI;</lang> And these may be mixed: <lang algol68>IF cond THEN

 ~
 IF cond THEN
   # multiple expressions #
 ELSE
   ~
 FI;
 ~

FI</lang> Or merged: <lang algol68>IF cond1 THEN

 ~

ELIF cond2 THEN

 # multiple expressions #

ELSE

 ~

FI # etc #</lang>

Short-circuit conditionals

The short-circuit evaluation of the ANDIF and ORELSE boolean operators are also often used for control flow. <lang algol68>IF node /= NULL ANDIF next OF node /= NULL ANDIF guarded_test(node) THEN

  next OF node := next OF next OF node

FI</lang> 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

<lang algol68>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

)</lang> The "CASE ~ IN ~ OUT ~ ESAC" is directly interchangeable with the "( ~ | ~ | ~ )", hence the above may be written as follows: <lang algol68>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. #
 )

)</lang> Case statements may also be merged: <lang algol68>main:(

 INT a1, a2, b1, b2, c99;
 CASE 99 IN
   a1,
   a2
 OUSE 99 IN
   b1,
   b2
 OUT
   c99
 ESAC := 333

)</lang> 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. <lang algol68>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))</lang>

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 ( ~ | ~ | ~ ) <lang algol68>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() #

)</lang> The "( ~ | ~ | ~ )" is directly interchangeable with the "IF ~ THEN ~ ELSE ~ FI" syntax. Hence the above could be written: <lang algol68>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() #

)</lang> A more complex example created by combining the above: <lang algol68>main: (

 INT x,y,z;
 CASE 2 IN x,y OUT z ESAC := IF 1+2=2 THEN 333 ELSE 666 FI

)</lang> In the result y becomes fully-evil, with x and z remaining undefined.

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