MainframeMaster

COBOL Tutorial

COBOL ENVIRONMENT DIVISION - Quick Reference

Progress0 of 0 lessons

Overview

The ENVIRONMENT DIVISION in COBOL specifies the program's operating environment, including the computer system, configuration, and external files or devices used by the program. It is essential for file handling and for programs that interact with the outside world.

Purpose and Usage

  • Defines system environment (hardware, OS, etc.)
  • Specifies external files and devices for input/output
  • Maps logical file names to physical files
  • Customizes program behavior for different environments
  • Sets special environment options (currency, decimal, etc.)

Syntax

The ENVIRONMENT DIVISION is divided into sections and paragraphs. Here is the typical structure:

Basic Structure

cobol
1
2
3
4
5
6
7
8
9
ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. computer-name. OBJECT-COMPUTER. computer-name. SPECIAL-NAMES. ... INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-name ASSIGN TO external-name ... I-O-CONTROL. ...

Not all paragraphs are required; include only those needed for your program.

Example: Minimal ENVIRONMENT DIVISION

cobol
1
2
3
4
5
6
7
8
ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-370. OBJECT-COMPUTER. IBM-370. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INFILE ASSIGN TO 'INPUT.DAT'. SELECT OUTFILE ASSIGN TO 'OUTPUT.DAT'.

Example: Using SPECIAL-NAMES

cobol
1
2
3
4
5
6
7
ENVIRONMENT DIVISION. CONFIGURATION SECTION. SPECIAL-NAMES. DECIMAL-POINT IS COMMA CURRENCY SIGN IS "$". CLASS ALPHA IS "A" THRU "Z". SYMBOLIC CHARACTERS ARE CR=13, LF=10.

Practical Examples

Here are some practical uses of the ENVIRONMENT DIVISION in COBOL programs:

File Assignment Example

cobol
1
2
3
4
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMPFILE ASSIGN TO 'EMPLOYEE.DAT'.

Maps the logical file EMPFILE to the physical file EMPLOYEE.DAT.

Multiple Files and Devices

cobol
1
2
3
4
5
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT PRINTER ASSIGN TO PRINTER. SELECT TAPEFILE ASSIGN TO TAPE.

Assigns logical files to a printer and a tape device.

Customizing Environment

cobol
1
2
3
4
5
ENVIRONMENT DIVISION. CONFIGURATION SECTION. SPECIAL-NAMES. DECIMAL-POINT IS COMMA CURRENCY SIGN IS "€".

Sets the decimal point to a comma and the currency sign to the Euro symbol.

Best Practices

  • Include only the sections and paragraphs you need for your program.
  • Use meaningful logical file names for clarity.
  • Document any environment-specific settings in SPECIAL-NAMES.
  • Keep the ENVIRONMENT DIVISION organized and easy to read.
  • Test file assignments and device mappings in your target environment.

Common Pitfalls

  • Forgetting to map logical files to physical files (missing ASSIGN TO).
  • Using incorrect device or file names for your operating system.
  • Omitting required paragraphs for file I/O operations.
  • Not updating SPECIAL-NAMES for locale-specific settings.

Test Your Knowledge

1. What is the primary purpose of the ENVIRONMENT DIVISION in COBOL?

  • To define data structures
  • To specify the program environment and I/O devices
  • To write executable logic
  • To declare variables only

2. Which section is NOT part of the ENVIRONMENT DIVISION?

  • CONFIGURATION SECTION
  • DATA DIVISION
  • INPUT-OUTPUT SECTION
  • SPECIAL-NAMES paragraph

3. What is the purpose of the SELECT statement in the ENVIRONMENT DIVISION?

  • To select a paragraph
  • To define a file and its external assignment
  • To select a variable
  • To select a procedure

4. Where do you specify the ASSIGN TO clause in COBOL?

  • In the PROCEDURE DIVISION
  • In the FILE-CONTROL paragraph of the ENVIRONMENT DIVISION
  • In the WORKING-STORAGE SECTION
  • In the SPECIAL-NAMES paragraph

5. What is the SPECIAL-NAMES paragraph used for?

  • To declare special variables
  • To define environment-specific names and settings
  • To specify file organization
  • To define data types

Frequently Asked Questions