Program logic is how you organize the steps and decisions in your COBOL program. It boils down to three ideas: sequence (do steps in order), selection (choose one path with IF or EVALUATE), and iteration (repeat with PERFORM UNTIL or PERFORM VARYING). This page shows how these appear in COBOL and how to structure them clearly. For where execution starts and how PERFORM returns, see Program flow.
| Kind | Meaning |
|---|---|
| Sequence | Statements run in order; one after another |
| Selection | IF/EVALUATE: choose one of several paths |
| Iteration | PERFORM UNTIL or VARYING: repeat a block |
Sequence means statements run one after another. The first sentence runs, then the next, then the next. PERFORM calls are part of sequence: PERFORM A then PERFORM B means “run A, then run B.” So your main paragraph often looks like a list of PERFORMs and other statements in the order you want them to run.
123456MAIN. PERFORM INIT-FILES PERFORM READ-AND-PROCESS UNTIL WS-EOF PERFORM CLOSE-AND-FINISH GOBACK. *> Sequence: init, then loop, then close
Selection means “do one thing or another” based on a condition. IF condition ... ELSE ... END-IF picks between two paths. EVALUATE ... WHEN ... WHEN OTHER ... END-EVALUATE picks among many values (like a switch/case). Use IF for simple true/false; use EVALUATE when you have several distinct values to test.
12345678910111213*> Selection: two-way branch IF WS-STATUS = "OK" PERFORM NORMAL-PROCESS ELSE PERFORM ERROR-PROCESS END-IF. *> Selection: multi-way EVALUATE WS-CODE WHEN "A" PERFORM HANDLE-A WHEN "B" PERFORM HANDLE-B WHEN OTHER PERFORM HANDLE-OTHER END-EVALUATE.
Iteration means repeating a block. PERFORM paragraph-name UNTIL condition runs the paragraph until the condition is true (tested after each execution). PERFORM VARYING idx FROM 1 BY 1 UNTIL idx > 10 ... END-PERFORM runs the in-line statements with idx 1, 2, ... 10. So loops are written with PERFORM, not with a separate “DO” or “WHILE” verb.
12345678*> Iteration: repeat until end-of-file PERFORM READ-RECORD PERFORM PROCESS-LOOP UNTIL WS-EOF. *> Iteration: repeat with a counter PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 10 ADD WS-I TO WS-TOTAL END-PERFORM.
Give each logical task its own paragraph and PERFORM it by name. The main flow then reads like a table of contents: INIT, then READ-AND-PROCESS until done, then CLOSE. Keep paragraphs short and focused. Use IF/EVALUATE inside a paragraph for small decisions; use PERFORM to jump to another paragraph for a larger step. For more on structure see Algorithm design and Program control.
1. Which construct is used for “do this until condition is true”?
2. What are the three basic kinds of program logic?