Introduction MTB Statements File System Compiler Applications Reference Guide Index

Language Structure Data Division I/O Format Division Procedure Division Mnemonics

NEXT statement

Syntax:
FOR control-variable = start-count TO stop-count [STEP increment]
.
.
.
NEXT control-variable
Discussion: The NEXT statement terminates a repetitive series of program statements (i.e., a loop). The statements between a FOR statement up to the associated NEXT statement form the loop.

The control-variable parameter is a previously-defined numeric variable. Its start-count, stop-count, and increment values are established by the preceding FOR statement. The NEXT statement increments the control-variable and branches to the previous FOR statement.

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