
COBOL: examples explained
Maturity and boundaries: experimental
Implemented scope: Numeric processing, CALL/LINKAGE, sequential files and selected JSON/XML, allocation and NATIONAL operations are shown.
Boundary: This is not full ISO COBOL: SQL/CICS/MQ are unsupported, dynamic CALL targets are module-known only, and JSON/XML shapes are limited.
COBOL supported commands and scope
Browse the language-specific supported-command and scope reference, with searchable categories and explicit boundaries.
Open scope referenceCOBOL separates identification, data description and execution. PIC describes data format; DISPLAY produces output and STOP RUN ends the program.
Twelve short examples and one larger program. Choose a topic or work through the examples in order.
1. 1 + 2 + 3 + 4 + 5 = 15
The numbers 1 through 5 are added to 15. The program prints 15 and ends with exit code 0.
IDENTIFICATION DIVISION.
PROGRAM-ID. QuickSum.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TOTAL PIC 9(2).
PROCEDURE DIVISION.
COMPUTE TOTAL = 1 + 2 + 3 + 4 + 5
DISPLAY TOTAL
STOP RUN.Expected exit code: 0; output verification pattern (regex): '15'
PBXB64.exe sum15-cobol.cbl -O2 -o example.exe .\example.exe $LASTEXITCODE
Executed natively with the V22 candidate from 2026-09-30; final-compiler verification pending.
2. DISPLAY and STOP RUN
PROGRAM-ID names the program. In PROCEDURE DIVISION, DISPLAY prints Hello from COBOL. STOP RUN ends it; the expected exit code is 0.
IDENTIFICATION DIVISION.
PROGRAM-ID. hello.
PROCEDURE DIVISION.
DISPLAY "Hello from COBOL".
STOP RUN.
Expected exit code: 0; output verification pattern (regex): 'Hello from COBOL\n'
PBXB64.exe test_hello.cbl -O2 -o example.exe .\example.exe $LASTEXITCODE
Canonical test case with an expected result; no new native V22 check here. Final execution pending.
3. Display a long decimal literal
DISPLAY uses a literal with 19 fractional digits. The test contract expects 1.1234567890123456789. This is a focused precision case; final verification remains tied to the final compiler.
IDENTIFICATION DIVISION.
PROGRAM-ID. DECIMAL-WIDE-DISPLAY.
PROCEDURE DIVISION.
DISPLAY 1.1234567890123456789.
STOP RUN.
Expected exit code: 0; output verification pattern (regex): '1\\.1234567890123456789\\n'
PBXB64.exe regress_decimal_literal_wide_display.cbl -O2 -o example.exe .\example.exe $LASTEXITCODE
Canonical test case with an expected result; no new native V22 check here. Final execution pending.
4. COMPUTE with a decimal field
Step-by-step flow
- A is 10 and B is 3; parentheses first produce 13.
- (13 × 2) - (5 / 5) gives 25.
- PIC 9(2)V99 specifies two fractional digits; DISPLAY shows 25.00 in the test.
IDENTIFICATION DIVISION.
PROGRAM-ID. ComputeTest.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(4) VALUE 10.
01 B PIC 9(4) VALUE 3.
01 C PIC 9(2)V99.
PROCEDURE DIVISION.
COMPUTE C = (A + B) * 2 - 5 / 5
DISPLAY C
STOP RUN.
Expected exit code: 0
Canonical output verification pattern (regex)
25.00
PBXB64.exe cobol-test_compute.cbl -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
5. DIVIDE INTO GIVING: operand order
Step-by-step flow
- A contains 0.5 and B contains 2.3.
- DIVIDE A INTO B means B / A rather than A / B.
- GIVING writes 4.6 into Q while retaining the original operands.
IDENTIFICATION DIVISION.
PROGRAM-ID. DivIntoGiving.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9V9 VALUE 0.5.
01 B PIC 9V9 VALUE 2.3.
01 Q PIC 9V9.
PROCEDURE DIVISION.
DIVIDE A INTO B GIVING Q
DISPLAY Q
STOP RUN.
Expected exit code: 0
Canonical output verification pattern (regex)
4.6
PBXB64.exe cobol-test_divide_into_giving.cbl -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
6. ROUNDED with an integer destination
Step-by-step flow
- Operands 1.4 and 1.4 sum to 2.8.
- Destination C has no fractional digit because its picture is PIC 9.
- ROUNDED produces output 3 here rather than truncated 2.
IDENTIFICATION DIVISION.
PROGRAM-ID. RoundedTest.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9V9 VALUE 1.4.
01 B PIC 9V9 VALUE 1.4.
01 C PIC 9.
PROCEDURE DIVISION.
ADD A TO B GIVING C ROUNDED
DISPLAY C
STOP RUN.
Expected exit code: 0
Canonical output verification pattern (regex)
3
PBXB64.exe cobol-test_rounded.cbl -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
7. Quotient and remainder of an integer
Step-by-step flow
- DIVIDE B INTO A computes 23 / 5.
- GIVING Q stores integer quotient 4 and REMAINDER R stores 3.
- 4 × 5 + 3 reconstructs 23 and explains both outputs.
IDENTIFICATION DIVISION.
PROGRAM-ID. RemainderTest.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(4) VALUE 23.
01 B PIC 9(4) VALUE 5.
01 Q PIC 9(4).
01 R PIC 9(4).
PROCEDURE DIVISION.
DIVIDE B INTO A GIVING Q REMAINDER R
DISPLAY Q
DISPLAY R
STOP RUN.
Expected exit code: 0
Canonical output verification pattern (regex)
4 3
PBXB64.exe cobol-test_remainder.cbl -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
8. A scaled remainder field
Step-by-step flow
- A is 2.3 and B is 0.5; Q uses PIC 9 and stores quotient 4.
- R uses PIC 9V9 and stores remaining value 0.3.
- 4 × 0.5 + 0.3 reconstructs 2.3. The remainder field's picture determines representable fractional digits.
IDENTIFICATION DIVISION.
PROGRAM-ID. RemScaled.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9V9 VALUE 2.3.
01 B PIC 9V9 VALUE 0.5.
01 Q PIC 9.
01 R PIC 9V9.
PROCEDURE DIVISION.
DIVIDE B INTO A GIVING Q REMAINDER R
DISPLAY Q
DISPLAY R
STOP RUN.
Expected exit code: 0
Canonical output verification pattern (regex)
4 0.3
PBXB64.exe cobol-test_remainder_scaled.cbl -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
9. INSPECT counts characters in a field
Step-by-step flow
- WS-STR contains BANANA in a 20-character field.
- TALLYING increases initially zero WS-COUNT for every A.
- BANANA contains three A characters, producing output 3.
IDENTIFICATION DIVISION.
PROGRAM-ID. InspectTallyingTest.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STR PIC X(20) VALUE "BANANA".
01 WS-COUNT PIC 9(4) VALUE 0.
PROCEDURE DIVISION.
INSPECT WS-STR TALLYING WS-COUNT FOR ALL "A"
DISPLAY WS-COUNT
STOP RUN.
Expected exit code: 0
Canonical output verification pattern (regex)
3
PBXB64.exe cobol-test_inspect_tallying.cbl -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
10. String intrinsics and fixed field width
Step-by-step flow
- UPPER-CASE, LOWER-CASE and REVERSE produce ABC, abc and cba; LENGTH returns 5 for hello.
- TRIM first removes trailing spaces. Assigning to PIC X(8) then pads the fixed-width field again.
- ORD and CHAR use one-based character ordering here: ORD(A) is 66 and CHAR(66) returns A.
IDENTIFICATION DIVISION.
PROGRAM-ID. w1str.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-T PIC X(8).
PROCEDURE DIVISION.
DISPLAY "[" FUNCTION UPPER-CASE("abc") "]".
DISPLAY "[" FUNCTION LOWER-CASE("ABC") "]".
DISPLAY "[" FUNCTION REVERSE("abc") "]".
DISPLAY FUNCTION LENGTH("hello").
MOVE FUNCTION TRIM("xy ") TO WS-T.
DISPLAY "[" WS-T "]".
DISPLAY FUNCTION ORD("A").
DISPLAY FUNCTION CHAR(66).
STOP RUN.
Expected exit code: 0
Canonical output verification pattern (regex)
\[ABC\]\n\[abc\]\n\[cba\]\n5\n\[xy \]\n66\nA\n
PBXB64.exe cobol-cobol_w1_intrinsics_string.cbl -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
11. Set and test level-88 condition names
Step-by-step flow
- Condition names SMALL, MID and BIG describe ranges of the same field A.
- A = 5 satisfies BIG. SET SMALL TO TRUE then assigns a value satisfying SMALL.
- The subsequent SET-FALSE case is checked by the concrete PBXB64 contract as NOTSMALL; outputs are BIG, SMALL and NOTSMALL.
IDENTIFICATION DIVISION.
PROGRAM-ID. L88.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9 VALUE 5.
88 SMALL VALUES 1 THRU 4.
88 MID VALUES 1, 3 THRU 5.
88 BIG VALUES 5 THRU 9.
PROCEDURE DIVISION.
IF BIG DISPLAY "BIG" ELSE DISPLAY "SMALL" END-IF
SET SMALL TO TRUE
IF SMALL DISPLAY "SMALL" ELSE DISPLAY "BIG" END-IF
SET SMALL TO FALSE
IF SMALL DISPLAY "BAD" ELSE DISPLAY "NOTSMALL" END-IF
STOP RUN.
Expected exit code: 0
Canonical output verification pattern (regex)
BIG SMALL NOTSMALL
PBXB64.exe cobol-test_88level.cbl -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
12. UNSTRING splits a comma-delimited field
Step-by-step flow
- WS-SRC contains ONE,TWO,THREE.
- DELIMITED BY splits at commas and writes parts into WS-A, WS-B and WS-C.
- Each destination has width five. ONE and TWO therefore print trailing spaces, while THREE fills its field.
IDENTIFICATION DIVISION.
PROGRAM-ID. UnstringDelimitedTest.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-SRC PIC X(30) VALUE "ONE,TWO,THREE".
01 WS-A PIC X(5).
01 WS-B PIC X(5).
01 WS-C PIC X(5).
PROCEDURE DIVISION.
UNSTRING WS-SRC DELIMITED BY "," INTO WS-A WS-B WS-C
DISPLAY WS-A
DISPLAY WS-B
DISPLAY WS-C
STOP RUN.
Expected exit code: 0
Canonical output verification pattern (regex)
ONE TWO THREE
PBXB64.exe cobol-test_unstring_delimited.cbl -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
Larger example: Decision tables with ALSO, THRU and NOT
Step-by-step flow
- EVALUATE A ALSO B considers two values together; (1, 2) matches while (1, 9) takes the other path.
- THRU includes its bounds: 5 lies between 3 and 7, while 8 is outside. NOT reverses range or list tests.
- Later blocks combine a range and excluded list using ALSO. The complete output contract documents each decision, including the final ANY case.
IDENTIFICATION DIVISION.
PROGRAM-ID. evalfull.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9 VALUE 1.
01 B PIC 9 VALUE 2.
PROCEDURE DIVISION.
EVALUATE A ALSO B
WHEN 1 ALSO 2
DISPLAY "BOTH"
WHEN OTHER
DISPLAY "MISS"
END-EVALUATE
MOVE 9 TO B
EVALUATE A ALSO B
WHEN 1 ALSO 2
DISPLAY "BOTH"
WHEN OTHER
DISPLAY "MISS"
END-EVALUATE
MOVE 5 TO A
EVALUATE A
WHEN 10
DISPLAY "TEN"
WHEN 3 THRU 7
DISPLAY "RANGE"
WHEN OTHER
DISPLAY "OTHER"
END-EVALUATE
MOVE 8 TO A
EVALUATE A
WHEN 3 THRU 7
DISPLAY "RANGE"
WHEN OTHER
DISPLAY "OTHER"
END-EVALUATE
MOVE 9 TO A
EVALUATE A
WHEN NOT 1 THRU 8
DISPLAY "NOTRANGE"
WHEN OTHER
DISPLAY "OTHER"
END-EVALUATE
MOVE 5 TO A
EVALUATE A
WHEN NOT 1 THRU 8
DISPLAY "NOTRANGE"
WHEN OTHER
DISPLAY "OTHER"
END-EVALUATE
MOVE 4 TO A
EVALUATE A
WHEN NOT 1, 2
DISPLAY "NOTLIST"
WHEN OTHER
DISPLAY "OTHER"
END-EVALUATE
MOVE 2 TO A
EVALUATE A
WHEN NOT 1, 2
DISPLAY "NOTLIST"
WHEN OTHER
DISPLAY "OTHER"
END-EVALUATE
MOVE 5 TO A
MOVE 9 TO B
EVALUATE A ALSO B
WHEN 3 THRU 7 ALSO NOT 1, 2
DISPLAY "COMBO"
WHEN OTHER
DISPLAY "MISS"
END-EVALUATE
MOVE 1 TO B
EVALUATE A ALSO B
WHEN 3 THRU 7 ALSO NOT 1, 2
DISPLAY "COMBO"
WHEN OTHER
DISPLAY "MISS"
END-EVALUATE
MOVE 1 TO A
MOVE 2 TO B
EVALUATE A ALSO B
WHEN 1 ALSO 2
DISPLAY "BOTH"
WHEN ANY
DISPLAY "MATCHED"
WHEN OTHER
DISPLAY "MISS"
END-EVALUATE
STOP RUN.
Expected exit code: 0
Canonical output verification pattern (regex)
BOTH MISS RANGE OTHER NOTRANGE OTHER NOTLIST OTHER COMBO MISS BOTH MATCHED
PBXB64.exe cobol-test_evaluate_also_thru_not.cbl -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
All sources for this tutorial
Each example has its own folder; the README lists compiler commands. Included support files retain matching paths.
Download all 13 examples as ZIPBridging
The numeric example binds COBOL SHARED-COUNT in BASIC. A PIC field is not a universal native machine type; the concrete bridge contract performs the mapping. PIC-X bridges require separate qualification.
Explore complete source pairsPBXB64 is part of the PBX-Compiler package. Free for private use, cleanly licensed for business.