Skip to content
abinition edited this page Oct 22, 2014 · 7 revisions

#if

###Conditional If statement.

Syntax

if ( expression ) statement ;
if ( expression ) statement ; else statement ;

Description

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 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.

Examples

if ( 1 ) puts "TRUE" ;
if ( 0 ) puts "TRUE" ; else puts "FALSE" ;

Related Links

else

Clone this wiki locally