Counted loop (FOR)

The EASI FOR command is similar to the WHILE looping function in that it also provides a general purpose looping construct. However, unlike the WHILE command, which continues to loop until a criteria is met, the FOR command will execute the statement list only a specific number of times.

The FOR statement generally has the following form:
 FOR iter_var = start_val TO end_val [By incr_val]
   statement list
 ENDFOR
where
 [iter_var]  is the iteration variable 
             (which may be any numeric variable type, including a parameter)
 [start_val] is the initial value to assign to the iter_var
 [end_val]   is the value at which the iter_var will stop at when reached
 [incr_val]  is the value by which to increment the iter_var at
             each iteration (the default is "1" )
The FOR statement initializes the iteration variable to the initial value and then executes the statement list. When the ENDFOR statement is reached, the iteration variable is increased by the increment value (or by 1 if using the default value). The new value is then compared to the end value. If this value is not exceeded, the statement is executed again. When the procedure has executed the required number of times, the loop will stop and the program will continue on to the next command immediately following the ENDFOR command.

For example, in the following program, a FOR statement is used to repeat a calculation only a certain number of times (specified by the user):

 local double i, NumberA, Highest
 print "This program will show a multiplication table for a given number."
 input "Enter the multiplier number: " NumberA
 input "Enter the highest number to show on the table: " Highest
 FOR i = 0 TO Highest BY 1
   print NumberA, " multiplied by ", i , " = " , i * NumberA
 ENDFOR

Notice that the counting variable, i, needs to be declared in the LOCAL statement. In the example, Highest is the end value. If 10 is entered as the highest number to be multiplied, the procedure will run the calculation 10 times. Each time, "i" will increase "BY" one (1).

Note that the increment value specified after the BY keyword in the FOR statement is set to 1, but since 1 is also the default increment value, its specification may be omitted. A for loop can by made to count by two's by specifying "BY 2", or to count backwards by specifying "BY -1". A For loop may also count by nonintegral values, using an increment such as "BY 0.5".

It is allowed to change the counting variable directly by assignment statement within a loop. In this case the counting will continue starting from the newly assigned value. This feature may be useful for quitting the loop in special cases. It is also possible to use GOTO for the same purpose, especially for quitting nested cycles.

© PCI Geomatics Enterprises, Inc.®, 2026. All rights reserved.