Menu
CoddyTech

START and READ NEXT

Part of the Programs That Work Together section of Coddy's COBOL Programming journey. Lesson 10 of 16.

With ACCESS MODE IS DYNAMIC a program can read by key and also walk the file in key order. READ ... NEXT RECORD reads the record after the current one:

READ CUST-MAST NEXT RECORD
    AT END SET END-OF-FILE TO TRUE
END-READ

Records come back in key order, whatever order they were written in.

START places the file at a key without reading. Move a key into the key field, then start at the first record whose key is equal or greater:

MOVE 10004 TO CUST-ID
START CUST-MAST KEY IS >= CUST-ID
    INVALID KEY DISPLAY "No customers"
END-START

INVALID KEY on START means no record has a key that high (status 23). The next READ ... NEXT reads the record START found.

challenge icon

Challenge

Easy

LIST-FROM lists the customers in key order from the id in CUST-ID, and the program runs it from 10004 and then from 99999. Write a START at its top so the list begins at the first key equal to or greater than CUST-ID; when there is none, print No customers from and the id and set END-OF-FILE.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. LISTFROM.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT CUST-MAST ASSIGN TO CUSTMAST
               ORGANIZATION IS INDEXED
               ACCESS MODE IS DYNAMIC
               RECORD KEY IS CUST-ID
               FILE STATUS IS MAST-STATUS.
       DATA DIVISION.
       FILE SECTION.
       FD CUST-MAST.
           COPY CUSTREC.
       WORKING-STORAGE SECTION.
       01 MAST-STATUS    PIC XX.
       01 EOF-FLAG       PIC X VALUE "N".
           88 END-OF-FILE  VALUE "Y".
       PROCEDURE DIVISION.
       MAIN-PARA.
           CALL "LOADMAST"
           OPEN INPUT CUST-MAST
           MOVE 10004 TO CUST-ID
           PERFORM LIST-FROM
           MOVE 99999 TO CUST-ID
           PERFORM LIST-FROM
           CLOSE CUST-MAST
           STOP RUN.
       LIST-FROM.
           MOVE "N" TO EOF-FLAG
           *> Start at CUST-ID here
           PERFORM UNTIL END-OF-FILE
               READ CUST-MAST NEXT RECORD
                   AT END SET END-OF-FILE TO TRUE
                   NOT AT END
                       DISPLAY CUST-ID " " CUST-NAME
               END-READ
           END-PERFORM.

All lessons in Programs That Work Together