Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

COBOL  Module 1 – Start here: COBOL today, its standards, compilers and source format

Divisions, paragraphs and the period that ends them

The four divisions of a COBOL program, how sentences and paragraphs run, how one stray period changes the logic, and how END-IF and CONTINUE avoid it.

  • Beginner
  • 20 minutes
  • Examples run with GnuCOBOL 3.2 and Python 3.14.8
  • By MySmartCoPilot
Free lesson, Pro answers: 20 minutes
Free: the whole lesson. With a pass: the model answers to its 2 interview questions.

What you will learn

  • Arrange the IDENTIFICATION, ENVIRONMENT, DATA and PROCEDURE divisions of a program in their required order
  • Explain how statements, sentences, paragraphs and sections relate, and how control passes between paragraphs
  • Use scope terminators such as END-IF instead of periods inside logic
  • Predict what a stray period or NEXT SENTENCE does to the flow of a program

Before you start

What you’ll learn · Before you start

What you will learn

  • Arrange the IDENTIFICATION, ENVIRONMENT, DATA and PROCEDURE divisions of a program in their required order
  • Explain how statements, sentences, paragraphs and sections relate, and how control passes between paragraphs
  • Use scope terminators such as END-IF instead of periods inside logic
  • Predict what a stray period or NEXT SENTENCE does to the flow of a program

Before you start

On this page

Two accounts, one of them overdrawn, and a COBOL program charged 50.00 in overdraft fees instead of 25.00. The cause was one period at the end of a line, which ended an IF two statements too early. A COBOL program is built from divisions, paragraphs and sentences, and the period is the punctuation that holds them together, or breaks them.

Below you will put the four divisions in their order, follow control from paragraph to paragraph and see how STOP RUN and GOBACK end a run, find what a stray period does inside an IF, tell NEXT SENTENCE from CONTINUE, and pair each END-IF with the IF it really closes.

Four divisions, always in the same order

A COBOL program has up to four division: One of the four main parts of a COBOL program, in a fixed order: IDENTIFICATION, ENVIRONMENT, DATA and PROCEDURE. Only the IDENTIFICATION DIVISION, with its PROGRAM-ID, is required., in a fixed order. IBM’s guide describes what each one holds. The IDENTIFICATION DIVISION names the program in its PROGRAM-ID paragraph, the name other programs use to call it. The ENVIRONMENT DIVISION describes “aspects of the program that depend on the computing environment”, from the computer it is compiled for to the files it connects to. The DATA DIVISION defines the data: file records, working storage, and data passed in from another program. The PROCEDURE DIVISION holds the code. Here are all four in one small program:

fourdiv.cob: the four divisions in order COB · fourdiv.cob
       IDENTIFICATION DIVISION.
       PROGRAM-ID. FOURDIV.
      * The four divisions, in the order the language requires.
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SOURCE-COMPUTER. LAPTOP.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  ACCOUNT-ID   PIC X(6)     VALUE "AC0001".
       01  BALANCE      PIC S9(5)V99 VALUE 1250.75.
       PROCEDURE DIVISION.
       SHOW-ACCOUNT.
           DISPLAY "Account " ACCOUNT-ID " balance " BALANCE
           STOP RUN.

The PROCEDURE DIVISION is where the structure goes deeper. IBM’s guide divides it into sections and paragraph: A named group of sentences in the PROCEDURE DIVISION. Its name starts in Area A and ends with a period, and PERFORM can run it by that name., which contain sentences, which contain statements. Here is fourdiv.cob from its divisions down to its two statements:

fourdiv.cob from top to bottom: the four divisions in order, and inside PROCEDURE a paragraph, a sentence and two statements.IDENTIFICATION DIVISIONPROGRAM-ID. FOURDIVENVIRONMENT DIVISIONCONFIGURATION SECTIONDATA DIVISIONWORKING-STORAGE SECTIONACCOUNT-ID, BALANCEPROCEDURE DIVISIONParagraph SHOW-ACCOUNTSentence, ended by the periodStatement: DISPLAYStatement: STOP RUN

The parts of fourdiv.cob, from divisions to statements

Text description of the diagram

The diagram is a column of the program's four divisions, in the order they must appear.

