Welcome to the Harris Geospatial product documentation center. Here you will find reference guides, help documents, and product libraries.


Harris Geospatial / Docs Center / IDL Reference / Statements / IF...THEN...ELSE

IF...THEN...ELSE

IF...THEN...ELSE

The IF...THEN...ELSE statement conditionally executes a statement or block of statements.

Note: Another way to write an IF...THEN...ELSE statement is with a conditional expression using the ?: operator. For more information, see Working with Conditional Expressions.

Tip: Programs with vector and array expressions run faster than programs with scalars, loops, and IF statements.

Examples


The following example illustrates the use of the IF statement using the ELSE clause. Notice that the IF statement is ended with ENDIF, and the ELSE statement is ended with ENDELSE. Also notice that the IF statement can be used with or without the BEGIN...END block:

A = 2
B = 4
 
IF (A EQ 2) AND (B EQ 3) THEN BEGIN
    PRINT, 'A = ', A
    PRINT, 'B = ', B
ENDIF ELSE BEGIN
    IF A NE 2 THEN PRINT, 'A != 2' ELSE PRINT, 'B != 3'
ENDELSE

IDL Prints:

B != 3

Nesting IF Statements

IF statements can 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 construction above is similar to the CASE statement except that the conditions are not necessarily related.

Syntax


IF expression THEN statement [ ELSE statement ]

or

IF expression THEN BEGIN

   statements

ENDIF [ ELSE BEGIN

   statements

ENDELSE ]

Version History


Original

Introduced

See Also


BEGIN...END, BREAK, CASE, CONTINUE, FOR, FOREACH, GOTO, REPEAT...UNTIL, SWITCH, WHILE...DO

IDL Programming



© 2017 Exelis Visual Information Solutions, Inc. |  Legal
My Account    |    Buy    |    Contact Us