HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to COBOLVariablesArithmeticConditions (IF)Loops (PERFORM)
REFERENCE
📖 Overview
🖥️ Structure & Output
📦 Variables
🔢 Arithmetic
🔀 Condition
🔁 Loop
COBOL›Conditions (IF)

Conditions (IF)

Decision making with IF ... ELSE ... END-IF.

Simple enough for a 5-year-old to understand

IF / ELSE / END-IF

Example code
1
IDENTIFICATION DIVISION.
2
PROGRAM-ID. H.
3
DATA DIVISION.
4
WORKING-STORAGE SECTION.
5
01 N PIC 99 VALUE 7.
Two digits, value 7.
VariableTwo digits, value 7.
6
PROCEDURE DIVISION.
7
IF FUNCTION MOD(N 2) = 0
Is the remainder 0?
ConditionIs the remainder 0?
8
DISPLAY "even"
9
ELSE
10
DISPLAY "odd"
11
END-IF.
End of condition.
ConditionEnd of condition.
12
STOP RUN.
VariableCondition
💡 Hint: IF closes with END-IF. Comparison = > < ; AND/OR are used.

About this topic

Conditions in COBOL read like English sentences, and comparison operators can be written as both symbols and words. The word format is common in legacy code, while the symbol format is common in modern code.

The most critical detail is scope termination. In classic COBOL, a condition ended with a period; the period also closed all nested structures at the same time. This led to a misplaced period completely changing the program flow.

Modern COBOL solves this problem with explicit scope terminators. Each condition closes with its own ending keyword; it is never ambiguous which structure is closing in nested constructs. This format is standard in newly written code.

What you will learn

  • Condition writing and operators
  • Explicit scope termination
  • The scope effect of the period
  • Multi-way evaluation
  • Condition names

Who is this for?

  • COBOL developers writing decision logic
  • Those looking for flow errors in legacy code
  • Resolving nested conditions

When to use?

An explicit ending keyword must always be used in nested conditions; closing with a period misleads the maintainer.

Giving meaningful names to specific values of a field brings conditions closer to the business language and allows fixing in a single place when the value changes.

Common mistakes

Wrong

IF WS-TUTAR > 100
    DISPLAY 'High'.
    DISPLAY 'Transaction complete'.
ELSE
    DISPLAY 'Low'.

The period after the first print closes the condition; the second print runs in any case, and since the following else branch is no longer bound to a condition, it causes a compilation error. The period is the most powerful terminator in COBOL and completely changes the flow when placed accidentally.

Correct

IF WS-TUTAR > 100
    DISPLAY 'High'
    DISPLAY 'Transaction complete'
ELSE
    DISPLAY 'Low'
END-IF.

Wrong

IF WS-A > 10
    IF WS-B > 20
        DISPLAY 'both are large'
    ELSE
        DISPLAY 'uncertain'.

It is ambiguous which condition the else branch belongs to; the rule binds it to the nearest open condition, but the reader might assume otherwise. Closing with a period closes both conditions at once, so the intent is not visible in the code. Explicit ending keywords eliminate this ambiguity.

Correct

IF WS-A > 10
    IF WS-B > 20
        DISPLAY 'both are large'
    ELSE
        DISPLAY 'uncertain'
    END-IF
END-IF.

Wrong

IF WS-DURUM = 1
    DISPLAY 'active'
ELSE
    IF WS-DURUM = 2
        DISPLAY 'passive'
    ELSE
        IF WS-DURUM = 3
            DISPLAY 'deleted'
        END-IF
    END-IF
END-IF.

The code works, but nested conditions create unnecessary depth; as the number of branches increases, it becomes unreadable. There is an evaluation structure for multi-way decisions and it expresses the same logic in a flat list.

Correct

EVALUATE WS-DURUM
    WHEN 1 DISPLAY 'active'
    WHEN 2 DISPLAY 'passive'
    WHEN 3 DISPLAY 'deleted'
    WHEN OTHER DISPLAY 'unknown'
END-EVALUATE.

Frequently asked questions

What is a condition name?

It is a special-level declaration that gives a name to a specific value of a field. You write that name instead of the number in the condition; the code gets closer to the business language, and if the value changes, you only need to update the declaration.

What are class conditions used for?

They test whether the contents of a field are numeric or alphabetic. Checking whether data coming from a user or a file is numeric before putting it into a calculation is the standard way to prevent runtime errors.

Does the evaluation structure flow through branches?

No. The first matching branch runs and the structure ends; there is no fall-through behavior like in some languages. To combine multiple conditions in the same branch, several values can be listed in a single branch.

Summary

The period closes all nested structures.

Explicit ending keywords should be used.

Else binds to the nearest open condition.

There is an evaluation structure for multi-way decisions.

Condition names bring the code closer to the business language.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code