The SET statement is used to assign values to data items and control program flow in COBOL. It is commonly used for setting flags, indexes, boolean values, and controlling various aspects of program execution.
The SET statement is used in the PROCEDURE DIVISION for value assignment and flow control.
123456789101112131415161718192021222324* Basic SET syntax SET data-item TO value SET data-item-1 data-item-2... TO value * Complete example IDENTIFICATION DIVISION. PROGRAM-ID. SET-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 FLAGS. 05 PROCESSING-FLAG PIC X VALUE 'N'. 05 ERROR-FLAG PIC X VALUE 'N'. 01 COUNTERS. 05 LOOP-COUNTER PIC 9(3) VALUE 0. 05 RECORD-COUNTER PIC 9(5) VALUE 0. PROCEDURE DIVISION. MAIN-LOGIC. SET PROCESSING-FLAG TO 'Y' SET LOOP-COUNTER TO 1 SET RECORD-COUNTER TO 0 DISPLAY "Processing started" STOP RUN.
SET assigns values to data items and controls program flow.
Examples of using the SET statement in different scenarios.
123456789101112131415161718192021* Set boolean flags 01 PROGRAM-FLAGS. 05 DATA-VALID-FLAG PIC X VALUE 'N'. 05 END-OF-FILE-FLAG PIC X VALUE 'N'. 05 ERROR-FLAG PIC X VALUE 'N'. PROCEDURE DIVISION. MANAGE-FLAGS. * Set flags for different conditions SET DATA-VALID-FLAG TO 'Y' SET END-OF-FILE-FLAG TO 'N' SET ERROR-FLAG TO 'N' * Use flags in conditional logic IF DATA-VALID-FLAG = 'Y' PERFORM PROCESS-DATA END-IF IF END-OF-FILE-FLAG = 'Y' PERFORM CLOSE-FILES END-IF.
SET for flag management and control.
12345678910111213141516171819202122232425* Initialize counters and accumulators 01 COUNTERS. 05 RECORD-COUNT PIC 9(5) VALUE 0. 05 ERROR-COUNT PIC 9(3) VALUE 0. 05 SUCCESS-COUNT PIC 9(5) VALUE 0. PROCEDURE DIVISION. INITIALIZE-COUNTERS. * Reset all counters to zero SET RECORD-COUNT TO 0 SET ERROR-COUNT TO 0 SET SUCCESS-COUNT TO 0 DISPLAY "Counters initialized" * Process records and update counters PERFORM UNTIL END-OF-FILE READ INPUT-FILE ADD 1 TO RECORD-COUNT IF PROCESSING-SUCCESSFUL ADD 1 TO SUCCESS-COUNT ELSE ADD 1 TO ERROR-COUNT END-IF END-PERFORM.
SET for counter initialization and management.
123456789101112131415161718192021* Set index values for table processing 01 TABLE-INDEXES. 05 CURRENT-INDEX PIC 9(3) VALUE 1. 05 MAX-INDEX PIC 9(3) VALUE 100. 05 SEARCH-INDEX PIC 9(3) VALUE 1. PROCEDURE DIVISION. MANAGE-INDEXES. * Initialize indexes SET CURRENT-INDEX TO 1 SET SEARCH-INDEX TO 1 * Process table elements PERFORM UNTIL CURRENT-INDEX > MAX-INDEX PERFORM PROCESS-TABLE-ELEMENT ADD 1 TO CURRENT-INDEX END-PERFORM * Reset for search operation SET SEARCH-INDEX TO 1 PERFORM SEARCH-TABLE.
SET for index management and table processing.
Understanding best practices ensures effective use of the SET statement.
1. What is the primary purpose of the SET statement in COBOL?
2. In which COBOL division is the SET statement typically used?
3. What types of values can be assigned using the SET statement?
4. How does SET differ from MOVE?
5. What is the relationship between SET and END-SET?