Skip to main content

๐Ÿงพ COBOL Statements and Phrases โ€“ At a Glance


๐Ÿ”น Imperative Statementsโ€‹

Imperative statements tell the COBOL program what to do, step-by-step. These statements are executed sequentially and are responsible for carrying out actual operations like moving data or doing arithmetic.

๐Ÿ“Œ What They Do:โ€‹

  • Perform calculations

  • Move data from one field to another

  • Manipulate or transform string data

๐Ÿ’ก Examples:โ€‹

  • ADD โ€“ Add values

  • MOVE โ€“ Copy data

  • INSPECT โ€“ Analyze and count characters

  • STRING โ€“ Concatenate fields into a single value

  • UNSTRING โ€“ Break a string into smaller parts

These are the action commands in COBOL โ€“ similar to what functions or operations do in other programming languages.


๐Ÿ”น Conditional Statementsโ€‹

Conditional statements allow COBOL programs to make decisions. These are similar to if-else statements in other languages. The program evaluates a condition and executes different logic depending on the result.

๐Ÿ“Œ When They're Used:โ€‹

  • Making decisions in business logic

  • Validating inputs

  • Handling exceptions and special cases

๐Ÿ’ก Examples:โ€‹

  • IF โ€“ Basic condition

  • EVALUATE โ€“ Multi-branch decision (like switch/case)

  • READ AT END โ€“ Used while reading files

  • ON SIZE ERROR โ€“ Triggered when a math operation fails

  • ON OVERFLOW โ€“ Used with operations like STRING to detect overflow

These statements control program flow based on data values or results of operations.


๐Ÿ”น Delimited Scope Statementsโ€‹

In COBOL, a statement or block of code must have a clear start and end. Scope delimiters help ensure that the program knows where a block begins and where it ends, especially important for nested conditions or loops.

โœ… Explicit Scope Terminatorsโ€‹

  • Use END-<verb> (like END-IF, END-PERFORM)

  • Helps in making code readable and avoiding logical errors

โš ๏ธ Implicit Scope Terminatorโ€‹

  • A period . ends all unclosed previous statements

  • Used in simple or older COBOL programs, but can be risky in nested logic

For clarity and maintainability, it's highly recommended to use explicit END- statements in modern COBOL code.


๐Ÿ”น Compiler-Directing Statementsโ€‹

These statements are not executed at runtime. Instead, they guide the compiler during the compilation phase. Think of them like compiler instructions or preprocessor directives.

๐Ÿ“Œ Examples:โ€‹

  • COPY โ€“ Import reusable code like a copybook

  • SKIP โ€“ Advance output listing to a new line (for printed reports)

  • EJECT โ€“ Force a page break in the printed output

These help organize code and output, especially in large applications with reusable components.


๐Ÿ”น Useful COBOL Phrasesโ€‹

Phrases in COBOL are extensions to statements that modify how they behave. They make the language powerful and flexible when used properly.


๐Ÿ”„ CORRESPONDING (CORR)โ€‹

Used with MOVE, ADD, and SUBTRACT to operate on matching data names in group-level structures.

		ADD CORRESPONDING GROUP2 TO GROUP1.
  • Fields with the same name inside each group will be added.

  • Reduces the need for writing multiple ADD or MOVE lines manually.


๐Ÿ“ค GIVINGโ€‹

Used in arithmetic operations to store results in a specified variable without changing the original values.

		ADD 10 TO WS-VALUE GIVING WS-RESULT.

Good for preserving inputs and storing clean results.


๐Ÿ” ROUNDEDโ€‹

Rounds the result when decimal overflow occurs. Used with arithmetic statements.

		COMPUTE WS-RESULT = 10 / 3 ROUNDED.
  • Ensures cleaner output, especially for financial applications.

โš ๏ธ ON SIZE ERRORโ€‹

Triggers a special set of instructions when a calculation exceeds the fieldโ€™s capacity (like trying to store 1000 in a 3-digit field).


ADD WS-A TO WS-B GIVING WS-RESULT
ON SIZE ERROR
DISPLAY "Calculation too large!"
MOVE 999 TO WS-RESULT
END-ADD.

๐Ÿ“˜ Sample Code Snippetโ€‹

  • Adds two fields and stores the result in WS-RESULT

  • If the result doesn't fit, it displays an error and assigns a fallback value

		ADD WS-NUM1 TO WS-NUM2 GIVING WS-RESULT
ON SIZE ERROR
DISPLAY "Size error!"
MOVE 999 TO WS-RESULT
END-ADD.