The FOR statement executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met.
FOR variable = init, limit [, Increment] DO statement
FOR variable = init, limit [, Increment] DO BEGIN
Data Type for the Loop Variable
By default the data type of the index variable is determined by the type of the initial value. However, if the value of the second expression (the loop limit) is larger than the maximum integer value for the initial value, then the index variable will be automatically promoted to the larger type. For example:
FOR I=0,32000 DO J = I
FOR I=0,33000 DO J = I
FOR I=0,33000.0 DO J = I
I INT = 32001
I LONG = 33001
I FLOAT = 33001.0
In the first case the loop limit is within the short integer range -32768...32767, and the type for the loop variable is INT. In the second and third cases the loop limit is greater than 32767, and the loop variable is automatically promoted to type LONG or type FLOAT.
Note: Changing the data type of an index variable within a loop is not allowed, and will cause an error.
Note: If the initial value is an unsigned integer type (such as BYTE, UINT, ULONG, ULONG64), then the index variable is not automatically promoted. Instead, the loop limit is converted to the unsigned integer type. Note that if your loop limit is larger than the maximum value for that unsigned type, then the limit will wrap around when converted to the unsigned type, which may make the loop prematurely finish. For example:
FOR i=0b,257 DO PRINT, i
The integer 257 gets converted to type byte which gives the value 1b, and therefore the loop executes twice.
Avoid Invariant Expressions
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] = ....
FOR Statement with an Increment of One
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.
Example: FOR Statement with Increment of One
A simple FOR statement:
FOR I = 1, 4 DO PRINT, I, I^2
This statement produces the following output:
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
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.
Note: 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. See Accuracy and Floating Point Operations for more information about floating-point numbers.
FOR Statement with Variable Increment
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.
Example: FOR Statement with Variable Increment
The following examples demonstrate the second type of FOR statement.
FOR K = 100.0, 1.0, -1 DO ...
FOR loop = 0, 1023, 2 DO ...
FOR mid = bottom, top, (top - bottom)/4.0 DO ...
Note: 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.0, 1, .1 DO ....
which defines X as a floating-point variable.
Sequence of the FOR Statement
The FOR statement performs the following steps:
- The value of the first expression 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 (unless the value of the limit is greater than the maximum integer for that data type).
- The value of the second expression is evaluated, converted to the type of the index variable, and saved in a temporary location. This value is called the limit value.
- The value of the third expression, called the step value, is evaluated, type-converted if necessary, and stored. If omitted, a value of 1 is assumed.
- 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 the limit value, control resumes after the FOR statement.
- The statement or block following the DO is executed.
- The step value is added to the index variable.
- Steps 4, 5, and 6 are repeated until the test of Step 4 fails.
WHILE...DO, IDL Programming