MainframeMaster

COBOL Tutorial

COBOL NEXT

The NEXT keyword represents sophisticated sequential processing and iteration control capabilities within COBOL programming environments, providing comprehensive record advancement mechanisms, advanced file traversal features, and intelligent sequential access patterns that enable efficient data processing, ordered record retrieval, and systematic file navigation. This keyword embodies modern sequential processing principles by supporting predictable record advancement, enabling comprehensive file traversal workflows, and facilitating ordered data access requirements while maintaining processing efficiency, ensuring consistent sequential behavior, and enabling scalable file processing architectures across enterprise applications requiring systematic record processing, ordered data retrieval, and reliable sequential iteration throughout complex business data management and file processing scenarios.

NEXT Usage Patterns

NEXT in Different Contexts
cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
*> READ NEXT for sequential file processing READ file-name NEXT RECORD AT END imperative-statements NOT AT END imperative-statements END-READ. *> SEARCH with NEXT for table iteration SEARCH table-name AT END imperative-statements WHEN condition imperative-statements END-SEARCH. *> Examples with different file types *> Sequential file processing FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC X(8). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-BALANCE PIC 9(8)V99. PROCEDURE DIVISION. PROCESS-CUSTOMERS. OPEN INPUT CUSTOMER-FILE READ CUSTOMER-FILE NEXT RECORD AT END MOVE 'Y' TO EOF-FLAG NOT AT END PERFORM PROCESS-CUSTOMER-RECORD END-READ PERFORM UNTIL EOF-FLAG = 'Y' READ CUSTOMER-FILE NEXT RECORD AT END MOVE 'Y' TO EOF-FLAG NOT AT END PERFORM PROCESS-CUSTOMER-RECORD END-READ END-PERFORM CLOSE CUSTOMER-FILE. *> Indexed file with NEXT FD PRODUCT-FILE. 01 PRODUCT-RECORD. 05 PRODUCT-CODE PIC X(10). 05 PRODUCT-NAME PIC X(25). 05 PRODUCT-PRICE PIC 9(6)V99. PROCEDURE DIVISION. SEQUENTIAL-PRODUCT-PROCESSING. OPEN INPUT PRODUCT-FILE *> Start from beginning READ PRODUCT-FILE NEXT RECORD AT END DISPLAY "No products found" NOT AT END DISPLAY "First product: " PRODUCT-NAME END-READ *> Continue reading next records PERFORM UNTIL EOF-FLAG = 'Y' READ PRODUCT-FILE NEXT RECORD AT END MOVE 'Y' TO EOF-FLAG NOT AT END DISPLAY "Product: " PRODUCT-NAME " Price: $" PRODUCT-PRICE END-READ END-PERFORM.
Sequential
File Processing
Iteration

