MainframeMaster

COBOL Tutorial

COBOL SET Statement - Quick Reference

Progress0 of 0 lessons

Overview

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.

Purpose and Usage

  • Value assignment - Assign values to data items
  • Flag control - Set boolean flags and switches
  • Index management - Set index values
  • Flow control - Control program execution flow
  • Status management - Set status indicators

Syntax

The SET statement is used in the PROCEDURE DIVISION for value assignment and flow control.

Basic Syntax

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
* 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.

Practical Examples

Examples of using the SET statement in different scenarios.

Flag Management

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
* 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.

Counter Initialization

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
* 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.

Index Management

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
* 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.

Best Practices

Understanding best practices ensures effective use of the SET statement.

Best Practices

  • Use meaningful names - Choose descriptive names for flags and counters
  • Initialize properly - Always initialize counters and flags
  • Check values - Validate values before setting them
  • Document usage - Document the purpose of flags and counters
  • Reset when needed - Reset flags and counters when appropriate

Test Your Knowledge

1. What is the primary purpose of the SET statement in COBOL?

  • To set files
  • To assign values to data items and control program flow
  • To set memory
  • To set hardware

2. In which COBOL division is the SET statement typically used?

  • IDENTIFICATION DIVISION
  • ENVIRONMENT DIVISION
  • DATA DIVISION
  • PROCEDURE DIVISION

3. What types of values can be assigned using the SET statement?

  • Only numeric values
  • Only character values
  • Numeric, character, and boolean values
  • Only file values

4. How does SET differ from MOVE?

  • They are the same thing
  • SET is for flags and indexes, MOVE is for data transfer
  • SET is faster than MOVE
  • SET is obsolete, MOVE is modern

5. What is the relationship between SET and END-SET?

  • They are the same thing
  • SET starts a block, END-SET ends it
  • SET is for input, END-SET is for output
  • They cannot be used together

Frequently Asked Questions