The availability of arrays and vectors makes it possible to structure many computations without using FOR, and this should be done wherever it is possible because it makes computations much faster and algorithm code much easier for people to understand. However, there are times when it is not possible to write a program without explicit use of FOR. In fact, FOR is one of the most common tools in programming.
The format of the FOR statement is as follows:
FOR Index = Start, Stop, Step DO Statement
Example: FOR k=11,21,2 DO PRINT,k
The Index is k
Start is the number 11
Stop is the number 21
Step is 2
Statement is PRINT,k
Start and Stop describe the range of numbers for the index variable. The Step specifies the increment of the index variable. A negative Step allows the index variable to step downward.
If Step is positive, the Statement is executed until Index is greater than Stop. If Step is negative, the Statement is executed until Index is less than Stop. If Step is omitted then a value of Step=1 is assumed.
Statement can be any valid IDL statement or a BLOCK.
Operation of the FOR Statement
The FOR statement performs the following steps:
1. The value of Start is evaluated and stored in the specified variable, which is called the index variable. The index variable is set to the type of this expression.
2. The value of Stop is evaluated, converted to the type of the index variable, and saved in a temporary location. This value is called the limit value.
3. The value of Step is evaluated, type-converted if necessary, and stored. If omitted, a value of 1 is assumed.
4. If the index variable is greater than the limit value (in the case of a positive step value) the FOR statement is finished and control resumes at the next statement. Similarly, in the case of a negative step value, if the index variable is less than or equal to the limit value, control resumes after the FOR statement.
5. The statement or block following the DO is executed.
6. The step value is added to the index variable.
7. Steps 4, 5, and 6 are repeated until the test of Step 4 fails.
The format of the WHILE statement is as follows:
WHILE Expression DO Statement
WHILE statements are used to execute a statement repeatedly while a condition remains true. The WHILE statement is similar to the REPEAT statement except that the condition is checked prior to the execution of the statement.
When the WHILE statement is executed, the Expression is tested, and if it is true, the statement following the DO is executed. Control then returns to the beginning of the WHILE statement, where the condition is again tested. This process is repeated until the condition is no longer true, at which point the control of the program resumes at the next statement.
In the WHILE statement, the subject is never executed if the condition is initially false.
Statement can be any valid IDL statement or a BLOCK.
EXAMPLE: Generate a sequence of random numbers and stop when the sum exceeds an absolute value of 10.
s=0
r=0
b=0.1
WHILE ABS(s) LE 10 DO BEGIN
s=s+RANDOMN(seed)+b
r=[r,s]
ENDWHILE
PLOT,r
This program is interesting in that it shows how many repetitions of
a game with a bias b are needed for you to win or lose 10 dollars. Setting
b=-0.1 will guarantee that you lose your money in about 100 iterations.
Let P be a proposition that may be TRUE or FALSE, and let S be a statement. The IF statement has the forms:
IF P THEN S
IF P THEN S1 ELSE S2
The proposition P can be any expression in IDL that produces a scalar result. TRUE and FALSE are determined by the value of the result. Logic statements produce actual TRUE or FALSE results. Other statements produce values that are interpreted as either TRUE or FALSE by using certain conventions.
IF proposition THEN BEGIN
statement 1
statement 2
etc
ENDIF
IF proposition THEN statement 1 ELSE BEGIN
statement 2
statement 3
etc
ENDELSE
IF proposition THEN BEGIN
statement 1
statement 2
etc
ENDIF ELSE statement 3
IF proposition THEN BEGIN
statement 1
statement 2
etc
ENDIF ELSE BEGIN
statement 3
statement 4
etc
ENDELSE
Nesting
of IF statements
The statement part of IF can be any valid IDL statement. This includes
another IF. This is most commonly used in combination with else to form
a logical tree. The form is: ($ is the line continuation symbol)
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.
Example: The segment of IDL code below will print out the type of the variable x. It uses the SIZE function to obtain the type code for the variable and then prints the appropriate message. Look up SIZE in IDL online help for a description of information that is returned by the function. We find the type code in position s[0]+1. Can you see why this is true?
s=SIZE(x)
typepos=s[0]+1
type=s[typepos]
IF type EQ 0 THEN PRINT,'Undefined' ELSE $
IF type EQ 1 THEN PRINT,'Byte' ELSE $
IF type EQ 2 THEN PRINT,'Integer' ELSE $
IF type EQ 3 THEN PRINT,'Longword integer' ELSE $
IF type EQ 4 THEN PRINT,'Floating point' ELSE $
IF type EQ 5 THEN PRINT,'Double-precision floating point' ELSE
$
IF type EQ 6 THEN PRINT,'Complex floating point' ELSE $
IF type EQ 7 THEN PRINT,'String' ELSE $
IF type EQ 8 THEN PRINT,'Structure' ELSE $
IF type EQ 9 THEN PRINT,'Double-precision complex' ELSE $
IF type EQ 10 THEN PRINT,'Pointer' ELSE $
IF type EQ 11 THEN PRINT,'Object reference' ELSE PRINT,'ERROR'
CASE Selector OF
Expression: Statement
...
...
...
...
Expression: Statement
ELSE: Statement
ENDCASE
Each statement is preceded by an expression that is compared to the value of the selector. If a match is found, the statement is executed and control resumes directly below the CASE statement. The ELSE clause of the CASE statement is optional. If included, it must be the last clause in the case statement. The statement after the ELSE is executed only if none of the preceding statement expressions match. If the ELSE is not included and none of the values match, an error occurs and program execution stops.
EXAMPLE: In the following name is a string that is compared to each entry in our directory. If name corresponds to one of the strings in the expression field then the information is printed. We put the case statement inside a function and make sure that it is in uppercase form.
FUNCTION DIRECTORY,t
name=STRUPCASE(t) ; Convert to upper case.
CASE name OF
'JANE': d='555-1212 Likes to eat at the Ritz'
'BILL': d='334-1234 wildbill@netplace.net'
ELSE: d='Not Found'
ENDCASE
RETURN,d
END