MainframeMaster

COBOL Tutorial

SCREEN SECTION - Screen Handling

Progress0 of 0 lessons

Overview

SCREEN SECTION defines terminal screens, field positions, and attributes. Use ACCEPT to read, DISPLAY to show.

🖥️ Analogy

It’s like drawing a form on graph paper: each box (field) has a row and column. The user fills the boxes; you check the values.

Defining and Using Screens

Define a Screen

cobol
1
2
3
4
5
6
7
8
DATA DIVISION. SCREEN SECTION. 01 LOGIN-SCREEN. 05 LINE 2 COLUMN 10 VALUE 'User ID:'. 05 LINE 2 COLUMN 20 PIC X(10) TO USER-ID. 05 LINE 3 COLUMN 10 VALUE 'Password:'. 05 LINE 3 COLUMN 20 PIC X(10) TO USER-PASS.

TO / FROM clauses bind screen fields to data items (compiler-specific).

ACCEPT/DISPLAY with UPDATE

cobol
1
2
3
4
5
6
7
8
PROCEDURE DIVISION. DISPLAY LOGIN-SCREEN ACCEPT LOGIN-SCREEN IF USER-ID = SPACES OR USER-PASS = SPACES DISPLAY 'Both fields required.' DISPLAY LOGIN-SCREEN ACCEPT LOGIN-SCREEN WITH UPDATE END-IF.

WITH UPDATE allows editing existing values in-place.

Validation Patterns

cobol
1
2
3
4
IF USER-ID = SPACES OR FUNCTION LENGTH(USER-ID) < 3 DISPLAY 'Invalid user id' AT LINE 5 COLUMN 10 GO TO REDISPLAY END-IF.

Validate immediately after ACCEPT and show messages at clear positions.

Non-Code Example

  • Pre-fill last used values for convenience
  • Highlight invalid fields
  • Guide with placeholder text

Best Practices and Mistakes

Best Practices

  • Keep forms simple and consistent
  • Use UPDATE for edit flows
  • Centralize validation rules
  • Provide clear error messages

Common Mistakes

MistakeProblemFix
Hard-coded positions onlyDifficult to changeUse constants for LINE/COLUMN
No input maskingLeaked sensitive inputMask passwords

Quick Reference

FeatureSyntaxExample
Screen definitionSCREEN SECTION01 LOGIN-SCREEN ...
PositioningLINE n COLUMN mLINE 2 COLUMN 10
InteractionDISPLAY / ACCEPTACCEPT LOGIN-SCREEN

Test Your Knowledge

1. Where are screen items defined?

  • FILE SECTION
  • SCREEN SECTION
  • WORKING-STORAGE
  • LINKAGE

2. Which verbs interact with the terminal?

  • ACCEPT/DISPLAY
  • WRITE/READ
  • CALL/INVOKE
  • MOVE/COMPUTE

3. How do you position a field?

  • LINE/COLUMN clauses
  • INDEX
  • OFFSET
  • REDEFINES

4. What does UPDATE do with ACCEPT?

  • Clears screen
  • Allows editing existing text
  • Compiles faster
  • Encrypts input

5. Where to validate input?

  • In SETUP only
  • Immediately after ACCEPT
  • In REPORT SECTION
  • In LINKAGE only

Frequently Asked Questions

Related Pages