The if-statement performs a test on the expression in parenthesis, and executes the statements enclosed within braces if the expression is true. The expression must evaluate to a scalar-expression. If the expression evaluates to a vector or matrix a run-time error will result.
if ( expression )
{
statements
}
> if ( 1 ) { "TRUE" } TRUE > if ( 0 ) { "TRUE" }
An optional `else
' keyword is allowed to delineate statements
that will be executed if the expression tests false:
> if ( 0 ) { "TRUE" else "FALSE" } FALSE
The any
and all
functions are useful with if-statements. If we want to execute some statements, conditional
on the contents of a matrix:
> a=[1,2;3,0]; > if (!all (all (a))) { "a has a zero element" } a has a zero element