Programmer Guide > Statement Types > FOR Statement
  

FOR Statement
There are two basic forms of the FOR statement:
FOR var = expr1, expr2 DO statement
Form 1: Increment of 1.
FOR var = expr1, expr2, expr3 DO statement
Form 2: Variable increment.
The FOR statement is used to execute one or more statements repeatedly while incrementing or decrementing a variable each repetition until a condition is met. It is analogous to the DO statement in FORTRAN. 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.
 
note
The data type of the statement and the index variable are determined by the type of the initial value expression.
The FOR statement provided here does not act like the C/Pascal FOR statement that most users are used to. For example expr2 is not evaluated as a boolean expression with a true/false result, but rather directly compared to the index variable with true returned only if index is less than or equal to expr2.
Form 1: Implicit Increment
The FOR statement with an implicit increment of 1 is written as follows:
FOR var = expr1, expr2 DO statement
The variable after the FOR is called the index variable and is set to the value of the first expression. The statement is executed, and the index variable is incremented by one, 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. expr2 is not evaluated as a Boolean expression with a True/False result, but rather directly compared to the index variable I with True returned only if I expr2 .
An example of a FOR statement is:
FOR I = 1, 4 DO PRINT, I, I^2
which produces the output:
1 1
2 4
3 9
4 16
The index variable I is first set to an variable of type INT with a value of 1. The call to the PRINT procedure is executed, then the index is incremented by 1. This is repeated until the value of I is greater than 4, when execution continues at the statement following the FOR statement.
The next example displays the use of a block structure in place of the single statement for the subject of the FOR statement. The example is a common process used for computing a count-density histogram.
FOR K = 0, N - 1 DO BEGIN
C = A(K)
HIST(C) = HIST(C) + 1
ENDFOR
 
note
A HISTOGRAM function is provided in the Standard Library.
The indexing variables and expressions can be any integer or floating-point type.
Another example is:
FOR X = 1.5, 10.5 DO S = S + SQRT(X)
In this example, X is set to a floating-point variable and steps through the values (1.5, 2.5, ..., 10.5).
The type of the index variable is determined by the type of the first expression after the = character.
If you need to use very large integers in a FOR loop condition, be sure to designate them as longword in the FOR loop statement. For example:
FOR i=300000L, 700000L DO BEGIN
. . .
ENDFOR
Form 2: Explicit Increment
The format of the second type of FOR statement is:
FOR var = expr1, expr2, expr3 DO statement
The first two expressions describe the range of numbers the variable will assume. The third expression specifies the increment of the index variable. A negative increment allows the index variable to step downward. In this case, the first expression must have a value greater than that of the second expression. If it does not, the statement will not be executed.
The following examples demonstrate the second type of FOR statement:
; Decrement K has the values: 100., 99., ...,2., 1.
FOR K = 100.0, 1.0, -1 DO ...
; Increments by 2. LOOP has the values 0, 2, 4,..., 1022.
FOR LOOP = 0, 1023, 2 DO ...
; Divides range from BOTTOM to TOP by 4.
FOR MID = BOTTOM, TOP, (TOP - BOTTOM) / 4.0 DO ...
 
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 a variable of type INT because the initial value expression is an integer zero constant. Then the limit and increment expressions are converted to the type of X, INT, yielding an increment value of zero because .1 converted to integer type is zero. The correct form of the statement is:
FOR X = 0., 1, .1 DO ...
which defines X as a floating-point variable.

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.