The ENVIRONMENT DIVISION is the second division in a COBOL program and describes the computing environment in which the program will run. It establishes the connection between the logical constructs of the program and the physical aspects of the computing environment.
This division is optional in COBOL programs that don't use files or special hardware devices, but it's commonly included in business applications that process data files. The ENVIRONMENT DIVISION is divided into two main sections:
Specifies the source and object computers and defines special names.
Defines files and external devices used by the program.
12345678910111213ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-Z. OBJECT-COMPUTER. IBM-Z. SPECIAL-NAMES. CURRENCY SIGN IS '$'. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO CUSTFILE ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS CUSTOMER-ID.
The example above shows a typical ENVIRONMENT DIVISION with both sections.
The CONFIGURATION SECTION specifies information about the computer environment where the program is compiled and executed. It also allows the programmer to define special features of the environment.
Identifies the computer on which the program is compiled:
1SOURCE-COMPUTER. computer-name [WITH DEBUGGING MODE].
The WITH DEBUGGING MODE
clause activates debugging lines in the source code (lines with 'D' in column 7).
Identifies the computer on which the program will be executed:
1234OBJECT-COMPUTER. computer-name [MEMORY SIZE IS integer {WORDS|CHARACTERS|MODULES}] [PROGRAM COLLATING SEQUENCE IS alphabet-name] [SEGMENT-LIMIT IS segment-number].
MEMORY SIZE
- Rarely used in modern COBOL; historically specified available memoryPROGRAM COLLATING SEQUENCE
- Specifies the collating sequence for comparisonsSEGMENT-LIMIT
- Historical feature related to overlay structuresEstablishes relationships between system names and user-defined names, and defines special features:
123456SPECIAL-NAMES. CURRENCY SIGN IS '€' DECIMAL-POINT IS COMMA CONSOLE IS CRT CLASS AlphaOnly IS 'A' THRU 'Z', 'a' THRU 'z' SYMBOLIC CHARACTERS StarChar IS 42.
In modern COBOL applications, the CONFIGURATION SECTION is often simplified. Many compilers provide default values for most configuration options, and many of the historical features are rarely used. However, the SPECIAL-NAMES paragraph remains important for internationalizing applications.
1234567CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-Z WITH DEBUGGING MODE. OBJECT-COMPUTER. IBM-Z PROGRAM COLLATING SEQUENCE IS EBCDIC. SPECIAL-NAMES. CURRENCY SIGN IS '£' DECIMAL-POINT IS COMMA.
This example shows a CONFIGURATION SECTION for a program that will use the pound sign (£) as the currency symbol and comma as the decimal point, typical for UK-based financial applications.
The INPUT-OUTPUT SECTION is the most frequently used part of the ENVIRONMENT DIVISION. It defines the files and devices that the program will use and specifies how they are connected to the external environment.
The FILE-CONTROL paragraph contains SELECT statements that define each file used by the program:
12345678FILE-CONTROL. SELECT [OPTIONAL] file-name ASSIGN TO external-name [ORGANIZATION IS {SEQUENTIAL|INDEXED|RELATIVE}] [ACCESS MODE IS {SEQUENTIAL|RANDOM|DYNAMIC}] [RECORD KEY IS data-name] [ALTERNATE RECORD KEY IS data-name [WITH DUPLICATES]] [FILE STATUS IS data-name].
SELECT
- Associates an internal file name (used in the program) with an external fileASSIGN TO
- Specifies the external file name or deviceORGANIZATION IS
- Defines the file organization (SEQUENTIAL, INDEXED, or RELATIVE)ACCESS MODE IS
- Specifies how records will be accessed (SEQUENTIAL, RANDOM, or DYNAMIC)RECORD KEY IS
- Identifies the key field for indexed filesALTERNATE RECORD KEY IS
- Defines additional keys for indexed filesFILE STATUS IS
- Names a two-character data item that will contain the file operation status code12345SELECT TRANSACTION-FILE ASSIGN TO TRANFILE ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS WS-TRANS-STATUS.
1234567SELECT CUSTOMER-MASTER ASSIGN TO CUSTMAST ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS CUST-ID ALTERNATE RECORD KEY IS CUST-NAME WITH DUPLICATES FILE STATUS IS WS-CUST-STATUS.
The I-O-CONTROL paragraph specifies input-output techniques for the program:
1234I-O-CONTROL. RERUN ON {file-name|implementor-name} EVERY {integer RECORDS|END OF REEL|integer CLOCK-UNITS} SAME {RECORD|SORT|SORT-MERGE} AREA FOR file-name-1 file-name-2 MULTIPLE FILE TAPE CONTAINS file-name-1 POSITION integer-1 [file-name-2 POSITION integer-2].
This paragraph is less commonly used in modern COBOL programs. Its main functions include:
RERUN
- Establishes checkpoints for program restartSAME AREA
- Specifies that multiple files share the same memory areaMULTIPLE FILE
- Historical feature for managing tape filesThe ORGANIZATION clause in the SELECT statement defines how a file is structured. Understanding these organizations is essential for proper file handling in COBOL.
Records are stored in physical order, one after another:
Records are referenced through an index based on key values:
Records are identified by their position in the file:
The ACCESS MODE clause specifies how records will be retrieved from or stored into a file. The choice of access mode depends on the file organization and application requirements.
Records are accessed in the order they appear in the file:
Records are accessed directly based on key value or record number:
Combines capabilities of sequential and random access:
12345678910111213FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO CUSTFILE ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS CUSTOMER-ID ALTERNATE RECORD KEY IS CUSTOMER-NAME WITH DUPLICATES FILE STATUS IS WS-FILE-STATUS. SELECT REPORT-FILE ASSIGN TO PRINTER ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL.
This example shows a FILE-CONTROL paragraph with two SELECT statements: one for an indexed customer file with dynamic access, and one for a sequential report file.
The implementation of the ENVIRONMENT DIVISION can vary significantly across different COBOL compilers and platforms. These variations primarily affect how files are defined and accessed.
IBM mainframe environments have specific considerations:
12SELECT CUSTOMER-FILE ASSIGN TO CUSTFILE *> References DD name in JCL
Distributed platforms often use different approaches:
12SELECT CUSTOMER-FILE ASSIGN TO "/data/customers.dat" *> Direct file path reference
Micro Focus has its own conventions for file assignments:
12SELECT CUSTOMER-FILE ASSIGN TO EXTERNAL CUSTFILE *> Name resolved at runtime
GnuCOBOL uses its own approach to file assignments:
12SELECT CUSTOMER-FILE ASSIGN TO DYNAMIC WS-FILENAME *> Filename in a variable
When writing portable COBOL code, it's important to understand these platform differences. Many organizations use environment variables or configuration files to manage file assignments across different environments, allowing the same COBOL program to run without modification.
Write an ENVIRONMENT DIVISION for a COBOL program with the following requirements:
Try to follow the best practices discussed in this lesson.
1. Which of the following sections is part of the ENVIRONMENT DIVISION?
2. What clause would you use in a SELECT statement to specify how records will be accessed?
3. Which file organization allows direct access to records based on key values?
4. What is the purpose of the FILE STATUS clause in a SELECT statement?
5. Write a SELECT statement for a sequential report file that will be sent to a printer.
No, the ENVIRONMENT DIVISION is not mandatory. It can be omitted if the program does not use files or special devices. However, most business COBOL programs process data files, so the ENVIRONMENT DIVISION is commonly included to define these files.
The ASSIGN clause in the SELECT statement links the internal file name to an external name. On mainframes, this typically refers to a DD name in JCL. On distributed systems, it might be a file path or environment variable. Many COBOL compilers allow for runtime assignment using environment variables or configuration files, enabling the same program to run in different environments without code changes.
VSAM (Virtual Storage Access Method) is IBM's file management system on mainframes, while COBOL file organizations are logical concepts. They map as follows: KSDS (Key Sequenced Data Set) corresponds to INDEXED organization, ESDS (Entry Sequenced Data Set) to SEQUENTIAL organization, and RRDS (Relative Record Data Set) to RELATIVE organization. COBOL provides a consistent programming interface regardless of the underlying implementation.
Use DYNAMIC access mode when your program needs the flexibility to switch between sequential and random access within the same program. This is common in applications that need to browse through records sequentially but also need to look up specific records by key. Since DYNAMIC access provides all capabilities of both SEQUENTIAL and RANDOM access, it's often the most versatile choice for indexed and relative files.