COBOL Tutorial

Progress0 of 0 lessons

COBOL algorithm design

Algorithm design is how you plan the logic of a program: the order of steps, when to make decisions, and when to repeat actions. In COBOL you express this with sequence (one statement after another), selection (IF, EVALUATE), and iteration (PERFORM UNTIL, PERFORM VARYING). Using these in a structured way makes programs easier to understand, test, and maintain.

The three basic control structures

Building blocks of algorithm design
StructureMeaning
SequenceStatements execute in order. No branching or looping.
SelectionDifferent code runs based on a condition (IF, EVALUATE).
IterationA block repeats (PERFORM UNTIL, PERFORM VARYING).

Every algorithm can be described with just these three. Avoid scattering GO TO; use PERFORM to call paragraphs and return, so the flow stays clear.

Sequence

Sequence is the default: statements run in the order they appear. A typical main paragraph is a sequence of PERFORMs, each doing one job.

cobol
1
2
3
4
5
6
PROCEDURE DIVISION. MAIN. PERFORM INITIALIZE-PROGRAM PERFORM PROCESS-FILE PERFORM CLOSE-AND-FINISH STOP RUN.

Selection

Selection chooses between paths based on a condition. Use IF/ELSE for simple choices and EVALUATE when you have many cases (like a menu or code value).

cobol
1
2
3
4
5
6
7
8
9
10
11
IF WS-STATUS = 'OK' PERFORM HANDLE-SUCCESS ELSE PERFORM HANDLE-ERROR END-IF. EVALUATE WS-CODE WHEN 1 PERFORM DO-OPTION-1 WHEN 2 PERFORM DO-OPTION-2 WHEN OTHER PERFORM DO-DEFAULT END-EVALUATE.

Iteration

Iteration repeats a block. PERFORM UNTIL runs until a condition is true; PERFORM VARYING runs with a loop variable that changes each time.

cobol
1
2
3
4
5
6
7
8
9
10
11
PERFORM UNTIL NO-MORE-RECORDS READ IN-FILE AT END SET NO-MORE-RECORDS TO TRUE NOT AT END PERFORM PROCESS-ONE-RECORD END-READ END-PERFORM. PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10 COMPUTE WS-VAL(I) = I * 2 END-PERFORM.

Structured programming and top-down design

Structured programming means relying on sequence, selection, and iteration and using PERFORM for subroutines instead of GO TO. Top-down design starts with the main steps (high level) and breaks each step into smaller paragraphs. The main paragraph reads like an outline; the detail is in the performed paragraphs. See Structured programming for more.

Step-by-step: designing a simple program

  • List the main steps from the specification (e.g. init, read/process, close, stop).
  • Write the main paragraph as a sequence of PERFORM paragraph-name for each step.
  • Implement each paragraph: use IF/EVALUATE for decisions and PERFORM UNTIL or PERFORM VARYING for loops.
  • If a paragraph gets long or does more than one thing, split it into smaller PERFORMed paragraphs.
  • Keep single entry and single exit: one way in, one way out (no GO TO out of the middle).

Explain like I'm five

An algorithm is the recipe: first do this, then if it’s X do that otherwise do something else, and keep doing this part until we’re done. The three building blocks are: do steps in order (sequence), choose between options (selection), and repeat a step (iteration). A clear recipe is easy to follow and to change later.

Test Your Knowledge

1. Which control structure is used when you need to repeat a block of code until a condition is true?

  • Sequence
  • Selection
  • Iteration
  • GO TO

2. What is a benefit of structured programming?

  • Faster execution
  • Clearer control flow, easier to read and maintain
  • Smaller object code
  • Fewer data divisions

Related concepts

Related Pages