IB Statements/for

From CometWiki

Jump to: navigation, search

FOR statement

Syntax: FOR control-variable = start-count TO stop-count [STEP increment]


NEXT control-variable

Discussion: The FOR statement starts a repetitive series of program statements (i.e., a loop). The statements following a FOR statement up to the associated NEXT statement form the loop.

The control-variable parameter is a previously-defined numeric variable with sufficient length and precision to store the values specified by the start-count, stop-count, and increment parameters.

The start-count parameter is the initial value assigned to the control-variable. It may be a numeric constant or numeric variable.

The stop-count parameter is the maximum value assigned to the control-variable. Stop-count may be a numeric constant or numeric variable. When the maximum value has been reached, the statements inside the loop will be executed for a final time and program flow will continue at the statement immediately following the NEXT statement. (The control-variable will retain the maximum value assigned.)

During the looping process, the value of the control-variable is incremented according to the increment parameter. If not specified, the increment is 1. The increment may be a numeric constant or numeric variable.

FOR/NEXT loops may be nested to any level.

Example 1:

FOR VALUE = 1 TO 250        ! Start loop from 1 to 250
SQUARE = VALUE * VALUE    ! Compute square
PRINT (0) VALUE;SQUARE    ! Print value and square
NEXT VALUE                  ! Continue loop

Example 2:

FOR LINE = 1 TO 60          ! Loop from 1 to 60
READ (1,DATA) EXCP=9999   ! Read the next data record
PRINT (2,INFO)            ! Print a line on the printer
NEXT LINE                   ! Continue loop

Example 3:

FOR I = J TO K STEP M       ! Loop using variable values
PRINT (0) "Value is: ";I  ! Print the value
NEXT I                      ! Continue loop

Example 4:

FOR ROW = 1 TO 50             ! Start outer loop (count 1 to 50)
FOR COLUMN = 1 TO 200       ! Start inner loop (count 1 to 200)
SUM=SUM+DATA(COLUMN,ROW)  ! Add up array elements
NEXT COLUMN                 ! Continue inner loop
NEXT ROW                      ! Continue outer loop
Personal tools