if bool-expr1 then statements1
{ elif bool-expr2 then statements2 }
[ else statements3 ]
fi;
The if statement allows one to execute statements depending on the
value of some boolean expression. The execution is done as follows.
First the expression bool-expr1 following the if is evaluated. If it
evaluates to true the statement sequence statements1 after the first
then is executed, and the execution of the if statement is complete.
Otherwise the expressions bool-expr2 following the elif are evaluated
in turn. There may be any number of elif parts, possibly none at all.
As soon as an expression evaluates to true the corresponding statement
sequence statements2 is executed and execution of the if statement is
complete.
If the if expression and all, if any, elif expressions evaluate to
false and there is an else part, which is optional, its statement
sequence statements3 is executed and the execution of the if
statement is complete. If there is no else part the if statement is
complete without executing any statement sequence.
Since the if statement is terminated by the fi keyword there is no
question where an else part belongs, i.e., GAP has no dangling else.
In if expr1 then if expr2 then stats1 else stats2 fi; fi;
the else part belongs to the second if statement, whereas in
if expr1 then if expr2 then stats1 fi; else stats2 fi;
the else part belongs to the first if statement.
Since an if statement is not an expression it is not possible to write
abs := if x > 0 then x; else -x; fi;
which would, even if legal syntax, be meaningless, since the if
statement does not produce a value that could be assigned to abs.
If one expression evaluates neither to true nor to false an error is
signalled and a break loop (see Break Loops) is entered. As usual you
can leave the break loop with quit;. If you enter return true;,
execution of the if statement continues as if the expression whose
evaluation failed had evaluated to true. Likewise, if you enter
return false;, execution of the if statement continues as if the
expression whose evaluation failed had evaluated to false.
gap> i := 10;;
gap> if 0 < i then
> s := 1;
> elif i < 0 then
> s := -1;
> else
> s := 0;
> fi;
gap> s;
1 # the sign of i
GAP 3.4.4