|
Application Programming: Program Control |
|
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. See the following topics for details:
When using FOR loops, you can increase program efficiency by avoiding invariant expressions. Expressions whose values do not change inside a loop should be moved outside the loop. For example, in the loop:
FOR I = 0, N - 1 DO arr[I, 2*J-1] = ...,
the expression (2*J-1) is invariant and should be evaluated only once before the loop is entered:
temp = 2*J-1 FOR I = 0, N-1 DO arr[I, temp] = ....
See Arrays for details on working with arrays.
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 The data type of the index variable is determined by the type of the initial value expression. Keep this fact in mind to avoid the following: FOR I = 0, 50000 DO ... ...This loop does not produce the intended result. Converting the longword constant 50,000 to a short integer yields -15,536 because of truncation. The loop is not executed. The index variable's initial value is larger than the limit variable. The loop should be written as follows: FOR I = 0L, 50000 DO ... ...Note also that changing the data type of an index variable within a loop is not allowed, and will cause an error. |
| Warning Also be aware of FOR loops that are entered but are not terminated after the expected number of iterations, because of the truncation effect. For example, if the index value exceeds the maximum value for the initial data type (and so is truncated) when it is expected instead to exceed the specified index limit, then the loop will continue beyond the expected number of iterations. The following FOR statement continues infinitely: FOR i = 0B, 240, 16 DO PRINT, iThe problem occurs because the variable i is initialized to a byte type with 0B. After the index reaches the limit value 240B, i is incremented by 16, causing the value to go to 256B, which is interpreted by IDL as 0B, because of the truncation effect. As a result, the FOR loop "wraps around" and the index can never be exceeded. |
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 Due to the inexact nature of IEEE floating-point numbers, using floating-point indexing can cause "infinite loops" and other problems. This problem is also manifested in both the C and FORTRAN programming languages. For example, the numbers 0.1, 0.01, 1.6, and 1.7 do not have exact representations under the IEEE standard. To see this phenomenon, enter the following IDL command: PRINT, 0.1, 0.01, 1.6, 1.7, FORMAT='(f20.10)'IDL prints the following approximations to the numbers we requested: 0.1000000015See Accuracy and Floating Point Operations for more information about floating-point numbers. |
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 If the value of the increment expression is zero, an infinite loop occurs. A common mistake resulting in an infinite loop is a statement similar to the following: FOR X = 0, 1, .1 DO ....The variable X is first defined as an integer variable because the initial value expression is an integer zero constant. Then the limit and increment expressions are converted to the type of X, integer, yielding an increment value of zero because .1 converted to integer type is 0. The correct form of the statement is: FOR X = 0., 1, .1 DO ....which defines X as a floating-point variable. |
The FOR statement performs the following steps:
IDL Online Help (March 06, 2007)