-
Notifications
You must be signed in to change notification settings - Fork 2
_else
Syntax
if ( expression ) statement ; bstatement ;
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