7.2.3 The FOR - NEXT Statement

Another way to write loop statements is the use of the FOR statement. This is a specialization of LOOP. The FOR is usually appropriate for loops in which the initialization and increment are single statements and logically related, since it is more compact than LOOP and it keeps the loop control statements together in one place.

Different to the LOOP statement, the control variable and control test can only handle integer or floating point variables.

Now we do the same program as before using the FOR statement:

/* Print Fahrenheit - Celsius table using FOR */

LET Fahreinheit$= "Fahrenheit : " LET Celsius$ = "Celsius : "

LET lower = 0

LET upper = 300

LET step = 20

LET fahr = lower

FOR fahr = lower TO upper STEP step

LET celsius = (5/9)*(fahr-32)

PRINT Fahrenheit$PRINT fahr PRINT CR PRINT Celsius$ PRINT celsius PRINT CR

NEXT fahr

As one can see, the FOR statement is well suited for loops in which the number of computations of the loop is exactly known, and the termination test is more of a "numerical" than a "logical" nature. Of course not only incremental, but also decrement FOR loops with a not necessarily integer step-size are possible.

The following program

/* Print Fahrenheit - Celsius table using FOR */

LET Fahreinheit$= "Fahrenheit : " LET Celsius$ = "Celsius : "

LET lower = 0

LET upper = 300

LET step = -20 /* step has a negative value */

LET fahr = lower

FOR fahr = upper TO lower STEP step

LET celsius = (5/9)*(fahr-32)

PRINT Fahrenheit$PRINT fahr PRINT CR PRINT Celsius$ PRINT celsius PRINT CR

NEXT fahr

prints as a list of Celsius - Fahrenheit correspondings in a decremental order:

Fahrenheit : 300

Celsius : 148.88888888

...

Fahrenheit : 20

Celsius : -6.666666667

Fahrenheit : 0

Celsius : -17.77777778

The FOR-loop operates as follows: the condition, whether there are still computation steps left is tested. If it is false, we leave the FOR statement, otherwise the body of the loop is executed. Then the condition is re-tested, and if true, the body is executed again. The body of a FOR can be one or more statements, where the statement block within the body of the loop is terminated by the NEXT statement.

Of course both, LOOP and FOR are statements on their own and can be used to build a statement block for the loop body of other LOOP and FOR statements:

LOOP

...

| LOOP

| ...

| EXITLOOP (...)

| ...

| ENDLOOP

...

EXITLOOP (...)

...

ENDLOOP

FOR a = ... TO ... STEP ...

...

| FOR b = ... TO ... STEP ...

| ...

| NEXT b

...

NEXT a

All control-flow constructions can encapsulate other ones without restrictions. So any statement can be several statements enclosed by keywords denoting begin and end of a statement block. The general form of the control-flow constructions we met so far is:

a) IF <condition> THEN

<statement block>

ELSE

<statement block>

ENDIF

b) LOOP

<statement block>

EXITLOOP <condition>

<statement block>

ENDLOOP

c) FOR <start assignment> TO <end assignment> STEP <step assignment>

<statement block>

NEXT

d) PROCEDUE procedurename

<statement block>

RETURN