| Blocks | categories Lists, Sets and Instructions Conditionals |
Loops |
if statements have the usual syntax (if <test> x else y) with implicit nestings (else if). The <test> expression is evaluated and the instruction x is evaluated if the value is different from false, nil or {}. Otherwise, the instruction y is evaluated, or the default value false is returned if no else part was provided.
| if (x = 1) x := f(x,y) else if (x > 1) x := g(x,y) else (x := 3, f(x,y)) if (let y := 3 in x + y > 4 / x) print(x) |
case is a set-based switch instruction: CLAIRE tests the branching sets one after another, executes the instruction associated with the first set that contains the object and exits the case instruction without any further testing. Hence, the default branch is associated with the set any. As for an if, the returned value is nil if no branch of the case is relevant :
| case x ({1} x + 1, {2,3} x + 2, any x + 3) case x (integer (x := 3, print(x)), any error("~I is no good\n",x)) |
Starting with CLAIRE 3.3, only boolean expressions should be used in the <test> expression of a conditional statement. The implicit coercion of any expression into a Boolean is still supported, but should not be used any longer. The compiler will issue a warning if a non-boolean expression is used in an If.