Comprehensive NEXT Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
IDENTIFICATION DIVISION. PROGRAM-ID. NEXT-PROCESSING-DEMO. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMPLOYEE-FILE ASSIGN TO "EMPLOYEE.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS WS-EMP-STATUS. SELECT INDEXED-CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS CUSTOMER-KEY FILE STATUS IS WS-CUST-STATUS. SELECT SORT-WORK-FILE ASSIGN TO "SORTWORK.TMP". DATA DIVISION. FILE SECTION. FD EMPLOYEE-FILE. 01 EMPLOYEE-RECORD. 05 EMP-ID PIC X(6). 05 EMP-NAME PIC X(25). 05 EMP-DEPARTMENT PIC X(10). 05 EMP-SALARY PIC 9(6)V99. 05 EMP-HIRE-DATE PIC 9(8). FD INDEXED-CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUSTOMER-KEY PIC X(8). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-ADDRESS PIC X(50). 05 CUSTOMER-PHONE PIC X(15). 05 CUSTOMER-BALANCE PIC S9(8)V99. SD SORT-WORK-FILE. 01 SORT-RECORD. 05 SORT-KEY PIC X(10). 05 SORT-DATA PIC X(70). WORKING-STORAGE SECTION. *> File status and control fields 01 WS-FILE-STATUS. 05 WS-EMP-STATUS PIC X(2). 05 WS-CUST-STATUS PIC X(2). 05 WS-EOF-FLAGS. 10 WS-EMP-EOF PIC X VALUE 'N'. 10 WS-CUST-EOF PIC X VALUE 'N'. *> Processing counters and statistics 01 WS-PROCESSING-STATS. 05 WS-RECORDS-READ PIC 9(8) VALUE 0. 05 WS-RECORDS-PROCESSED PIC 9(8) VALUE 0. 05 WS-TOTAL-SALARY PIC 9(12)V99 VALUE 0. 05 WS-DEPARTMENT-COUNT PIC 9(4) VALUE 0. 05 WS-HIGH-EARNERS PIC 9(6) VALUE 0. *> Department analysis 01 WS-DEPARTMENT-ANALYSIS. 05 WS-DEPT-TABLE OCCURS 20 TIMES INDEXED BY DEPT-IDX. 10 WS-DEPT-NAME PIC X(10). 10 WS-DEPT-COUNT PIC 9(4) VALUE 0. 10 WS-DEPT-TOTAL PIC 9(10)V99 VALUE 0. 10 WS-DEPT-AVG PIC 9(8)V99 VALUE 0. *> Search and comparison fields 01 WS-SEARCH-FIELDS. 05 WS-TARGET-DEPT PIC X(10). 05 WS-SALARY-THRESHOLD PIC 9(6)V99 VALUE 75000.00. 05 WS-CURRENT-DEPT PIC X(10). 05 WS-FOUND-FLAG PIC X VALUE 'N'. *> Customer processing fields 01 WS-CUSTOMER-PROCESSING. 05 WS-CUSTOMER-COUNT PIC 9(6) VALUE 0. 05 WS-BALANCE-TOTAL PIC S9(12)V99 VALUE 0. 05 WS-NEGATIVE-BALANCES PIC 9(4) VALUE 0. 05 WS-CREDIT-CUSTOMERS PIC 9(6) VALUE 0. PROCEDURE DIVISION. MAIN-NEXT-DEMO. DISPLAY "=== COBOL NEXT PROCESSING DEMONSTRATION ===" DISPLAY SPACES PERFORM DEMONSTRATE-SEQUENTIAL-NEXT PERFORM DEMONSTRATE-INDEXED-NEXT PERFORM DEMONSTRATE-DEPARTMENT-ANALYSIS PERFORM DEMONSTRATE-CONDITIONAL-NEXT PERFORM DEMONSTRATE-CUSTOMER-BALANCE-PROCESSING PERFORM DEMONSTRATE-SEARCH-WITH-NEXT DISPLAY "=== NEXT PROCESSING DEMO COMPLETE ===" STOP RUN. DEMONSTRATE-SEQUENTIAL-NEXT. DISPLAY "=== SEQUENTIAL FILE PROCESSING WITH NEXT ===" DISPLAY SPACES DISPLAY "Processing employee file sequentially..." OPEN INPUT EMPLOYEE-FILE IF WS-EMP-STATUS NOT = "00" DISPLAY "Error opening employee file: " WS-EMP-STATUS GO TO END-SEQUENTIAL-DEMO END-IF *> Read first record READ EMPLOYEE-FILE NEXT RECORD AT END DISPLAY "Employee file is empty" MOVE 'Y' TO WS-EMP-EOF NOT AT END ADD 1 TO WS-RECORDS-READ DISPLAY "First employee: " EMP-NAME " (ID: " EMP-ID ")" END-READ *> Process remaining records using NEXT PERFORM UNTIL WS-EMP-EOF = 'Y' *> Process current record PERFORM PROCESS-EMPLOYEE-RECORD *> Read next record READ EMPLOYEE-FILE NEXT RECORD AT END MOVE 'Y' TO WS-EMP-EOF DISPLAY "End of employee file reached" NOT AT END ADD 1 TO WS-RECORDS-READ END-READ END-PERFORM CLOSE EMPLOYEE-FILE DISPLAY "Sequential processing summary:" DISPLAY " Total records read: " WS-RECORDS-READ DISPLAY " Records processed: " WS-RECORDS-PROCESSED DISPLAY " Total payroll: $" WS-TOTAL-SALARY DISPLAY " High earners (>$75K): " WS-HIGH-EARNERS DISPLAY SPACES END-SEQUENTIAL-DEMO. CONTINUE. PROCESS-EMPLOYEE-RECORD. ADD 1 TO WS-RECORDS-PROCESSED ADD EMP-SALARY TO WS-TOTAL-SALARY *> Check for high earners IF EMP-SALARY > WS-SALARY-THRESHOLD ADD 1 TO WS-HIGH-EARNERS DISPLAY " High earner: " EMP-NAME " - $" EMP-SALARY END-IF *> Update department statistics PERFORM UPDATE-DEPARTMENT-STATS. UPDATE-DEPARTMENT-STATS. SET DEPT-IDX TO 1 MOVE 'N' TO WS-FOUND-FLAG *> Search for existing department PERFORM UNTIL DEPT-IDX > 20 OR WS-FOUND-FLAG = 'Y' IF WS-DEPT-NAME(DEPT-IDX) = EMP-DEPARTMENT MOVE 'Y' TO WS-FOUND-FLAG ADD 1 TO WS-DEPT-COUNT(DEPT-IDX) ADD EMP-SALARY TO WS-DEPT-TOTAL(DEPT-IDX) ELSE IF WS-DEPT-NAME(DEPT-IDX) = SPACES MOVE EMP-DEPARTMENT TO WS-DEPT-NAME(DEPT-IDX) MOVE 1 TO WS-DEPT-COUNT(DEPT-IDX) MOVE EMP-SALARY TO WS-DEPT-TOTAL(DEPT-IDX) MOVE 'Y' TO WS-FOUND-FLAG ADD 1 TO WS-DEPARTMENT-COUNT END-IF END-IF SET DEPT-IDX UP BY 1 END-PERFORM. DEMONSTRATE-INDEXED-NEXT. DISPLAY "=== INDEXED FILE PROCESSING WITH NEXT ===" DISPLAY SPACES DISPLAY "Processing indexed customer file sequentially..." OPEN INPUT INDEXED-CUSTOMER-FILE IF WS-CUST-STATUS NOT = "00" DISPLAY "Error opening customer file: " WS-CUST-STATUS GO TO END-INDEXED-DEMO END-IF *> Read records in key sequence using NEXT READ INDEXED-CUSTOMER-FILE NEXT RECORD AT END DISPLAY "Customer file is empty" MOVE 'Y' TO WS-CUST-EOF NOT AT END ADD 1 TO WS-CUSTOMER-COUNT DISPLAY "First customer: " CUSTOMER-NAME " (Key: " CUSTOMER-KEY ")" END-READ PERFORM UNTIL WS-CUST-EOF = 'Y' *> Process customer record PERFORM PROCESS-CUSTOMER-RECORD *> Read next record in key sequence READ INDEXED-CUSTOMER-FILE NEXT RECORD AT END MOVE 'Y' TO WS-CUST-EOF DISPLAY "End of customer file reached" NOT AT END ADD 1 TO WS-CUSTOMER-COUNT END-READ END-PERFORM CLOSE INDEXED-CUSTOMER-FILE DISPLAY "Indexed file processing summary:" DISPLAY " Customers processed: " WS-CUSTOMER-COUNT DISPLAY " Total balance: $" WS-BALANCE-TOTAL DISPLAY " Negative balances: " WS-NEGATIVE-BALANCES DISPLAY " Credit customers: " WS-CREDIT-CUSTOMERS DISPLAY SPACES END-INDEXED-DEMO. CONTINUE. PROCESS-CUSTOMER-RECORD. ADD CUSTOMER-BALANCE TO WS-BALANCE-TOTAL IF CUSTOMER-BALANCE < 0 ADD 1 TO WS-NEGATIVE-BALANCES DISPLAY " Overdrawn: " CUSTOMER-NAME " - $" CUSTOMER-BALANCE ELSE ADD 1 TO WS-CREDIT-CUSTOMERS IF CUSTOMER-BALANCE > 10000 DISPLAY " High-value customer: " CUSTOMER-NAME " - $" CUSTOMER-BALANCE END-IF END-IF. DEMONSTRATE-DEPARTMENT-ANALYSIS. DISPLAY "=== DEPARTMENT ANALYSIS USING NEXT ===" DISPLAY SPACES DISPLAY "Analyzing department statistics..." SET DEPT-IDX TO 1 PERFORM UNTIL DEPT-IDX > WS-DEPARTMENT-COUNT *> Calculate average salary for department IF WS-DEPT-COUNT(DEPT-IDX) > 0 COMPUTE WS-DEPT-AVG(DEPT-IDX) = WS-DEPT-TOTAL(DEPT-IDX) / WS-DEPT-COUNT(DEPT-IDX) END-IF DISPLAY "Department: " WS-DEPT-NAME(DEPT-IDX) DISPLAY " Employees: " WS-DEPT-COUNT(DEPT-IDX) DISPLAY " Total payroll: $" WS-DEPT-TOTAL(DEPT-IDX) DISPLAY " Average salary: $" WS-DEPT-AVG(DEPT-IDX) DISPLAY " " SET DEPT-IDX UP BY 1 END-PERFORM DISPLAY "Total departments processed: " WS-DEPARTMENT-COUNT DISPLAY SPACES. DEMONSTRATE-CONDITIONAL-NEXT. DISPLAY "=== CONDITIONAL PROCESSING WITH NEXT ===" DISPLAY SPACES DISPLAY "Processing with conditional logic..." OPEN INPUT EMPLOYEE-FILE MOVE 'N' TO WS-EMP-EOF MOVE 0 TO WS-RECORDS-READ MOVE "IT" TO WS-TARGET-DEPT *> Process only IT department employees READ EMPLOYEE-FILE NEXT RECORD AT END MOVE 'Y' TO WS-EMP-EOF END-READ PERFORM UNTIL WS-EMP-EOF = 'Y' ADD 1 TO WS-RECORDS-READ *> Process only if matches target department IF EMP-DEPARTMENT = WS-TARGET-DEPT DISPLAY " IT Employee: " EMP-NAME " - $" EMP-SALARY ADD 1 TO WS-RECORDS-PROCESSED END-IF *> Always read next record READ EMPLOYEE-FILE NEXT RECORD AT END MOVE 'Y' TO WS-EMP-EOF END-READ END-PERFORM CLOSE EMPLOYEE-FILE DISPLAY "Conditional processing results:" DISPLAY " Total records scanned: " WS-RECORDS-READ DISPLAY " " WS-TARGET-DEPT " employees found: " WS-RECORDS-PROCESSED DISPLAY SPACES. DEMONSTRATE-CUSTOMER-BALANCE-PROCESSING. DISPLAY "=== CUSTOMER BALANCE PROCESSING ===" DISPLAY SPACES DISPLAY "Processing customers by balance ranges..." OPEN INPUT INDEXED-CUSTOMER-FILE MOVE 'N' TO WS-CUST-EOF MOVE 0 TO WS-CUSTOMER-COUNT 01 WS-BALANCE-RANGES. 05 WS-NEGATIVE-COUNT PIC 9(4) VALUE 0. 05 WS-LOW-BALANCE PIC 9(4) VALUE 0. *> 0-999 05 WS-MEDIUM-BALANCE PIC 9(4) VALUE 0. *> 1000-9999 05 WS-HIGH-BALANCE PIC 9(4) VALUE 0. *> 10000+ READ INDEXED-CUSTOMER-FILE NEXT RECORD AT END MOVE 'Y' TO WS-CUST-EOF END-READ PERFORM UNTIL WS-CUST-EOF = 'Y' ADD 1 TO WS-CUSTOMER-COUNT *> Categorize by balance range EVALUATE TRUE WHEN CUSTOMER-BALANCE < 0 ADD 1 TO WS-NEGATIVE-COUNT DISPLAY " Overdrawn: " CUSTOMER-NAME WHEN CUSTOMER-BALANCE < 1000 ADD 1 TO WS-LOW-BALANCE WHEN CUSTOMER-BALANCE < 10000 ADD 1 TO WS-MEDIUM-BALANCE WHEN OTHER ADD 1 TO WS-HIGH-BALANCE DISPLAY " Premium: " CUSTOMER-NAME " - $" CUSTOMER-BALANCE END-EVALUATE READ INDEXED-CUSTOMER-FILE NEXT RECORD AT END MOVE 'Y' TO WS-CUST-EOF END-READ END-PERFORM CLOSE INDEXED-CUSTOMER-FILE DISPLAY "Balance distribution:" DISPLAY " Negative balances: " WS-NEGATIVE-COUNT DISPLAY " Low balances (0-999): " WS-LOW-BALANCE DISPLAY " Medium balances (1K-9.9K): " WS-MEDIUM-BALANCE DISPLAY " High balances (10K+): " WS-HIGH-BALANCE DISPLAY " Total customers: " WS-CUSTOMER-COUNT DISPLAY SPACES. DEMONSTRATE-SEARCH-WITH-NEXT. DISPLAY "=== SEARCH OPERATIONS WITH NEXT CONCEPT ===" DISPLAY SPACES DISPLAY "Department search and analysis..." *> Search for specific department MOVE "SALES" TO WS-TARGET-DEPT SET DEPT-IDX TO 1 MOVE 'N' TO WS-FOUND-FLAG SEARCH WS-DEPT-TABLE AT END DISPLAY "Department " WS-TARGET-DEPT " not found" WHEN WS-DEPT-NAME(DEPT-IDX) = WS-TARGET-DEPT MOVE 'Y' TO WS-FOUND-FLAG DISPLAY "Found department: " WS-DEPT-NAME(DEPT-IDX) DISPLAY " Employees: " WS-DEPT-COUNT(DEPT-IDX) DISPLAY " Average salary: $" WS-DEPT-AVG(DEPT-IDX) END-SEARCH *> Display all departments using index advancement (NEXT concept) DISPLAY "All departments (using index advancement):" SET DEPT-IDX TO 1 PERFORM UNTIL DEPT-IDX > WS-DEPARTMENT-COUNT IF WS-DEPT-NAME(DEPT-IDX) NOT = SPACES DISPLAY " " WS-DEPT-NAME(DEPT-IDX) " (" WS-DEPT-COUNT(DEPT-IDX) " employees)" END-IF SET DEPT-IDX UP BY 1 *> Advance to next (similar to NEXT concept) END-PERFORM DISPLAY SPACES DISPLAY "NEXT processing benefits demonstrated:" DISPLAY " ✓ Sequential record advancement" DISPLAY " ✓ Predictable file traversal" DISPLAY " ✓ Ordered data processing" DISPLAY " ✓ Efficient iteration control" DISPLAY " ✓ Consistent file navigation" DISPLAY SPACES.

