ENVIRONMENT DIVISION
- ENVIRONMENT DIVISION is primarily responsible for specifying the computer environment (mostly computer name in mainframe environment) on which the program is compiled and executed.
- In addition, it defines the input and output sources (files) required to run the program and interact with devices like printers, files, etc.
- Entire ENVIRONMENT DIVISION is optional and should be coded as a second division in the program immediately after IDENTIFICATION DIVISION if needed.
Syntax -
ENVIRONMENT DIVISION.
[CONFIGURATION SECTION.
[SOURCE-COMPUTER. source-computer-name]
[OBJECT-COMPUTER. object-computer-name]
[SPECIAL-NAMES. special-names-enties]]
[INPUT-OUTPUT SECTION.
[FILE-CONTROL. file-control-entries]
[I-O-CONTROL. i-o-control-entries]]
ENVIRONMENT DIVISION has two sections -
- CONFIGURATION SECTION.
- INPUT-OUTPUT SECTION.
CONFIGURATION SECTION
It describes the computer environment in which the program is compiled and executed. It is an optional section in the COBOL program.
SOURCE-COMPUTER. {source-computer-entry} -
SOURCE-COMPUTER provides the computer name on which the source program is compiled. All entries of the SOURCE-COMPUTER are for syntax checking and have no impact on the program execution.
- computer-name - Source computer name.
- WITH DEBUGGING MODE - It activates a compile-time debugging switch that enables all debugging lines as code. A debugging line is a code with a "D" in column 7.
Examples -
Scenario1 - Example to enable DEBUGGING MODE.
Code -
----+----1----+----2----+----3----+----4----+
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM3278 WITH DEBUGGING MODE.
PROCEDURE DIVISION.
D DISPLAY "DISPLAYING DEBUGGING LINE".
DISPLAY "DISPLAYING NORMAL LINE".
Output -
DISPLAYING DEBUGGING LINE DISPLAYING NORMAL LINE
Scenario2 - Example to disable DEBUGGING MODE.
Code -
----+----1----+----2----+----3----+----4----+
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM3278.
PROCEDURE DIVISION.
D DISPLAY "DISPLAYING DEBUGGING LINE".
DISPLAY "DISPLAYING NORMAL LINE".
Output -
DISPLAYING NORMAL LINE
OBJECT-COMPUTER. {object-computer-entry} -
The OBJECT-COMPUTER paragraph specifies the system name where the object program is executed. If it is not coded, the program doesn't run on the current machine.
- computer-name - Object computer name.
- PROGRAM COLLATING SEQUENCE IS alphabet-name - A collating sequence is used to change the system collating sequence. If it is not coded, the system's default collating sequence is in effect.
SPECIAL-NAMES. {special-names-entry} -
SPECIAL-NAMES provide symbolic characters and special functions related to the existing mnemonic names in the source program. The constant entries can be created using SPECIAL-NAMES to validate the fields at a program level.
- environment-name-1 - It specifies the system name where the compiler took the actions.
- mnemonic-name-1 - It is a user-defined name.
- ALPHABET clause - It declares a name with a character code or collating sequence.
- SYMBOLIC CHARACTERS clause - It applies a variable to single-byte character sets. Each character represented is an alphanumeric character.
- CURRENCY SIGN clause - It sets the currency symbol in a PICTURE clause.
- DECIMAL-POINT IS COMMA clause - It swaps the functions of the period. Also, changes the comma in PICTURE character-strings and numeric literals.
Example -
Scenario - Changing system default collating sequence.
Code -
----+----1----+----2----+----3----+----4----+
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM3278.
OBJECT-COMPUTER. IBM3278
PROGRAM COLLATING SEQUENCE IS TEST-COLLATE.
SPECIAL-NAMES. ALPHABET TEST-COLLATE IS 'STUVWXY'.
.
.
PROCEDURE DIVISION.
MOVE LOW-VALUE TO WS-VAR.
DISPLAY "LOWEST VALUE IS: " WS-VAR.
Output -
LOWEST VALUE IS: S
INPUT-OUTPUT SECTION
It specifies the input and output sources information that is useful to access the resources outside the program. INPUT-OUTPUT section has two system-defined paragraphs -
- FILE-CONTROL paragraph.
- I-O-CONTROL paragraph.
Syntax -
[Optional] [INPUT-OUTPUT SECTION.
[Optional] [file-control-paragraph]
[Optional] [i-o-control-paragraph]]
FILE-CONTROL Paragraph -
FILE-CONTROL paragraph contains complete information about the files used in the program. It maps the logical file defined in the program to the corresponding physical file in the JCL by using DDname or DSName in CICS. The mapping is shown below -
Explaining all the entries in this paragraph is not needed at this point and is only required while working with files. So, we will discuss this paragraph during the File Declaration topic.
I-O-CONTROL paragraph -
The I-O-CONTROL paragraph specifies the resuming points from where the rerun is to be started and defines the memory area that is shared by different files.
It is optional in a COBOL program. It can appear only once at the beginning. The I-O-CONTROL header should begin in Area-A and end with a period.