Skip to main content

4.1) if...then...else Statement


As in other programming languages, the IF statement is used to check if a particular condition is true and then executes a piece of code based on the outcome of the check (true or false).

In it's simplest form, the IF statements takes the form:

If condition THEN statement 1 ELSE statement 2

So for example, if we want to check if the variable I is equal to a particular value, then print something to the screen depending on the outcome, we can use:

I = 2
IF I eq 2 THEN print, 'I is two' ELSE print, 'I is not two'

In this case, IDL will print 'I is two' to the screen. We can also use Boolean Operators to check for several conditions:

I = 2
j = 3
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'