if condition then statement 1 else
statement 2
This will execute statement 1 if the condition is true and statement 2 if the condition is false. (Note that in these descriptions of what a statement does I use typewriter font to show what you actually type and italics to indicate where you have to put in your own stuff.) Here is an example if an `if' statement:
if i eq 2 then print,'I is two' else print, 'i is not two'or
if i eq 2 and j eq 3 then print,'I is two and J is three' else print, 'i and j are not two and three respectively'
Notice how the logical operators you need to set up the condition look like the FORTRAN ones (but without the dots), not like the C ones. Here is a table of relational and boolean operators which you can use in the condition of an if statement.
Purpose | IDL | C | FORTRAN |
Relational Operators | |||
Equal to | eq | == | .EQ. |
Not equal to | ne | != | .NE. |
Less than or equal to | le | .LE. | |
Less than | lt | .LT. | |
Greater than or equal to | ge | = | .GE. |
Greater than | gt | .GT. | |
Boolean Operators | |||
And | and | && | .AND. |
Not | not | ! | .NOT. |
Or | or | || | .OR. |
Exclusive OR | xor |
If you want statement 1 and / or statement 2 to consist of more than one statement, then the if construct looks like this:
if condition then begin
statement 1a
statement 1b
statement 1c
endif else begin
statement 2a
statement 2b
statement 2c
endelse
The statements between a begin and an end are called a block of statements. The begin and end are analogous to the curly brackets in 'C' or Java except that the end statement has to match the thing that comes before the begin e.g. an if .... then begin has to be matched with an endif and a else begin with an endelse. Blocks of statements are used within programs, not at the IDL prompt.