if: expr_block x block -> ()
if: expr_block x block x block -> ()
if: expr_block x block x expr_block x block x ... -> ()
if: expr_block x block x expr_block x block x ... x block -> ()
if(condBlock, ifBlock, elseBlock)
if(condBlock_1, ifBlock_1, condBlock_2, ifBlock_2, ...)
if(condBlock_1, ifBlock_1, condBlock_2, ifBlock_2, ..., elseBlock)
The function if implements conditional execution of blocks. It first evaluates parameter condBlock which should return a result of type boolean. If this result is TRUE, parameter ifBlock is executed. If there are multiple pairs of condBlocks and ifBlocks, the resp. condBlocks are successively evaluated from left to right until one returns TRUE; then the associated ifBlock is evaluated and the if-statement is terminated. Optionally, an elseBlock can be specified which is executed when all preceeding condBlocks evaluated to FALSE. For the condBlock parameters, any block can be used which is guaranteed to return a value of type boolean; therefore its execution may cause side-effects. However, to increase readability of SALIERI code, this should be avoided.
if-statements can be used to condition on data; in this case,an elseBlock has to be specified and all ifBlocks as well as the elseBlock should be guaranteed to return values of the same type. This variant of if should only be used by exerienced programmers.
Usually, condBlock will contain a boolean expression (condition). However, it has to be specified as a block (i.e., be enclosed in block-quotes), since if does not accept a boolean value here.
To improve performance and readability, multiple conditional statements should be realised using single if-statement with multiple conditions rather than as a sequence of nested or consecutive simple if's.
Error conditions arise, when condBlock returns no value (Missing Parameter) or a non-boolean result (Type mismatch).
> if(`TRUE', `write("hello")')
> if(`rnd() < 0.5', `write("less")', `write("greater or equal")')
> if(`2*3 < 5', `write("weird!")', `2*3 < 6', `write("peculiar!")', `2*3 < 7', `write("so what?")', `2*3 < 9', `write("really?")', `write("oops!")')
> seq := if(`rnd() < 0.5', `[c d e]', `[a c]')
[c1/4 d1/4 e1/4]
> if(0!=1, `write("hello")')
Error in command-line:
(occures while executing function "if")
> if(`a := 1', `write("hello")')
Error in command-line:
(occured while executing function "if")
See also: while, loop, loopn.