One of the most common programming tasks is to perform the same set of statements multiple times. Rather than repeat a set of statements again and again, a loop can be used to perform the same set of statements repeatedly.
| Note |
array = INDGEN(10)
FOR i = 0,9 DO BEGIN
array[i] = array[i] * 2
ENDFOR
array = INDGEN(10)
array = array * 2
The FOR statement is used to execute one or more statements repeatedly, while incrementing or decrementing a variable with each repetition, until a condition is met. It is analogous to the DO statement in FORTRAN.
In IDL, there are two types of FOR statements: one with an implicit increment of 1 and the other with an explicit increment. If the condition is not met the first time the FOR statement is executed, the subject statement is not executed.
The FOR statement with an implicit increment of one is written as follows:
FOR Variable = Expression, Expression DO Statement
The variable after the FOR is called the index variable and is set to the value of the first expression. The subject statement is executed, and the index variable is incremented by 1 until the index variable is larger than the second expression. This second expression is called the limit expression. Complex limit and increment expressions are converted to floating-point type.
| Warning |
FOR I = 0, 50000 DO ... ...
FOR I = 0L, 50000 DO ... ...
| Warning |
FOR i = 0B, 240, 16 DO PRINT, i
A simple FOR statement:
FOR I = 1, 4 DO PRINT, I, I^2
This statement produces the following output:
1 1 2 4 3 9 4 16
The index variable I is first set to an integer variable with a value of one. The call to the PRINT procedure is executed, then the index is incremented by one. This is repeated until the value of I is greater than four at which point execution continues at the statement following the FOR statement.
The next example displays the use of a block structure (instead of a single statement) as the subject of the FOR statement. The example is a common process used for computing a count-density histogram. (Note that a HISTOGRAM function is provided by IDL.)
FOR K = 0, N - 1 DO BEGIN C = A[K] HIST(C) = HIST(C)+1 ENDFOR
The next example displays a FOR statement with floating-point index and limit expressions, where X is set to a floating-point variable and steps through the values (1.5, 2.5, ..., 10.5):
FOR X = 1.5, 10.5 DO S = S + SQRT(X)
The indexing variables and expressions can be integer, longword, floating-point, or double-precision. The type of the index variable is determined by the type of the first expression after the "=" character.
| Warning |
PRINT, 0.1, 0.01, 1.6, 1.7, FORMAT='(f20.10)'
0.1000000015
0.0099999998
1.6000000238
1.7000000477
The format of the second type of FOR statement is as follows:
FOR Variable = Expression1, Expression2, Increment DO Statement
This form is used when an increment other than 1 is desired.
The first two expressions describe the range of numbers for the index variable. The Increment specifies the increment of the index variable. A negative increment allows the index variable to step downward.
The following examples demonstrate the second type of FOR statement.
;Decrement, K has the values 100., 99., ..., 1. FOR K = 100.0, 1.0, -1 DO ... ;Increment by 2., loop has the values 0., 2., 4., ..., 1022. FOR loop = 0, 1023, 2 DO ... ;Divide range from bottom to top by 4. FOR mid = bottom, top, (top - bottom)/4.0 DO ...
| Warning |
FOR X = 0, 1, .1 DO ....
FOR X = 0., 1, .1 DO ....
The FOR statement performs the following steps:
REPEAT...UNIL loops are used to repetitively execute a subject statement until a condition is true. The condition is checked after the subject statement is executed. Therefore, the subject statement is always executed at least once. (See Definition of True and False for details on how the "truth" of an expression is determined.)
The syntax of the REPEAT statement is as follows:
REPEAT statement UNTIL expression
or
REPEAT BEGIN statements ENDREP UNTIL expression
The following example finds the smallest power of 2 that is greater than B:
A = 1 B = 10 REPEAT A = A * 2 UNTIL A GT B
The subject statement can also be in the form of a block:
A = 1 B = 10 REPEAT BEGIN A = A * 2 ENDREP UNTIL A GT B
The next example sorts the elements of ARR using the inefficient bubble sort method. (A more efficient way to sort elements is to use IDL's SORT function.)
;Sort array. REPEAT BEGIN ;Set flag to true. NOSWAP = 1 FOR I = 0, N - 2 DO IF arr[I] GT arr[I + 1]THEN BEGIN ;Swapped elements, clear flag. NOSWAP = 0 T = arr[I] & arr[I] = arr[I + 1] & arr[I + 1] = T ENDIF ;Keep going until nothing is moved. ENDREP UNTIL NOSWAP
WHILE...DO loops are used to execute a statement repeatedly while a condition remains true. The WHILE...DO statement is similar to the REPEAT...UNTIL statement except that the condition is checked prior to the execution of the statement. (See Definition of True and False for details on how the "truth" of an expression is determined.)
The syntax of the WHILE...DO statement is as follows:
WHILE expression DO statement
or
WHILE expression DO BEGIN statements ENDWHILE
When the WHILE statement is executed, the conditional 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.
The following example reads data until the end-of-file is encountered:
WHILE ~ EOF(1) DO READF, 1, A, B, C
The subject statement can also be in the form of a block:
WHILE ~ EOF(1) DO BEGIN READF, 1, A, B, C ENDWHILE
The next example demonstrates one way to find the first element of an array greater than or equal to a specified value assuming the array is sorted into ascending order:
array = [2, 3, 5, 6, 10] i = 0 ;Initialize index n = N_ELEMENTS(array) ;Increment i until a point larger than 5 is found or the end of the ;array is reached: WHILE (array[i] LT 5) AND (i LT n) DO i = i + 1 PRINT, 'The first element >= 5 is element ', i
IDL Prints:
The first element >= 5 is element 2
| Tip |
P = WHERE(arr GE X)
;Save first subscript:
I = P(0)