Skip to content

decision

bergsma edited this page Jul 31, 2014 · 1 revision

Decision Statements

If Statement

A value can be tested by an if statement:

if ( expression ) statement
**if** ( expression ) statement **else** statement

_

In each case the expression is evaluated, and if it is not zero, the first statement is executed. Otherwise, the second statement (if it is specified) is executed.

In a series of if-else clauses, the else matches the most recent if.

The comparison operators: `== != < <= > >=



return the _boolean_ _true_ (1) if the comparison is true, and _false_ (0) otherwise. This implies that the expression in an _if_
statement (or any other conditional statement) can be any expression with 
predictable results (non-zero or zero). 

The operators: `&&  ||  !

are most commonly used in if statements. The operators && and || will not evaluate their second argument unless doing so is necessary. For example:

if ( count( v ) > 1 && v[1] )        /* ... */

_

first tests if v contains more than one element (using the count function). It tests that v[1] is non-zero only if v has more than one element.

Some if statements are more appropriately expressed as conditional statements. For example:

if ( x > y )
   max = x;
else
   max = y;

_

would be better expressed as:

max = ( x > y ) ? x : y;

_

An empty expression in the if statement generates the unrecoverable error code %EXPRESSION, which can be trapped by a handler declared by the on_error statement.

Clone this wiki locally