1. IDENTIFICATION DIVISION, with PROGRAM-ID. FOURDIV, which names the program. 2. ENVIRONMENT DIVISION, with its CONFIGURATION SECTION, which describes the computer the program is compiled for. 3. DATA DIVISION, whose WORKING-STORAGE SECTION defines the items ACCOUNT-ID and BALANCE. 4. PROCEDURE DIVISION, which holds the code. Inside it is the paragraph SHOW-ACCOUNT; inside the paragraph is one sentence, which the period ends; inside the sentence are two statements, DISPLAY and then STOP RUN.

Build it, then a program with only two divisions, then one that puts two divisions in the wrong order:

Two divisions are enough, the wrong order is not Python · divisions_session.py
from session import files, run

# The four divisions in order, the two that every program needs, and what cobc says when the order is wrong.
files("fourdiv.cob", "tiny.cob", "order.cob")
run("""
cobc -x fourdiv.cob && ./fourdiv
cobc -x tiny.cob && ./tiny
cobc -x order.cob 2>&1 | grep 'error:'
""")

Output

$ cobc -x fourdiv.cob && ./fourdiv
Account AC0001 balance +01250.75
$ cobc -x tiny.cob && ./tiny
Two divisions are enough
$ cobc -x order.cob 2>&1 | grep 'error:'
order.cob:7: error: PROCEDURE DIVISION header missing
order.cob:7: error: syntax error, unexpected ENVIRONMENT
order.cob:8: error: syntax error, unexpected CONFIGURATION
order.cob:9: error: syntax error, unexpected SOURCE-COMPUTER
order.cob:10: error: syntax error, unexpected PROCEDURE

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 divisions_session.py

fourdiv printed its account. tiny shows that the ENVIRONMENT and DATA divisions may be left out when a program needs nothing from them. IBM’s Language Reference calls even the PROCEDURE DIVISION optional, although a program without one does nothing; what every program needs is the IDENTIFICATION DIVISION, first, with its PROGRAM-ID paragraph, which IBM’s documentation marks as required. order.cob put its DATA DIVISION before the ENVIRONMENT DIVISION, and cobc’s first error came at line 7: after the DATA DIVISION only the PROCEDURE DIVISION may follow, so it reported the PROCEDURE DIVISION header missing and then could make no sense of the lines after it.

Control falls from one paragraph into the next

Inside the PROCEDURE DIVISION, a statement such as MOVE or DISPLAY is one action. A sentence: One or more COBOL statements ended by a separator period. is one or more statements that end with a separator period: A period followed by a space or the end of the line. It ends a sentence, and with it every statement still open, an IF included.. A paragraph is a name in Area A followed by its sentences, and a section groups paragraphs under a header. PERFORM runs a paragraph by name and comes back. Otherwise the program runs its paragraphs in the order they are written, and it does not stop at the end of one. This program has no STOP RUN:

fallthru.cob: two paragraphs and no STOP RUN COB · fallthru.cob
       IDENTIFICATION DIVISION.
       PROGRAM-ID. FALLTHRU.
      * MAIN-PARA has no STOP RUN: watch what runs after it ends.
       PROCEDURE DIVISION.
       MAIN-PARA.
           DISPLAY "1. MAIN-PARA starts"
           PERFORM PRINT-TOTALS
           DISPLAY "3. back in MAIN-PARA".
       PRINT-TOTALS.
           DISPLAY "2. PRINT-TOTALS runs".

The session runs it, and then a file of three programs that shows how a run ends:

Falling through, and two ways to end Python · flow_session.py
from session import files, run

# Control falls from one paragraph into the next; STOP RUN ends the whole run unit, GOBACK returns to the caller.
files("fallthru.cob", "rununit.cob")
run("""
cobc -x fallthru.cob && ./fallthru
cobc -x rununit.cob && ./rununit
""")

Output

$ cobc -x fallthru.cob && ./fallthru
1. MAIN-PARA starts
2. PRINT-TOTALS runs
3. back in MAIN-PARA
2. PRINT-TOTALS runs
$ cobc -x rununit.cob && ./rununit
MAINPGM calls SUBGOOD
  SUBGOOD ends with GOBACK
MAINPGM is back from SUBGOOD
  SUBSTOP ends with STOP RUN

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 flow_session.py

fallthru printed 2 twice. PERFORM ran PRINT-TOTALS and came back for line 3; then MAIN-PARA ended, nothing ended the run, and control fell into PRINT-TOTALS, the next paragraph in the source. Step through it:

Interactive · stage by stage Where control goes in fallthru.cob Step through the run. The numbers are the four lines the program printed.
Where control goes in fallthru.cob, step 5 of 5. Showing MAIN-PARA, PRINT-TOTALS, End of the program. In focus: End of the program.PERFORM returnsMAIN-PARAprints 1 and 3PRINT-TOTALSprints 2End of the programnothing left to run

The drawing shows the whole scene; its 5 steps are listed below.

Every step as text
  1. The first paragraph. The run starts at the first paragraph of the PROCEDURE DIVISION, MAIN-PARA, which prints 1. MAIN-PARA starts.
  2. PERFORM. PERFORM PRINT-TOTALS runs that paragraph, which prints 2. PRINT-TOTALS runs.
  3. Back after PERFORM. When PRINT-TOTALS ends, PERFORM returns to the statement after it, which prints 3. back in MAIN-PARA.
  4. Falling through. The period ends the last sentence of MAIN-PARA, but nothing ends the run. Control falls into the next paragraph in the source, so PRINT-TOTALS runs a second time and prints 2. again.
  5. The end. After the last paragraph the PROCEDURE DIVISION is over and the run ends. A STOP RUN after the third DISPLAY would have ended it there, after three lines.

Two statements end a run, and they differ inside a subprogram. IBM’s documentation says that STOP RUN in a subprogram returns directly to the program that called the main program, which ends the whole run unit: The programs that run together from one start. STOP RUN ends all of them, wherever it is reached; GOBACK returns only from the program that executes it., while GOBACK returns to the caller. rununit.cob shows both: after SUBGOOD’s GOBACK, MAINPGM carried on, and after SUBSTOP’s STOP RUN, MAINPGM never printed its last line.

Key idea

Paragraphs run in the order they are written, so end a main program with STOP RUN or GOBACK, and a subprogram with GOBACK, before control falls into code that was never meant to run next.

Checkpoint

Check: The four divisions and their order · Sentences, paragraphs and control flow

  1. Question 1 of 2 In order.cob the DATA DIVISION comes before the ENVIRONMENT DIVISION. What did cobc report first?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: PROCEDURE DIVISION header missing, at the ENVIRONMENT DIVISION line

    The divisions must come in the order IDENTIFICATION, ENVIRONMENT, DATA, PROCEDURE. A division out of place is an error, and cobc reports it where the division it expected is missing.

  2. Question 2 of 2 What would fallthru.cob print if STOP RUN were added after DISPLAY "3. back in MAIN-PARA"?

    Choose one answer.

    A hint

    STOP RUN ends the run where it stands.

    Show the answer: Question 2 of 2

    Answer: 1, 2 and 3, each once

    Control falls from one paragraph into the next unless something ends the run or a PERFORM returns. STOP RUN after the third DISPLAY ends it there, so the output is 1, 2, 3.

One period ends every open statement

Now the fee program from the opening. Its paragraph CHARGE-OVERDRAFT is meant to display a message, add 25.00 and display a second message, for an overdrawn account only:

fees.cob: the fee program with a stray period COB · fees.cob
       IDENTIFICATION DIVISION.
       PROGRAM-ID. FEES.
      * Charges a 25.00 fee on each overdrawn account. The IF is
      * meant to cover the three statements under it.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  BALANCE      PIC S9(5)V99.
       01  FEES-TOTAL   PIC 9(3)V99 VALUE ZERO.
       PROCEDURE DIVISION.
       MAIN-PARA.
           MOVE 120.00 TO BALANCE
           PERFORM CHARGE-OVERDRAFT
           MOVE -35.50 TO BALANCE
           PERFORM CHARGE-OVERDRAFT
           DISPLAY "Fees charged in total: " FEES-TOTAL
           STOP RUN.
       CHARGE-OVERDRAFT.
           DISPLAY "Account balance " BALANCE
           IF BALANCE < ZERO
               DISPLAY "  overdrawn".
               ADD 25.00 TO FEES-TOTAL
               DISPLAY "  fee charged".

The line DISPLAY " overdrawn" ends with a period. IBM’s guide says what that does: the period is an implicit scope terminator that “ends the scope of all previous statements not yet ended”, the IF included. So the IF covers one statement, and the ADD after it runs for every account. fees-fixed.cob drops the inner periods and closes the IF with END-IF, an explicit scope terminator: A word such as END-IF or END-READ that ends one statement without ending the sentence. It closes the nearest statement of its kind that is still open., which ends a statement without ending the sentence:

The stray period, the fix, and a check that finds it Python · period_session.py
from session import files, run

# One period too many: the fee program with the stray period, the same program with END-IF, a -Wall build that says
# nothing, what -Wterminator reports, and a check that finds IFs closed by a period.
files("fees.cob", "fees-fixed.cob")
run("""
cobc -x fees.cob && ./fees
cobc -x fees-fixed.cob && ./fees-fixed
cobc -x -Wall -o fees-wall fees.cob
cobc -x -Wterminator -o fees-count fees.cob 2>&1 | grep -o '[A-Z]* statement not terminated' | sort | uniq -c
cobc -x -Wterminator -o fees-check fees.cob 2>&1 | grep 'IF statement'
cobc -x -Wterminator -o fees-fixed-check fees-fixed.cob 2>&1 | grep 'IF statement' || echo "no IF without END-IF"
""")

Output

$ cobc -x fees.cob && ./fees
Account balance +00120.00
  fee charged
Account balance -00035.50
  overdrawn
  fee charged
Fees charged in total: 050.00
$ cobc -x fees-fixed.cob && ./fees-fixed
Account balance +00120.00
Account balance -00035.50
  overdrawn
  fee charged
Fees charged in total: 025.00
$ cobc -x -Wall -o fees-wall fees.cob
$ cobc -x -Wterminator -o fees-count fees.cob 2>&1 | grep -o '[A-Z]* statement not terminated' | sort | uniq -c
   1 ADD statement not terminated
   4 DISPLAY statement not terminated
   1 IF statement not terminated
$ cobc -x -Wterminator -o fees-check fees.cob 2>&1 | grep 'IF statement'
fees.cob:19: warning: IF statement not terminated by END-IF [-Wterminator]
$ cobc -x -Wterminator -o fees-fixed-check fees-fixed.cob 2>&1 | grep 'IF statement' || echo "no IF without END-IF"
no IF without END-IF

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 period_session.py

The version with the stray period charged the account with 120.00 and totalled 050.00; the fixed one charged only the overdrawn account, 025.00. The -Wall build said nothing about the period. -Wterminator lists every statement that ends without its END- word: in fees.cob four DISPLAY statements, the ADD and the IF. Filtering its output for IF statement found line 19, the IF that ends at a period, and nothing in the fixed version. Follow the false branch for the account in credit:

Interactive · stage by stage Where the false branch of the IF lands in fees.cob Step through CHARGE-OVERDRAFT for an account with 120.00 in it.
Where the false branch of the IF lands in fees.cob, step 5 of 5. Showing DISPLAY the balance, IF BALANCE < ZERO, DISPLAY overdrawn, ADD 25.00, DISPLAY fee charged. In focus: DISPLAY overdrawn, ADD 25.00, DISPLAY fee charged.false: after the periodDISPLAY the balanceAccount balance +00120.00IF BALANCE < ZEROfalse for 120.00DISPLAY overdrawnends with a periodADD 25.00to FEES-TOTAL, outside IFDISPLAY fee chargedoutside the IF

The drawing shows the whole scene; its 5 steps are listed below.

Every step as text
  1. The balance. CHARGE-OVERDRAFT starts with BALANCE at 120.00 and displays it as Account balance +00120.00.
  2. The test. IF BALANCE < ZERO is false. The IF ends at the first period after it, the one that ends the line with DISPLAY " overdrawn", so only that one statement belongs to it.
  3. The false branch. Control continues after the end of the IF, at ADD 25.00 TO FEES-TOTAL, so a fee is added for an account that is in credit.
  4. The second statement. DISPLAY " fee charged" runs too, and the paragraph ends at its last period, with the fee wrongly charged.
  5. The overdrawn account. For -35.50 the test is true, so DISPLAY " overdrawn" runs as well and the fee is charged, as intended. Two accounts, one of them overdrawn, and FEES-TOTAL came to 050.00 instead of 025.00.

A period at the end of every line

Wrong: ending each line inside an IF with a period, the habit of old code. Right: no period inside the logic, END-IF to close each IF, and one period at the end of the paragraph, which is what IBM’s guide recommends.

NEXT SENTENCE jumps past the next period, CONTINUE does nothing

Older code often writes NEXT SENTENCE where nothing should happen. It does not do nothing: it sends control to the statement after the next separator period, and IBM’s documentation adds that inside IF … END-IF, control “does not pass to the statement following the END-IF”. CONTINUE is the statement that does nothing, a “no operation statement”. This program uses one and then the other:

skips.cob: NEXT SENTENCE, then CONTINUE COB · skips.cob
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SKIPS.
      * NEXT SENTENCE and CONTINUE, each inside IF ... END-IF.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  BALANCE      PIC S9(5)V99 VALUE 80.00.
       PROCEDURE DIVISION.
       MAIN-PARA.
           IF BALANCE > ZERO
               NEXT SENTENCE
           END-IF
           DISPLAY "A: after the first END-IF"
           DISPLAY "B: still in the first sentence".
           DISPLAY "C: first statement after the period"
           IF BALANCE > ZERO
               CONTINUE
           END-IF
           DISPLAY "D: after the second END-IF"
           STOP RUN.
Which DISPLAY statements run Python · skips_session.py
from session import files, run

# NEXT SENTENCE jumps past the next period, even from inside IF ... END-IF; CONTINUE does nothing.
files("skips.cob")
run("""
cobc -x skips.cob && ./skips
cobc -x -Wall -o skips-wall skips.cob 2>&1 | grep 'warning:'
""")

Output

$ cobc -x skips.cob && ./skips
C: first statement after the period
D: after the second END-IF
$ cobc -x -Wall -o skips-wall skips.cob 2>&1 | grep 'warning:'
skips.cob:10: warning: NEXT SENTENCE is archaic in GnuCOBOL [-Warchaic]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 skips_session.py

NEXT SENTENCE skipped A and B, the rest of its sentence, and landed on C, the first statement after the period. CONTINUE let control go on to D. GnuCOBOL’s -Wall reports NEXT SENTENCE as archaic. The two side by side, as skips.cob ran them:

Inside IF … END-IF Where control goes next In skips.cob
NEXT SENTENCE The statement after the next period C, skipping A and B
CONTINUE Nowhere new: the statement after END-IF D

Replacing NEXT SENTENCE

The two are not interchangeable: with CONTINUE in place of NEXT SENTENCE, skips.cob would also print A and B. Change one only with tests that compare the output before and after.

Checkpoint

Check: What a period ends · NEXT SENTENCE and CONTINUE

  1. Question 1 of 2 In fees.cob, with its stray period, which statements of CHARGE-OVERDRAFT run for a balance of 120.00?

    Choose every answer that is right.

    Show the answer: Question 1 of 2

    Answer:

    • ADD 25.00 TO FEES-TOTAL
    • DISPLAY " fee charged"
    • DISPLAY "Account balance " BALANCE

    A separator period ends every open statement, the IF included. Only DISPLAY " overdrawn" stayed inside the IF, so the ADD and the last DISPLAY ran for the account in credit and FEES-TOTAL reached 050.00.

  2. Question 2 of 2 In skips.cob, NEXT SENTENCE ran inside IF ... END-IF. Which DISPLAY ran next?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: C: first statement after the period

    IBM's documentation says it plainly: with NEXT SENTENCE, control does not pass to the statement after the END-IF but to the statement after the closest following period. The session printed C and D only.

An END-IF closes the nearest open IF

Explicit terminators have a trap of their own when statements nest: each END-IF closes the nearest IF that is still open, whatever the indentation suggests. In this paragraph the END-IF was meant for the outer IF:

nested.cob: two IF statements and one END-IF COB · nested.cob
       IDENTIFICATION DIVISION.
       PROGRAM-ID. NESTED.
      * Savings accounts get a second message when they are overdrawn.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  ACCOUNT-TYPE PIC X.
       01  BALANCE      PIC S9(5)V99.
       PROCEDURE DIVISION.
       MAIN-PARA.
           MOVE "S" TO ACCOUNT-TYPE
           MOVE 300.00 TO BALANCE
           PERFORM CHECK-ACCOUNT
           MOVE -40.00 TO BALANCE
           PERFORM CHECK-ACCOUNT
           STOP RUN.
       CHECK-ACCOUNT.
           IF BALANCE < ZERO
               DISPLAY "Overdrawn: " BALANCE
               IF ACCOUNT-TYPE = "S"
                   DISPLAY "  savings may not be overdrawn"
           END-IF
           DISPLAY "Checked " BALANCE.
One END-IF, then two Python · nested_session.py
from session import files, run

# One END-IF for two IFs closes the inner one, so the last DISPLAY stays inside the outer IF; the fix adds the second.
files("nested.cob", "nested-fixed.cob")
run("""
cobc -x nested.cob && ./nested
cobc -x nested-fixed.cob && ./nested-fixed
""")

Output

$ cobc -x nested.cob && ./nested
Overdrawn: -00040.00
  savings may not be overdrawn
Checked -00040.00
$ cobc -x nested-fixed.cob && ./nested-fixed
Checked +00300.00
Overdrawn: -00040.00
  savings may not be overdrawn
Checked -00040.00

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 nested_session.py

For 300.00 the first program printed nothing at all. Its only END-IF closed the inner IF, so DISPLAY "Checked " stayed inside the outer IF, whose condition was false, until the period at the end of the paragraph. nested-fixed.cob adds the second END-IF, and it printed Checked +00300.00 for the account in credit. IBM’s guide shows the same with READ: one END-READ under two nested READ statements closes the inner READ, and the fix is a second END-READ.

Key idea

Give every IF its own END-IF, and keep the period for the end of the paragraph: then the scopes are where the code says they are.

Checkpoint

Check: Which statement END-IF closes

  1. One question In nested.cob, which IF does the END-IF under the two IF statements close?

    Choose one answer.

    Show the answer: One question

    Answer: The inner IF, IF ACCOUNT-TYPE = "S"

    The inner IF was still open, so END-IF closed it, and DISPLAY "Checked " stayed inside the outer IF. That is why the account with 300.00 printed nothing until a second END-IF was added.

Practise: divisions, paragraphs and periods

Every answer below can be checked against a session of this lesson. Practice begins where your answers have been weakest, with a reason for every choice:

Practice that adapts to you

Practise this lesson

13 questions about 5 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 13 Put the four divisions of a COBOL program in the order they must appear.

    Give each item its position, from 1 (first).

    Show the answer: Question 1 of 13

    Answer:

    1. IDENTIFICATION DIVISION
    2. ENVIRONMENT DIVISION
    3. DATA DIVISION
    4. PROCEDURE DIVISION

    A program names itself, describes the environment it depends on, defines its data and then gives its code. The ENVIRONMENT and DATA divisions may be left out, as tiny.cob does, but never moved.

  2. Question 2 of 13 In which division does a program define the fields it works with, such as BALANCE?

    Choose one answer.

    Show the answer: Question 2 of 13

    Answer: The DATA DIVISION

    IBM's guide sums the DATA DIVISION up as describing the data: file records, working storage, data passed in from another program.

  3. Question 3 of 13 tiny.cob compiled and ran with only an IDENTIFICATION DIVISION and a PROCEDURE DIVISION. What does that show?

    Choose one answer.

    Show the answer: Question 3 of 13

    Answer: The ENVIRONMENT and DATA divisions are optional when a program needs nothing from them

    A program has to name itself with a PROGRAM-ID; the other divisions are there when the program needs them, always in their order, and a program with no PROCEDURE DIVISION would do nothing.

  4. Question 4 of 13 What ends a sentence in the PROCEDURE DIVISION?

    Choose one answer.

    Show the answer: Question 4 of 13

    Answer: A separator period

    Statements make up sentences, sentences end at a period, and paragraphs are named groups of sentences.

  5. Question 5 of 13 In rununit.cob, why was MAINPGM is back from SUBSTOP never printed?

    Choose one answer.

    Show the answer: Question 5 of 13

    Answer: STOP RUN in SUBSTOP ended the whole run unit, the calling program included

    GOBACK returns to the caller; STOP RUN ends every program of the run unit at once. So a subprogram that ends with STOP RUN silently stops the program that called it.

  6. Question 6 of 13 You write a subprogram that several batch programs will CALL. Which statement should end it?

    Choose one answer.

    Show the answer: Question 6 of 13

    Answer: GOBACK, so control returns to whichever program called it

    Callers expect control back. End subprograms with GOBACK, which also ends a main program correctly, so it is safe wherever the program ends up being used.

  7. Question 7 of 13 What does a separator period inside an IF do?

    Choose one answer.

    Show the answer: Question 7 of 13

    Answer: It ends the IF and every other statement still open at that point

    A period closes the sentence and with it every open statement. Inside logic that is almost never what you mean, which is why IBM recommends periods only at the end of a paragraph or a program.

  8. Question 8 of 13 The version of fees.cob with the stray period runs for three accounts with balances 50.00, -10.00 and 0.00. What is FEES-TOTAL afterwards?

    Type a number.

    A hint

    Which statements of CHARGE-OVERDRAFT are outside the IF?

    Show the answer: Question 8 of 13

    Answer: 75

    The ADD is outside the IF, so every call adds 25.00: three accounts give 75.00, printed as 075.00. The version with END-IF charges only the overdrawn account, -10.00, for a total of 25.00.

  9. Question 9 of 13 You inherit a paragraph whose IF statements all end with periods, and you must add one statement inside one of them. What is the safest way?

    Choose one answer.

    Show the answer: Question 9 of 13

    Answer: First rewrite that IF in END-IF style, check that the output is unchanged, then add the statement inside it

    Change structure and behaviour in separate steps. Once the IF ends with END-IF, its scope is visible, and a new statement placed before END-IF is inside it.

  10. Question 10 of 13 What does CONTINUE do?

    Choose one answer.

    Show the answer: Question 10 of 13

    Answer: Nothing, control goes on to the next statement

    CONTINUE does nothing and lets control go on, so inside IF ... END-IF it behaves as most people expect NEXT SENTENCE to behave.

  11. Question 11 of 13 Someone replaces NEXT SENTENCE with CONTINUE in skips.cob. Which lines does the program print then?

    Choose one answer.

    Show the answer: Question 11 of 13

    Answer: A, B, C and D

    The two phrases are not interchangeable: replacing NEXT SENTENCE with CONTINUE adds A and B to the output here. That is why such a replacement needs the same tests before and after.

  12. Question 12 of 13 An END-IF closes which IF?

    Choose one answer.

    Show the answer: Question 12 of 13

    Answer: The nearest IF that is still open

    Each END-IF ends one IF, the innermost one still open. Write one END-IF per IF, and let the indentation follow the code rather than the other way round.

  13. Question 13 of 13 With the second END-IF added, what did nested-fixed print for the balance of 300.00?

    Choose one answer.

    Show the answer: Question 13 of 13

    Answer: Checked +00300.00

    With one END-IF per IF, the scopes match the indentation, and the check line runs for every account: Checked +00300.00, then the three lines for -40.00.

Rewrite skips.cob without NEXT SENTENCE

Change skips.cob so that it still prints only C and D but uses no NEXT SENTENCE, for example with IF … ELSE … END-IF. Build it with cobc -x -Wall -debug skips.cob, check that the archaic warning is gone, and compare the output with the session’s.

Interview questions

Warm-up (fresher to 2–5 years): how does a COBOL sentence differ from a statement?

A statement is one action that starts with a verb, such as MOVE, ADD, DISPLAY or IF. A sentence is one or more statements ended by a separator period, a period followed by a space or the end of the line. The difference matters because the period ends every statement still open at that point: an IF whose first statement ends with a period covers only that one statement, and the statements after it run whatever the condition says. Modern style therefore writes each paragraph as a single sentence: no periods inside the logic, END-IF and the other scope terminators to close statements, and one period at the end of the paragraph. A paragraph, in turn, is a name in Area A followed by its sentences.

Two harder questions follow, about fees charged to accounts in credit and about removing NEXT SENTENCE from a legacy program. Note your own answer first, then open each first step:

Model answers open with any pass: ₹79 for 1 week ($2.99), one payment that never renews.

  1. Fresher to mid

    Customers in credit are being charged an overdraft fee. The fee paragraph has an IF with no END-IF, and its statements end with periods. How do you find the cause, fix it and stop it coming back?

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  2. Mid to senior

    You are asked to remove every NEXT SENTENCE from a legacy program because the compiler reports it as archaic. Can you replace each one with CONTINUE mechanically? How would you make the change safely?

    Model answer, Pro. First step and follow-ups

Organisations named on this page

MySmartCoPilot is not connected with IBM or the GnuCOBOL project. Each statement rule on this page cites their published manuals, which References lists.

Flashcards · 7 cards

Recall it, then check

Say the answer to yourself before you look: recalling it is what makes it stick.

  1. In what order do the four divisions come, and which one must every program have?

    IDENTIFICATION, ENVIRONMENT, DATA, PROCEDURE. Only the IDENTIFICATION DIVISION with its PROGRAM-ID paragraph is required; a program that needs nothing from the middle two may leave them out.

  2. What happens when a paragraph ends and nothing ends the run?

    Control falls into the next paragraph in the source. fallthru.cob printed its totals twice: once through PERFORM, then again by falling through.

  3. How do STOP RUN and GOBACK differ when a subprogram runs them?

    GOBACK returns to the caller, which carries on. STOP RUN ends the whole run unit, so the caller never runs the rest of its statements.

  4. What does a period inside an IF do?

    It ends every statement still open, the IF included. The statements after the period then run whatever the condition says, which is how a fee was charged to an account in credit.

  5. Which GnuCOBOL warning helps find a stray period inside an IF?

    -Wterminator lists every statement that ends without its END- word; filter it for IF statements. -Wall says nothing about the period.

  6. Inside IF ... END-IF, how do NEXT SENTENCE and CONTINUE differ?

    NEXT SENTENCE jumps past the next period, to the first statement after it, and skips whatever lies before that period. CONTINUE is a statement that does nothing, so the program carries on after END-IF. Swapping one for the other changes the output.

  7. Two IF statements nest and there is one END-IF. Which IF does it close?

    The nearest IF that is still open, whatever the indentation suggests. Give every IF its own END-IF, and keep the one period for the end of the paragraph.

Key takeaways

  • A program has up to four divisions in a fixed order, IDENTIFICATION, ENVIRONMENT, DATA and PROCEDURE, and only the IDENTIFICATION DIVISION with its PROGRAM-ID is required, although the code lives in the PROCEDURE DIVISION.
  • Statements make sentences, sentences end at a period, paragraphs are named groups of sentences, and control falls from one paragraph into the next unless a PERFORM returns or STOP RUN or GOBACK ends the run.
  • A period ends every statement still open, so a period inside an IF ended it early and charged a fee to an account in credit, 050.00 in fees where 025.00 was right.
  • NEXT SENTENCE jumps past the next period, even from inside IF … END-IF, while CONTINUE does nothing, so the two are not interchangeable.
  • An END-IF closes the nearest IF still open, so every IF needs its own END-IF, and the period belongs at the end of the paragraph.

Terms in this lesson

division
One of the four main parts of a COBOL program, in a fixed order: IDENTIFICATION, ENVIRONMENT, DATA and PROCEDURE. Only the IDENTIFICATION DIVISION, with its PROGRAM-ID, is required.
paragraph
A named group of sentences in the PROCEDURE DIVISION. Its name starts in Area A and ends with a period, and PERFORM can run it by that name.
run unit
The programs that run together from one start. STOP RUN ends all of them, wherever it is reached; GOBACK returns only from the program that executes it.
scope terminator
A word such as END-IF or END-READ that ends one statement without ending the sentence. It closes the nearest statement of its kind that is still open.
sentence
One or more COBOL statements ended by a separator period.
separator period
A period followed by a space or the end of the line. It ends a sentence, and with it every statement still open, an IF included.

References

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.