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.
| Structure | Meaning |
|---|---|
| Sequence | Statements execute in order. No branching or looping. |
| Selection | Different code runs based on a condition (IF, EVALUATE). |
| Iteration | A 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 is the default: statements run in the order they appear. A typical main paragraph is a sequence of PERFORMs, each doing one job.
123456PROCEDURE DIVISION. MAIN. PERFORM INITIALIZE-PROGRAM PERFORM PROCESS-FILE PERFORM CLOSE-AND-FINISH STOP RUN.
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).
1234567891011IF 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 repeats a block. PERFORM UNTIL runs until a condition is true; PERFORM VARYING runs with a loop variable that changes each time.
1234567891011PERFORM 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 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.
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.
1. Which control structure is used when you need to repeat a block of code until a condition is true?
2. What is a benefit of structured programming?