BudiBadu Logo
Samplebadu

COBOL by Example: Screen Section

COBOL 2002

Code

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SCREEN-DEMO.
       
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  USER-INPUT  PIC X(20).
       
       SCREEN SECTION.
       01  MAIN-SCREEN.
           05 BLANK SCREEN.
           05 LINE 5 COLUMN 20 VALUE "Welcome to COBOL".
           05 LINE 7 COLUMN 20 VALUE "Enter Name: ".
           05 LINE 7 COLUMN 32 PIC X(20) TO USER-INPUT REVERSE-VIDEO.
           
       PROCEDURE DIVISION.
           DISPLAY MAIN-SCREEN.
           ACCEPT MAIN-SCREEN.
           DISPLAY "You entered: " USER-INPUT AT 1020.
           STOP RUN.

Explanation

The SCREEN SECTION allows you to define complex terminal layouts with colors, positioning, and input fields. Instead of using raw DISPLAY statements, you define the entire screen structure declaratively in the Data Division. This was the standard way to build user interfaces before GUIs.

You can control attributes like REVERSE-VIDEO, BLINK, HIGHLIGHT, and colors (foreground/background). The DISPLAY statement paints the whole screen at once, and ACCEPT handles the interaction, moving the cursor between fields and collecting user input.

This section is often used in CICS or other transaction processing monitors to define the "maps" that users see on their green-screen terminals. It provides a structured way to handle form data separately from the program logic.

Code Breakdown

8
SCREEN SECTION. A special section in the Data Division for UI definitions.
10
BLANK SCREEN. A command to clear the entire terminal window before drawing the new screen.
11
LINE 5 COLUMN 20. Absolute positioning. Places the text starting at row 5, column 20.
13
TO USER-INPUT. Binds this screen field to the Working-Storage variable. When the user types here, USER-INPUT is updated.
13
REVERSE-VIDEO. Swaps the foreground and background colors to highlight the input field.
16
DISPLAY MAIN-SCREEN. Renders the static text and initial values of fields to the terminal.
17
ACCEPT MAIN-SCREEN. Transfers control to the user. The program pauses until the user presses Enter.