A typical FOR-TO statement would look like this:
In this example, M is the running variable. The first time the loop isexecuted, M will be assigned a value of 1. M will increase by 1 each time the loop is executed, until M has reached its final value of 36. The loop will be terminated once M has exceeded its final value of 36. The loop in the example will be executed 36 times.
The running variable will always increase by 1, if the FOR-TO statementcontains no instructions telling it to do otherwise. However, we can increment the running variable by some value other than 1 if we wish. This can be done by the addition of a STEP clause to the FOR-TO statement.
Suppose we want to execute a loop 50 times, and we want the runningvariable to increase by 2 after each execution. We could write it this way:
The running variable, M, would be assigned a value of 1 during the firstpass; a value of 3 during the second pass; 5 during the third pass and so on, until the value of M was 99 during the 50th (final) pass.
The running variable does not have to be a positive integer value; it canbe a negative or decimal value. Also, the running variable can be made to decrease with each execution of the loop. This is done by specifying a negative value in the STEP clause. The initial, final, and STEP values assigned to the running variable can be expressed as variables or expressions as well as numbers.
Some important points to know and remember when creating aFOR-TO-NEXT loop are:
. The same running variable name must be used in the FOR-TOand NEXT statements.
. The running variable can appear in a statement inside theloop, but its value cannot be changed.
. The running variable will be incremented by 1 unlessotherwise specified by a STEP clause.
. If the initial and final values of the running variable are equal,and the step size is nonzero, the loop will be executed once.
. There are three conditions under which a loop will not beexecuted at all.
and the step size is zero.
2. The final value of the running variable is less than the originalvalue, and the step size is positive.
3. The final value of the running variable is greater than theoriginal value, and the step size is negative.
. Control can be transferred out of a loop but not in. (The transfer outcan be done by using a GOTO, an ON-GOTO, or an IF-THEN statement.)
Examine the following loop and see how it conforms to the points just listed.
This example shows the use of the running variable (A) withinthe loop (line number 650). The statement in line number 660 will cause control to be transferred outside the loop, if the value of X is greater than the value of Xl. Also, the running variable (A) that appears in the NEXT statement is the same as the running variable in the FOR-TO statement. These must be the same or the loop won’t work. The step clause specifies that A is to be increased by .2 each time the loop is executed. If X does not exceed Xl the loop will be executed 9 times.
The following mortgage amortization program contains an example ofthe FOR-NEXT loop structure.
The loop in this example is comprised of lines 80 through 130.Line 80 sets the initial value of M to 1 for the first execution of the loop. Lines 90 through 120 perform the calculations and print the results. Once the PRINT statement in line 120 has been executed, the NEXT statement in line 130 directs the computer to start the loop all over again. The loop will continue until the number of times it has been executed is equal to N (line 80). N is the number of months of the loan.
This example shows the use of a single loop structure using theFOR-TO. . .NEXT statements. It is also possible to have a loop within a loop. These are called nested loops.