Custom Search
 
  

 
FOR-TO Statement

A typical FOR-TO statement would look like this:

Example:

In this example, M is the running variable. The first time the loop is executed, 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 statement contains 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 running variable to increase by 2 after each execution. We could write it this way:

Example:

The running variable, M, would be assigned a value of 1 during the first pass; 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 can be 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.

Examples:

Some important points to know and remember when creating a FOR-TO-NEXT loop are:

. The loop begins with a FOR-TO statement and ends with a NEXT statement.

. The same running variable name must be used in the FOR-TO and NEXT statements.

. The running variable can appear in a statement inside the loop, but its value cannot be changed.

. The running variable will be incremented by 1 unless otherwise 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 be executed at all.

1. The initial and final values of the running variable are equal and the step size is zero.

2. The final value of the running variable is less than the original value, and the step size is positive.

3. The final value of the running variable is greater than the original value, and the step size is negative.

. Control can be transferred out of a loop but not in. (The transfer out can 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.

Example:

This example shows the use of the running variable (A) within the 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 wont 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 of the FOR-NEXT loop structure.

Example:

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 the FOR-TO. . .NEXT statements. It is also possible to have a loop within a loop. These are called nested loops.







Western Governors University
 


Privacy Statement - Copyright Information. - Contact Us

Integrated Publishing, Inc. - A (SDVOSB) Service Disabled Veteran Owned Small Business