Previous Topic: FOR EACH/FIRST/ANY Statement (Sequential Files)Next Topic: FOR NEW Statement (Sequential Files)


FOR NEXT Statement (Sequential Files)

The FOR NEXT statement specifies a series of statements that apply only to the next record of a sequential file dataview. If a previous FOR FIRST was executed for the same dataview, the next record in sequence is accessed. If no previous FOR FIRST was executed, FOR NEXT accesses the first record.

Note: This statement is not iterative. To repeat execution of this statement, you must code it in a LOOP construct.

Under z/OS or VSE, this construct applies only to sequential file dataviews in applications run in batch since sequential files cannot be read online.

This statement has the following format:

<<label>>
     FOR [THE] NEXT dataview_name
            statements

   [ WHEN NONE      ]
   [     statements ]

   [ WHEN ERROR     ]
   [     statements ]

  ENDFOR
<<label>> (Optional)

Specifies a 1‑ to 15‑character name of the FOR construct. You can use it to refer to the construct in a QUIT statement.

FOR [THE] NEXT

Specifies that the action to take only applies to the next record of a sequential file dataview. You can add the reserved word THE for readability.

dataview‑name

Specifies the name of the dataview that defines each record to read.

WHEN NONE(Optional)

Reserved words that specify that when no next record exists (for example, there are no more records in the file), the statements following the WHEN NONE execute.

WHEN ERROR(Optional)

Specifies statements to execute when a dataview error is encountered in the scope of the FOR construct. If WHEN ERROR is not specified, errors are processed by the user‑defined or default error procedure.

The statements specified following a WHEN ERROR clause can access $ERROR functions and should resolve the error with either a PROCESS NEXT or DO ERROR statement. If processing falls through to the ENDFOR, the $ERROR functions are no longer available.

Note: Only dataview errors are handled by the WHEN ERROR clause. System and internal errors are handled by the user‑specified or default error procedure.

ENDFOR

A reserved word that terminates the FOR construct. If FOR constructs are nested, the most recent unterminated FOR construct is terminated by the first occurrence of ENDFOR. Each FOR must have a corresponding ENDFOR.

The FOR EACH construct is used for most sequential processing. The FOR NEXT construct is used in situations where only one record, following the current record, is required.

You can nest any of the FOR constructs as long as each FOR construct refers to a different dataview. Do not nest a FOR construct for a given dataview in another FOR construct for the same dataview.

Example

FOR NEXT EMPLOYEE
   IF STATUS = 'T'
     SET MSG = 'EMPLOYEE TERMINATED'
  ENDIF
ENDFOR

Example

<<MAIN>>PROCEDURE
<<LOAD>> LOOP
  FOR NEXT STUDENT_QSAM
    IF STUDENT_QSAM.CUM_GPA >= 3.5
       PRODUCE JEDEANS
    ENDIF
  WHEN NONE
    IF $COUNT (LOAD) = 0
        LIST 'NO RECORDS IN STUDENT_QSAM'
    ELSE
        SET RECORDS = $COUNT (LOAD)
       LIST RECORDS 'RECORDS PROCESSED ' SKIP
        LIST '***END OF FILE ***'
   ENDIF
    QUIT RUN
  ENDFOR
  FOR NEW STUDENT_LOAD
    MOVE STUDENT_QSAM TO STUDENT_LOAD BY NAME
  WHEN DUPLICATE
    LIST 'STUDENT #'
    STUDENT_QSAM.STUDENT.NR 'ALREADY EXISTS'
 ENDFOR
ENDLOOP