MainframeMaster

COBOL Tutorial

COBOL WORKING-STORAGE SECTION - Quick Reference

Progress0 of 0 lessons

Overview

WORKING-STORAGE SECTION is a section within the DATA DIVISION of a COBOL program that defines temporary variables and data items used during program execution. It provides a place to declare variables, constants, and data structures that are needed throughout the program but are not part of file records.

Key Features

  • Temporary variables - Define variables used during program execution
  • Program-level scope - Variables accessible throughout the program
  • Multiple data types - Support for numeric, alphanumeric, and group variables
  • Initialization - Variables can be initialized with default values

Syntax and Usage

WORKING-STORAGE SECTION syntax and usage patterns for different data definition scenarios.

Basic WORKING-STORAGE SECTION

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
IDENTIFICATION DIVISION. PROGRAM-ID. WORKING-STORAGE-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 COUNTER PIC 9(3) VALUE 0. 01 TOTAL-AMOUNT PIC 9(8)V99 VALUE 0.00. 01 CUSTOMER-NAME PIC X(30) VALUE SPACES. 01 PROCESSING-FLAG PIC X VALUE "N". PROCEDURE DIVISION. DISPLAY "Counter: " COUNTER DISPLAY "Total Amount: " TOTAL-AMOUNT DISPLAY "Customer Name: " CUSTOMER-NAME DISPLAY "Processing Flag: " PROCESSING-FLAG STOP RUN.

Basic WORKING-STORAGE SECTION with various data types and initialization.

WORKING-STORAGE with Group Items

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
IDENTIFICATION DIVISION. PROGRAM-ID. WORKING-STORAGE-GROUP. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(6) VALUE 0. 05 CUSTOMER-NAME PIC X(30) VALUE SPACES. 05 CUSTOMER-STATUS PIC X(1) VALUE "A". 05 CUSTOMER-BALANCE PIC 9(8)V99 VALUE 0.00. 01 PROCESSING-STATS. 05 RECORDS-PROCESSED PIC 9(6) VALUE 0. 05 RECORDS-ERROR PIC 9(6) VALUE 0. 05 TOTAL-AMOUNT PIC 9(10)V99 VALUE 0.00. PROCEDURE DIVISION. DISPLAY "Customer ID: " CUSTOMER-ID DISPLAY "Customer Name: " CUSTOMER-NAME DISPLAY "Records Processed: " RECORDS-PROCESSED STOP RUN.

WORKING-STORAGE SECTION with group items for structured data organization.

WORKING-STORAGE with Arrays

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
IDENTIFICATION DIVISION. PROGRAM-ID. WORKING-STORAGE-ARRAY. DATA DIVISION. WORKING-STORAGE SECTION. 01 MONTHLY-TOTALS. 05 MONTH-AMOUNT PIC 9(8)V99 OCCURS 12 TIMES. 01 PROCESSING-ARRAYS. 05 TEMP-VALUES PIC 9(3) OCCURS 10 TIMES VALUE 0. 05 STATUS-FLAGS PIC X OCCURS 5 TIMES VALUE "N". 01 COUNTERS. 05 ARRAY-INDEX PIC 9(2) VALUE 1. 05 LOOP-COUNT PIC 9(3) VALUE 0. PROCEDURE DIVISION. PERFORM VARYING ARRAY-INDEX FROM 1 TO 10 MOVE ARRAY-INDEX TO TEMP-VALUES(ARRAY-INDEX) END-PERFORM DISPLAY "Array processing completed" STOP RUN.

WORKING-STORAGE SECTION with arrays and tables using OCCURS clause.

Data Types

Numeric Variables

Integer and decimal numeric data items.

cobol
1
2
3
01 COUNTER PIC 9(3) VALUE 0. 01 AMOUNT PIC 9(8)V99 VALUE 0.00. 01 PERCENTAGE PIC 9(3)V99 VALUE 0.00.

Alphanumeric Variables

Text and character data items.

cobol
1
2
3
01 NAME PIC X(30) VALUE SPACES. 01 DESCRIPTION PIC X(100) VALUE SPACES. 01 CODE PIC X(5) VALUE "00000".

Group Variables

Structured data items with multiple fields.

cobol
1
2
3
4
01 RECORD-ITEM. 05 ID PIC 9(6) VALUE 0. 05 NAME PIC X(30) VALUE SPACES. 05 STATUS PIC X(1) VALUE "A".

Array Variables

Tables and arrays using OCCURS clause.

cobol
1
2
3
4
01 ARRAY-DATA. 05 ELEMENT PIC 9(3) OCCURS 10 TIMES. 01 TABLE-DATA. 05 ROW PIC X(20) OCCURS 5 TIMES.

Best Practices

  • Initialize variables - Always initialize WORKING-STORAGE variables with appropriate default values
  • Use meaningful names - Choose descriptive names for variables that clearly indicate their purpose
  • Group related data - Organize related variables into group items for better structure
  • Document variables - Add comments explaining the purpose of complex variables
  • Consider memory usage - Be mindful of memory allocation for large arrays or complex structures

WORKING-STORAGE SECTION Quick Reference

Data TypeSyntaxUse Case
NumericPIC 9(n) VALUE numberCounters, amounts, calculations
AlphanumericPIC X(n) VALUE "text"Names, descriptions, codes
Group01 group-name. 05 field-nameStructured data, records
ArrayPIC type OCCURS n TIMESTables, lists, collections

Test Your Knowledge

1. What is the primary purpose of WORKING-STORAGE SECTION in COBOL?

  • To define file structures
  • To define temporary variables and data items used during program execution
  • To define external data
  • To define program parameters

2. Where is WORKING-STORAGE SECTION located in a COBOL program?

  • In the IDENTIFICATION DIVISION
  • In the ENVIRONMENT DIVISION
  • In the DATA DIVISION
  • In the PROCEDURE DIVISION

3. What happens to WORKING-STORAGE variables when the program starts?

  • They are automatically initialized to zero
  • They contain undefined values unless explicitly initialized
  • They are automatically initialized to spaces
  • They are automatically initialized to high values

4. Which of the following can be defined in WORKING-STORAGE SECTION?

  • Only numeric variables
  • Only alphanumeric variables
  • Numeric, alphanumeric, and group variables
  • Only file descriptions

5. How do you initialize a variable in WORKING-STORAGE SECTION?

  • Using the INITIALIZE statement
  • Using the VALUE clause
  • Both A and B
  • Using the MOVE statement only

Frequently Asked Questions