Programmer Guide > Statement Types > IF Statement
  

IF Statement
The basic forms of the IF statement are:
IF expression THEN statement
IF expression THEN statement ELSE statement
The IF statement is used to execute conditionally a statement or a block of statements.
The expression after the IF is called the condition of the IF statement. This expression (or condition) is evaluated, and if true, the statement following the THEN is executed. If the expression evaluates to a false value the statement following the ELSE clause is executed. Control passes immediately to the next statement if the condition is false and the ELSE clause is not present.
Examples of the IF statement include:
IF A NE 2 THEN PRINT, 'A IS NOT TWO'
IF A EQ 1 THEN PRINT, 'A IS ONE' ELSE $
PRINT, 'A IS NOT ONE'
The first example contains no ELSE clause. If the value of A is not equal to 2, A IS NOT TWO is printed. If A is equal to 2, the THEN clause is ignored, nothing is printed, and execution resumes at the next statement. In the second example above, the condition of the IF statement is (A EQ 1). If the value of A is equal to 1, A IS ONE is printed, otherwise NOT ONE is printed.
Definition of True in an IF Statement
The condition of the IF statement may be any scalar expression. The definition of true and false for the different data types is as follows:
*BYTE, INT, INT32, and LONG—Odd values are true, even values are false.
*Floating-point, Double-precision floating-point and Complex—Nonzero values are true, zero values are false. The imaginary part of complex floating numbers is ignored.
*String—Any string with a non-zero length is true, null strings are false.
In the following example, the logical expression is a conjunction of two relational expressions.
IF (LON GT -40) AND (LON LE -20) THEN . . .
If both conditions—LON being larger than –40 and less than or equal to –20—are true then the statement following the THEN will be executed.
The THEN and ELSE clauses may also be in the form of a block (or group of statements) with the delimiters BEGIN and END. (See "Blocks of Statements".) To ensure proper nesting of blocks, you may use ENDIF to terminate the block, instead of using the generic END.
Below is the structure for a commonly used IF statement.
IF (expression) THEN BEGIN
. . .
. . .
. . .
ENDIF ELSE statement
For example:
PRO test
x=RANDOMN(seed)
PRINT,x
IF (x lt 1.) THEN BEGIN
PRINT,'x < 1'
ENDIF ELSE PRINT,'x >= 1'
END
Adding a block of statements to the ELSE clause would be done using the following format:
IF (expression) THEN BEGIN
. . .
. . .
. . .
ENDIF ELSE BEGIN
. . .
. . .
. . .
ENDELSE
Lastly, if you want to build a more complex IF Statement you can use the following.
IF (expression) THEN BEGIN
. . .
. . .
. . .
ENDIF ELSE IF (expression) THEN BEGIN
. . .
. . .
. . .
ENDIF ELSE BEGIN
. . . 
. . . 
. . . 
ENDELSE ;End of else clause
IF statements may be nested in the following manner:
IF P1 THEN S1 ELSE $
IF P2 THEN S2 ELSE $
. . .
. . .
. . .
IF Pn THEN Sn ELSE Sx
If condition P1 is true, only statement S1 is executed; if condition P2 is true, only statement S2 is executed, etc. If none of the conditions are true statement Sx will be executed. Conditions are tested in the order they are written. The above construction is similar to the CASE statement except that the conditions are not necessarily related.

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.