NEXT Processing Features

Sequential Access
  • • Ordered record retrieval
  • • Predictable file traversal
  • • Automatic positioning advancement
  • • End-of-file detection
File Types Support
  • • Sequential files
  • • Indexed files (key sequence)
  • • Relative files (record sequence)
  • • Sort files and work files

Interactive Tutorial

Hands-On Exercise: Sequential Report Processing
Practice using NEXT for comprehensive file processing and reporting

Exercise 1: Sales Report Generation

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
01 SALES-RECORD. 05 SALE-DATE PIC 9(8). 05 SALESPERSON PIC X(20). 05 PRODUCT-CODE PIC X(10). 05 QUANTITY-SOLD PIC 9(4). 05 SALE-AMOUNT PIC 9(8)V99. 01 WS-REPORT-TOTALS. 05 WS-DAILY-SALES PIC 9(10)V99 VALUE 0. 05 WS-TOTAL-QUANTITY PIC 9(8) VALUE 0. 05 WS-RECORD-COUNT PIC 9(6) VALUE 0. 05 WS-CURRENT-DATE PIC 9(8). PROCEDURE DIVISION. GENERATE-SALES-REPORT. OPEN INPUT SALES-FILE *> Read first record READ SALES-FILE NEXT RECORD AT END DISPLAY "No sales data available" GO TO CLOSE-SALES-REPORT NOT AT END MOVE SALE-DATE TO WS-CURRENT-DATE DISPLAY "Sales Report for " WS-CURRENT-DATE DISPLAY "=================================" END-READ *> Process all records sequentially PERFORM UNTIL EOF-FLAG = 'Y' *> Process current sale record ADD SALE-AMOUNT TO WS-DAILY-SALES ADD QUANTITY-SOLD TO WS-TOTAL-QUANTITY ADD 1 TO WS-RECORD-COUNT DISPLAY SALESPERSON " - " PRODUCT-CODE " Qty:" QUANTITY-SOLD " $" SALE-AMOUNT *> Read next record READ SALES-FILE NEXT RECORD AT END MOVE 'Y' TO EOF-FLAG NOT AT END CONTINUE END-READ END-PERFORM *> Display totals DISPLAY " " DISPLAY "Report Summary:" DISPLAY " Total sales: $" WS-DAILY-SALES DISPLAY " Units sold: " WS-TOTAL-QUANTITY DISPLAY " Transactions: " WS-RECORD-COUNT CLOSE-SALES-REPORT. CLOSE SALES-FILE.

