Conditional Statements and Expressions#

The conditional statement has the usual form if ... then ... else ... end if;. It has several variants. Within the statement, a special prompt will appear, indicating that the statement has yet to be closed. Conditional statements may be nested. The conditional expression, select ... else, is used for in-line conditionals.

The Simple Conditional Statement#

if Boolean expression then
    statements₁
else
    statements₂
end if;#
if Boolean expression then
    statements
end if;#

The standard conditional statement: the value of the Boolean expression is evaluated. If the result is true then the first block of statements is executed, while if the result is false the second block of statements is executed. If no action is desired in the latter case, the construction may be abbreviated to the second form above.

if Boolean expression₁ then
    statements₁
elif Boolean expression₂ then
    statements₂
else
    statements₃
end if;#

Since nested conditions occur frequently, elif provides a convenient abbreviation for else if, which also restricts the ‘level’:

if Boolean expression then
   statements₁
elif Boolean expression₂ then
   statements₂
else
   statements₃
end if;

is equivalent to

if Boolean expression₁ then
    statements₁
else
    if Boolean expression₂ then
       statements₂
    else
       statements₃
    end if;
end if;
Example: if (ex-6d1681)#
> m := Random(2, 10000);
> if IsPrime(m) then
>     m, "is prime";
> else
>     Factorization(m);
> end if;
[ <23, 1>, <37, 1> ]

Run in calculator

The Simple Conditional Expression#

Boolean expression select expr₁ else expr₂#

This is an expression, of which the value is that of expr\(_1\) or expr\(_2\), depending on whether Boolean expression is true or false.

Example: In Line Conditional (ex-bbc380)#

Using the select ... else construction, we wish to assign the sign of \(y\) to the variable \(s\).

> y := 11;
> s := (y gt 0) select 1 else -1;
> s;
1

Run in calculator

This is not quite right (when \(y = 0\)), but fortunately we can nest select ... else constructions:

> y := -3;
> s := (y gt 0) select 1 else (y eq 0 select 0 else -1);
> s;
-1
> y := 0;
> s := (y gt 0) select 1 else (y eq 0 select 0 else -1);
> s;
0

Run in calculator

The select ... else construction is particularly important in building sets and sequences, because it enables in-line if constructions. Here is a sequence containing the first 100 entries of the Fibonacci sequence:

>  f := [ i gt 2 select Self(i-1)+Self(i-2) else 1 : i in [1..100] ];

Run in calculator

The Case Statement#

case expr : when exprᵢ : statements end case#

The expression following case is evaluated. The statements following the first expression whose value equals this value are executed, and then the case statement has finished. If none of the values of the expressions equal the value of the case expression, then the statements following else are executed, in the first form. If no action is desired in the latter case, the construction may be abbreviated to the second form above.

Example: case (ex-62b01f)#
> x := 73;
> case Sign(x):
>    when 1:
>       x, "is positive";
>    when 0:
>       x, "is zero";
>    when -1:
>       x, "is negative";
> end case;
73 is positive

Run in calculator

The Case Expression#

case< expr | expr_left,1 : expr_right,1, ..., expr_left,n : expr_right,n, default : expr_def>#

This is the expression form of case. The expr is evaluated to the value \(v\). Then each of the left-hand expressions expr\(_{{\rm left},i}\) is evaluated until one is found whose value equals \(v\); if this happens the value of the corresponding right-hand expression expr\(_{{\rm right},i}\) is returned. If no left-hand expression with value \(v\) is found the value of the default expression expr\(_{\rm def}\) is returned.

The default case cannot be omitted, and must come last.