Conditional Statements & Expressions
else if chains are fine because the statement after else may itself be conditional.
11.1 if-then and if-then-else
'if' x < 0 'then' x := -x;
'if' score >= 50 'then'
outstring(1, "pass\n")
'else'
outstring(1, "fail\n");11.2 Chains
else if chains are fine because the statement after else may itself be conditional.
'if' n < 0 'then' outstring(1, "negative")
'else' 'if' n = 0 'then' outstring(1, "zero")
'else' outstring(1, "positive");11.3 No dangling else
After then you may not directly write another if statement (the syntax demands an *unconditional statement*). Use begin ... end:
This rule removes the dangling-else ambiguity that later plagued Pascal and C (which resolve it by convention: else binds to the nearest if).
'if' a > 0 'then'
'begin'
'if' b > 0 'then' outstring(1, "both positive")
'else' outstring(1, "a positive, b not")
'end';11.4 Conditional statements with for
if B then for ... is allowed (a for statement after then), but if you want an else part you must wrap the for in begin ... end.
11.5 Conditional expressions everywhere
Arithmetic, Boolean and designational expressions can all be conditional:
y := 'if' x >= 0 'then' sqrt(x) 'else' 0;
b := 'if' n > 0 'then' a[n] > 0 'else' 'false';
'goto' 'if' error 'then' fail 'else' continue;
outreal(1, 'if' p 'then' u 'else' v);