READ NEXT and START
A program that reads a whole indexed file always starts at its first key. A program that reads part of it names the key it wants and lets START position the file, then walks forward with READ ... NEXT until the key leaves the range or the file ends.
START positions the file
START takes the file and a key comparison, and it moves the file's position to the first record that satisfies it. Nothing is read: the record is not in the area until the next READ:
IDENTIFICATION DIVISION.
PROGRAM-ID. STARTRUN.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUS-F ASSIGN TO "cus5.idx"
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS CUS-ID
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD CUS-F.
01 CUS-REC.
05 CUS-ID PIC X(3).
05 CUS-NAME PIC X(8).
WORKING-STORAGE SECTION.
01 WS-STATUS PIC XX.
01 SEEK-F PIC X(3) VALUE "C30".
PROCEDURE DIVISION.
OPEN OUTPUT CUS-F.
MOVE "A10" TO CUS-ID.
MOVE "Ada" TO CUS-NAME.
WRITE CUS-REC.
MOVE "B20" TO CUS-ID.
MOVE "Bob" TO CUS-NAME.
WRITE CUS-REC.
MOVE "C30" TO CUS-ID.
MOVE "Carol" TO CUS-NAME.
WRITE CUS-REC.
MOVE "D40" TO CUS-ID.
MOVE "Dan" TO CUS-NAME.
WRITE CUS-REC.
MOVE "E50" TO CUS-ID.
MOVE "Eve" TO CUS-NAME.
WRITE CUS-REC.
CLOSE CUS-F.
OPEN INPUT CUS-F.
MOVE SEEK-F TO CUS-ID.
START CUS-F KEY IS NOT LESS THAN CUS-ID
INVALID KEY DISPLAY "no record from " SEEK-F
NOT INVALID KEY DISPLAY "start at or after " SEEK-F
END-START.
PERFORM UNTIL WS-STATUS = "10"
READ CUS-F NEXT
AT END CONTINUE
NOT AT END DISPLAY " " CUS-ID " " CUS-NAME
END-READ
END-PERFORM.
CLOSE CUS-F.
STOP RUN.
cobc -x -free -Wall -o startrun start.cob
./startrun
start at or after C30
C30 Carol
D40 Dan
E50 Eve
READ NEXT from that position
The three records from C on were read in key order and the loop stopped at the end of the file. The two records before C were never read, which is the point of the verb: a program that processes the orders of one branch, or the customers whose identifier starts with a prefix, positions the file and reads forward instead of testing every record.
KEY IS NOT LESS THAN is the comparison >= written in words. The other forms are EQUAL TO and GREATER THAN, and the last one is how a program walks a range that starts after a key it has already processed.
A key past the end
A key higher than every record in the file cannot be positioned, and START takes the INVALID KEY branch with status 23:
IDENTIFICATION DIVISION.
PROGRAM-ID. BADSTART.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUS-F ASSIGN TO "cus6.idx"
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS CUS-ID
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD CUS-F.
01 CUS-REC.
05 CUS-ID PIC X(3).
WORKING-STORAGE SECTION.
01 WS-STATUS PIC XX.
01 SEEK-F PIC X(3) VALUE "Z99".
PROCEDURE DIVISION.
OPEN OUTPUT CUS-F.
MOVE "A10" TO CUS-ID.
WRITE CUS-REC.
MOVE "B20" TO CUS-ID.
WRITE CUS-REC.
CLOSE CUS-F.
OPEN INPUT CUS-F.
MOVE SEEK-F TO CUS-ID.
START CUS-F KEY IS NOT LESS THAN CUS-ID
INVALID KEY DISPLAY "no record from " SEEK-F " status " WS-STATUS
NOT INVALID KEY DISPLAY "start ok"
END-START.
CLOSE CUS-F.
STOP RUN.
cobc -x -free -Wall -o badstart badstart.cob
./badstart
no record from Z99 status 23
The same 23 a random read of a missing key returns, which is the code of the status table for "no record". A program that starts a range has to treat it as an empty range and not as an error.
The modes that allow START
START needs the file open for input or I-O. It does not need a special access mode: this compiler accepts it on a DYNAMIC file, which is the mode that also allows the random reads of the previous lesson, and a program that both finds records by key and walks a range uses that one mode for both.
The next lesson is the last of the file part with the verbs that change a record in place: REWRITE and DELETE, which need the file open for I-O and a record the program has just read.
Exercises
- Change the comparison to KEY IS GREATER THAN and run the program. Which record is the first one printed?
- Start at "B", one character of a three-character key, and print the result. Which records come out, and why?
- Start at "D40" and stop when the key is greater than "D40". How many records does the loop print?
- Move the START into a paragraph and perform it with three different keys. What changes between the runs?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does START do to an indexed file?
Answer
It positions the file at the first record that satisfies the key comparison
Nothing is read until the next READ, and the program printed the record only after its first READ NEXT.
-
Which comparison did the program use to start at C or later?
Answer
KEY IS NOT LESS THAN CUS-ID
NOT LESS THAN is the greater-or-equal comparison, so the record at C30 itself was included.
-
What happens when START names a key past the last record?
Answer
It takes the INVALID KEY branch with status 23
The second program printed no record from Z99 status 23.
-
Which mode does START need?
Answer
The file open for input or I-O, with an access mode that allows keys
The page used an INPUT open with DYNAMIC access, which allows both START and the random read.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-I ASSIGN TO "q5.idx" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS K-F FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-I. 01 R-I. 05 K-F PIC X(2). WORKING-STORAGE SECTION. 01 W-S PIC XX. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. OPEN OUTPUT F-I. MOVE "AA" TO K-F. WRITE R-I. MOVE "BB" TO K-F. WRITE R-I. MOVE "CC" TO K-F. WRITE R-I. CLOSE F-I. OPEN INPUT F-I. MOVE "BB" TO K-F. START F-I KEY IS NOT LESS THAN K-F INVALID KEY DISPLAY "none" END-START. PERFORM UNTIL W-S = "10" READ F-I NEXT AT END CONTINUE NOT AT END ADD 1 TO N-F END-READ END-PERFORM. CLOSE F-I. DISPLAY N-F. STOP RUN.Answer
2
The start is at BB, so the loop reads BB and CC.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-I ASSIGN TO "q6.idx" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS K-F FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-I. 01 R-I. 05 K-F PIC X(2). WORKING-STORAGE SECTION. 01 W-S PIC XX. PROCEDURE DIVISION. OPEN OUTPUT F-I. MOVE "AA" TO K-F. WRITE R-I. CLOSE F-I. OPEN INPUT F-I. MOVE "ZZ" TO K-F. START F-I KEY IS NOT LESS THAN K-F INVALID KEY DISPLAY "none " W-S NOT INVALID KEY DISPLAY "ok" END-START. CLOSE F-I. STOP RUN.Answer
none 23
ZZ is past the only record, so the start is invalid with the not-found code.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-F PIC X(2) VALUE "CC". 01 B-F PIC X(2) VALUE "BB". PROCEDURE DIVISION. IF B-F IS LESS THAN A-F DISPLAY "BB first" ELSE DISPLAY "CC first" END-IF. STOP RUN.Answer
BB first
The comparison is on the bytes, and BB sorts before CC.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 K-F PIC X(3) VALUE "BB". 01 M-F PIC X(3) VALUE "BB". PROCEDURE DIVISION. IF K-F IS NOT LESS THAN M-F DISPLAY "at or after" ELSE DISPLAY "before" END-IF. STOP RUN.Answer
at or after
The two values are equal, and NOT LESS THAN includes equality.
-
A program processes one branch of customers from an indexed file. Which pair of verbs fits?
Answer
START at the branch key and READ NEXT until the key changes
The start positions at the first record of the range, and the walk stops when the key leaves it.
-
Why is status 23 on START not an error?
Answer
It means the range is empty, which is a normal result of a query
A key past the last record gives an empty range, and the program reports it instead of failing.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev