MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL STANDARD-2 Clause - Quick Reference

The STANDARD-2 clause in COBOL specifies compliance with the COBOL 2002 standard (ISO/IEC 1989:2002). It enables access to modern COBOL features including built-in functions, enhanced PERFORM statements, improved exception handling, and other advanced capabilities.

Primary Use

Specify COBOL 2002 standard compliance

Division

ENVIRONMENT DIVISION

Section

CONFIGURATION SECTION

Status

Optional clause

Overview

The STANDARD-2 clause is part of the CONFIGURATION SECTION in the ENVIRONMENT DIVISION. It specifies that the program should follow the COBOL 2002 standard (ISO/IEC 1989:2002). This enables access to modern COBOL features including built-in functions, enhanced PERFORM statements, improved exception handling, and other advanced capabilities that were not available in COBOL 85.

Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ENVIRONMENT DIVISION. CONFIGURATION SECTION. STANDARD-2. * Examples: ENVIRONMENT DIVISION. CONFIGURATION SECTION. STANDARD-2. * With other configuration clauses: ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-3090. OBJECT-COMPUTER. IBM-3090. STANDARD-2.

Practical Examples

Basic STANDARD-2 Usage

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
26
27
28
29
30
31
32
* Basic STANDARD-2 clause example (COBOL 2002) IDENTIFICATION DIVISION. PROGRAM-ID. STANDARD-2-EXAMPLE. ENVIRONMENT DIVISION. CONFIGURATION SECTION. STANDARD-2. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-COUNTER PIC 9(3) VALUE ZERO. 01 WS-RESULT PIC 9(5). 01 WS-MESSAGE PIC X(50) VALUE "Using COBOL 2002 Standard". 01 WS-ARRAY PIC 9(3) OCCURS 10 TIMES. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY WS-MESSAGE DISPLAY "Standard: COBOL 2002 (STANDARD-2)" * Use COBOL 2002 features like enhanced PERFORM PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 10 MOVE WS-COUNTER TO WS-ARRAY(WS-COUNTER) COMPUTE WS-RESULT = WS-COUNTER * WS-COUNTER DISPLAY "Index: " WS-COUNTER " Array Value: " WS-ARRAY(WS-COUNTER) " Square: " WS-RESULT END-PERFORM * Use COBOL 2002 built-in functions DISPLAY "String length of message: " FUNCTION LENGTH(WS-MESSAGE) DISPLAY "STANDARD-2 processing completed" STOP RUN.

Explanation: This example demonstrates basic usage of the STANDARD-2 clause. The program specifies COBOL 2002 compliance, which enables access to modern COBOL features like enhanced PERFORM statements, built-in functions (FUNCTION LENGTH), and improved array handling. These features provide more functionality and better code readability compared to COBOL 85.

STANDARD-2 with Built-in Functions

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
* STANDARD-2 with built-in functions example IDENTIFICATION DIVISION. PROGRAM-ID. STANDARD-2-FUNCTIONS-EXAMPLE. ENVIRONMENT DIVISION. CONFIGURATION SECTION. STANDARD-2. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-INPUT-STRING PIC X(50) VALUE "Hello World". 01 WS-UPPER-STRING PIC X(50). 01 WS-LOWER-STRING PIC X(50). 01 WS-REVERSE-STRING PIC X(50). 01 WS-SUBSTRING PIC X(20). 01 WS-STRING-LENGTH PIC 9(3). PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "STANDARD-2 Built-in Functions Example" DISPLAY "Original string: " WS-INPUT-STRING * Use COBOL 2002 built-in functions MOVE FUNCTION UPPER-CASE(WS-INPUT-STRING) TO WS-UPPER-STRING MOVE FUNCTION LOWER-CASE(WS-INPUT-STRING) TO WS-LOWER-STRING MOVE FUNCTION REVERSE(WS-INPUT-STRING) TO WS-REVERSE-STRING MOVE FUNCTION SUBSTRING(WS-INPUT-STRING, 1, 5) TO WS-SUBSTRING MOVE FUNCTION LENGTH(WS-INPUT-STRING) TO WS-STRING-LENGTH * Display results DISPLAY "Uppercase: " WS-UPPER-STRING DISPLAY "Lowercase: " WS-LOWER-STRING DISPLAY "Reversed: " WS-REVERSE-STRING DISPLAY "Substring (1-5): " WS-SUBSTRING DISPLAY "Length: " WS-STRING-LENGTH * Use numeric functions DISPLAY "Absolute value of -15: " FUNCTION ABS(-15) DISPLAY "Square root of 16: " FUNCTION SQRT(16) DISPLAY "Maximum of 10, 20, 5: " FUNCTION MAX(10, 20, 5) DISPLAY "Minimum of 10, 20, 5: " FUNCTION MIN(10, 20, 5) STOP RUN.

Explanation: This example shows how to use COBOL 2002 built-in functions with STANDARD-2. The program demonstrates string functions like FUNCTION UPPER-CASE, FUNCTION LOWER-CASE, FUNCTION REVERSE, FUNCTION SUBSTRING, and FUNCTION LENGTH, as well as numeric functions like FUNCTION ABS, FUNCTION SQRT, FUNCTION MAX, and FUNCTION MIN. These functions provide powerful capabilities that were not available in COBOL 85.

Enhanced PERFORM Statements

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
* STANDARD-2 enhanced PERFORM statements example IDENTIFICATION DIVISION. PROGRAM-ID. STANDARD-2-PERFORM-EXAMPLE. ENVIRONMENT DIVISION. CONFIGURATION SECTION. STANDARD-2. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-COUNTER PIC 9(3) VALUE ZERO. 01 WS-TOTAL PIC 9(5) VALUE ZERO. 01 WS-ARRAY PIC 9(3) OCCURS 10 TIMES. 01 WS-FOUND-FLAG PIC X(1) VALUE 'N'. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "STANDARD-2 Enhanced PERFORM Example" * Initialize array PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 10 MOVE WS-COUNTER TO WS-ARRAY(WS-COUNTER) END-PERFORM * Enhanced PERFORM with multiple conditions PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 10 OR WS-FOUND-FLAG = 'Y' IF WS-ARRAY(WS-COUNTER) = 7 MOVE 'Y' TO WS-FOUND-FLAG DISPLAY "Found 7 at position: " WS-COUNTER END-IF ADD WS-ARRAY(WS-COUNTER) TO WS-TOTAL END-PERFORM DISPLAY "Total sum: " WS-TOTAL * PERFORM with EXIT PERFORM PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 10 IF WS-ARRAY(WS-COUNTER) > 8 EXIT PERFORM END-IF DISPLAY "Processing: " WS-ARRAY(WS-COUNTER) END-PERFORM DISPLAY "Enhanced PERFORM processing completed" STOP RUN.

Explanation: This example demonstrates enhanced PERFORM statements available in STANDARD-2. The program shows PERFORM VARYING with multiple conditions using OR, and the EXIT PERFORM statement for early loop termination. These features provide better control flow and more flexible loop handling compared to the basic PERFORM statements available in COBOL 85.

Improved Exception Handling

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
* STANDARD-2 improved exception handling example IDENTIFICATION DIVISION. PROGRAM-ID. STANDARD-2-EXCEPTION-EXAMPLE. ENVIRONMENT DIVISION. CONFIGURATION SECTION. STANDARD-2. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-DIVIDEND PIC 9(5) VALUE 100. 01 WS-DIVISOR PIC 9(3) VALUE 0. 01 WS-RESULT PIC 9(7). 01 WS-ERROR-MESSAGE PIC X(50). PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "STANDARD-2 Exception Handling Example" * Division with exception handling COMPUTE WS-RESULT = WS-DIVIDEND / WS-DIVISOR ON SIZE ERROR DISPLAY "Size error occurred" MOVE "Division result too large" TO WS-ERROR-MESSAGE ON ZERO DIVIDE DISPLAY "Zero divide error occurred" MOVE "Division by zero" TO WS-ERROR-MESSAGE NOT ON SIZE ERROR DISPLAY "Division successful: " WS-RESULT NOT ON ZERO DIVIDE DISPLAY "No division errors" END-COMPUTE * File processing with exception handling DISPLAY "File processing with exception handling" * Simulate file operations with exception handling PERFORM PROCESS-FILE-WITH-EXCEPTIONS DISPLAY "Exception handling example completed" STOP RUN. PROCESS-FILE-WITH-EXCEPTIONS. * Enhanced exception handling for file operations DISPLAY "Processing file operations..." * Simulate different file status conditions EVALUATE TRUE WHEN WS-DIVISOR = 0 DISPLAY "File not found error simulated" WHEN WS-DIVISOR = 1 DISPLAY "File access denied error simulated" WHEN OTHER DISPLAY "File processing successful" END-EVALUATE.

Explanation: This example shows improved exception handling capabilities in STANDARD-2. The program demonstrates enhanced COMPUTE statements with multiple exception handlers (ON SIZE ERROR, ON ZERO DIVIDE) and their corresponding NOT clauses. This provides more comprehensive error handling and better program robustness compared to COBOL 85.

Best Practices and Considerations

Important Considerations

  • STANDARD-2 requires COBOL 2002 compliant compiler and runtime
  • Provides access to modern COBOL features and improved functionality
  • May have reduced portability compared to STANDARD-1
  • Enables better code readability and maintainability
  • Requires understanding of COBOL 2002 features

Advantages

  • Access to modern COBOL features
  • Built-in functions for string and numeric operations
  • Enhanced PERFORM statements
  • Improved exception handling
  • Better code readability and maintainability

Limitations

  • Requires COBOL 2002 compliant environment
  • May have reduced portability
  • Not compatible with COBOL 85 systems
  • Requires understanding of newer features
  • May increase complexity for simple programs

Best Practices

  • • Use when modern COBOL features are needed
  • • Ensure target environment supports COBOL 2002
  • • Leverage built-in functions for better code
  • • Use enhanced exception handling for robustness
  • • Consider portability requirements carefully

Test Your Knowledge

1. What does STANDARD-2 represent in COBOL?

  • COBOL 85 standard
  • COBOL 2002 standard
  • COBOL 74 standard
  • COBOL 2014 standard

2. What additional features does STANDARD-2 provide over STANDARD-1?

  • Only basic COBOL features
  • Built-in functions, enhanced PERFORM, improved exception handling
  • Only file processing features
  • Only arithmetic operations

3. Which of the following is available in STANDARD-2 but not in STANDARD-1?

  • Basic PERFORM statements
  • FUNCTION LENGTH and other built-in functions
  • Standard arithmetic operations
  • File processing statements

4. What is the primary advantage of using STANDARD-2?

  • Maximum portability
  • Access to modern COBOL features and improved functionality
  • Compatibility with legacy systems
  • Simpler syntax

5. Can STANDARD-2 programs run on COBOL 85 systems?

  • Yes, always
  • No, they require COBOL 2002 support
  • Only if they don't use newer features
  • Only on some systems