๐งพ 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 (likeswitch/case
) -
READ AT END
โ Used while reading files -
ON SIZE ERROR
โ Triggered when a math operation fails -
ON OVERFLOW
โ Used with operations likeSTRING
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>
(likeEND-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
orMOVE
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.