Exercise 2: Inventory Audit System

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
01 INVENTORY-RECORD. 05 ITEM-CODE PIC X(12). 05 ITEM-DESCRIPTION PIC X(30). 05 QUANTITY-ON-HAND PIC 9(6). 05 UNIT-COST PIC 9(6)V99. 05 REORDER-POINT PIC 9(4). 05 LAST-ACTIVITY PIC 9(8). 01 WS-AUDIT-SUMMARY. 05 WS-ITEMS-AUDITED PIC 9(6) VALUE 0. 05 WS-LOW-STOCK PIC 9(4) VALUE 0. 05 WS-ZERO-STOCK PIC 9(4) VALUE 0. 05 WS-TOTAL-VALUE PIC 9(12)V99 VALUE 0. 05 WS-ITEM-VALUE PIC 9(10)V99. PROCEDURE DIVISION. INVENTORY-AUDIT. OPEN INPUT INVENTORY-FILE DISPLAY "Inventory Audit Report" DISPLAY "======================" *> Read first inventory record READ INVENTORY-FILE NEXT RECORD AT END DISPLAY "No inventory data found" GO TO END-AUDIT END-READ *> Process all inventory items PERFORM UNTIL EOF-FLAG = 'Y' ADD 1 TO WS-ITEMS-AUDITED *> Calculate item value COMPUTE WS-ITEM-VALUE = QUANTITY-ON-HAND * UNIT-COST ADD WS-ITEM-VALUE TO WS-TOTAL-VALUE *> Check stock levels EVALUATE TRUE WHEN QUANTITY-ON-HAND = 0 ADD 1 TO WS-ZERO-STOCK DISPLAY "⚠️ OUT OF STOCK: " ITEM-DESCRIPTION WHEN QUANTITY-ON-HAND <= REORDER-POINT ADD 1 TO WS-LOW-STOCK DISPLAY "📦 LOW STOCK: " ITEM-DESCRIPTION " (Qty: " QUANTITY-ON-HAND ")" WHEN OTHER DISPLAY "✓ " ITEM-DESCRIPTION " Qty:" QUANTITY-ON-HAND " Value:$" WS-ITEM-VALUE END-EVALUATE *> Read next inventory record READ INVENTORY-FILE NEXT RECORD AT END MOVE 'Y' TO EOF-FLAG END-READ END-PERFORM *> Display audit summary DISPLAY " " DISPLAY "Audit Summary:" DISPLAY " Items audited: " WS-ITEMS-AUDITED DISPLAY " Out of stock: " WS-ZERO-STOCK DISPLAY " Low stock alerts: " WS-LOW-STOCK DISPLAY " Total inventory value: $" WS-TOTAL-VALUE END-AUDIT. CLOSE INVENTORY-FILE.

Best Practices

Knowledge Check

Test Your Understanding

Question 1: READ NEXT Purpose

What is the primary purpose of using READ NEXT in COBOL?

Answer: READ NEXT advances file positioning to retrieve the next sequential record from a file. It ensures predictable forward movement through the file, whether processing sequential files in order or indexed files in key sequence, providing reliable sequential access patterns.

Question 2: File Types

Which file types support READ NEXT operations?

Answer: READ NEXT works with sequential files (in physical order), indexed files (in key sequence when using sequential or dynamic access), and relative files (in record number order). It's particularly useful for ordered processing of any sequentially accessible file.

Question 3: Error Handling

How should you handle end-of-file conditions with READ NEXT?

Answer: Always use AT END clause with READ NEXT to detect when no more records are available. Check file status codes for error conditions, and implement appropriate loop termination logic to prevent infinite loops when processing sequential data.

Related Pages