WAIT is a modern COBOL statement that allows programs to pause execution for a specified time period or until a specific event occurs. It provides timing control and synchronization capabilities for coordinating program execution with external events or other processes.
WAIT statement syntax and usage patterns for different timing and synchronization scenarios.
1234567891011IDENTIFICATION DIVISION. PROGRAM-ID. WAIT-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 WAIT-TIME PIC 9(3) VALUE 5. PROCEDURE DIVISION. DISPLAY "Starting program..." WAIT WAIT-TIME SECONDS DISPLAY "Waited for " WAIT-TIME " seconds" STOP RUN.
Basic WAIT statement that pauses execution for a specified number of seconds.
123456789101112131415161718IDENTIFICATION DIVISION. PROGRAM-ID. WAIT-EVENT. DATA DIVISION. WORKING-STORAGE SECTION. 01 EVENT-FLAG PIC X VALUE "N". 01 MAX-WAIT-TIME PIC 9(3) VALUE 30. PROCEDURE DIVISION. DISPLAY "Waiting for event..." WAIT UNTIL EVENT-FLAG = "Y" OR MAX-WAIT-TIME SECONDS IF EVENT-FLAG = "Y" DISPLAY "Event occurred" ELSE DISPLAY "Timeout reached" END-IF STOP RUN.
WAIT statement that waits for an event or times out after a maximum period.
1234567891011121314151617181920212223IDENTIFICATION DIVISION. PROGRAM-ID. WAIT-EXCEPTION. DATA DIVISION. WORKING-STORAGE SECTION. 01 WAIT-DURATION PIC 9(3) VALUE 10. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "Starting timed operation..." WAIT WAIT-DURATION SECONDS ON EXCEPTION DISPLAY "WAIT operation was interrupted" PERFORM HANDLE-INTERRUPTION NOT ON EXCEPTION DISPLAY "WAIT completed successfully" END-WAIT STOP RUN. HANDLE-INTERRUPTION. DISPLAY "Handling interruption..." EXIT.
WAIT statement with exception handling for interrupted operations.
Wait for a specified time period.
1WAIT time-value SECONDS
Wait until a condition becomes true.
1WAIT UNTIL condition
Wait for event with timeout.
1WAIT UNTIL condition OR timeout SECONDS
Handle WAIT interruptions.
123WAIT time-value SECONDS ON EXCEPTION * Handle interruption
Wait Type | Syntax | Use Case |
---|---|---|
Time Delay | WAIT time SECONDS | Simple timing delays |
Event Wait | WAIT UNTIL condition | Wait for specific events |
Timeout Wait | WAIT UNTIL condition OR timeout | Event wait with timeout |
Exception Handling | WAIT time ON EXCEPTION | Handle interruptions |
1. What is the primary purpose of WAIT in COBOL?
2. Which COBOL standard introduced the WAIT statement?
3. What types of WAIT operations are available?
4. How do you specify a time delay with WAIT?
5. What is a common use case for WAIT?