KTU S1

Drawing Flowcharts for Selection and Loops

By the end you should be able to: Draw flowcharts for multi-way selection and for all three loop types, and convert between pseudocode and flowchart form.

You know the symbols. This topic is about assembling them into the three constructs — and the loop patterns in particular are what past papers ask for.

Multi-way selection

A chain of IF-ELSE becomes a chain of diamonds, each No branch feeding the next decision.

        ◇ marks >= 90 ? ──Yes──► [grade ← "S"] ──┐
        │ No                                      │
        ▼                                         │
        ◇ marks >= 85 ? ──Yes──► [grade ← "A+"] ──┤
        │ No                                      │
        ▼                                         │
        ◇ marks >= 80 ? ──Yes──► [grade ← "A"] ───┤
        │ No                                      │
        ▼                                         │
       [grade ← "F"] ─────────────────────────────┤
                                                  ▼
                                            (continue)

Every Yes branch jumps to the same merge point. That merge is the ENDIF.

The three loops, drawn

WHILE — test at the top

              │
              ▼
        ┌────►◇
        │   ╱   ╲
        │  ╱ i<=5 ╲──No──► (exit)
        │  ╲       ╱
        │   ╲     ╱
        │     Yes
        │      │
        │      ▼
        │  [ PRINT i ]
        │      │
        │      ▼
        │  [ i ← i+1 ]
        │      │
        └──────┘        flow-line returns ABOVE the diamond

The return line goes back to a point before the test. That is what makes it a pre-test loop, and it's the detail markers look for.

REPEAT-UNTIL — test at the bottom

              │
              ▼
        ┌►[ PRINT i ]
        │      │
        │      ▼
        │ [ i ← i+1 ]
        │      │
        │      ▼
        │      ◇
        │   ╱     ╲
        └─No─ i>5 ? ╲
            ╲       ╱
             ╲     ╱
              Yes
               │
               ▼
            (exit)

The return line goes back to a point before the body but after nothing else — the body is entered directly, without a test. That's the post-test structure.

FOR — the hexagon

              │
              ▼
        ┌►╱─────────────╲
        │⟨  i ← 1 TO 50  ⟩──exhausted──► (exit)
        │ ╲─────────────╱
        │      │ next i
        │      ▼
        │  [ PRINT i ]
        │      │
        └──────┘

The hexagon holds initialisation, test and increment in one symbol, which is why the FOR loop's diagram is the most compact of the three.

Converting pseudocode to a flowchart

Work statement by statement:

PseudocodeFlowchart
READ x / PRINT xParallelogram
x ← expressionRectangle
IF … THEN … ELSE … ENDIFDiamond, two branches, merge
WHILE … ENDWHILEDiamond with return line above it
REPEAT … UNTILBody, then diamond, return line above body
FOR … ENDFORHexagon with return line
CALL moduleRectangle with double bars

Two rules that decide marks

1. Loops must have a visible return line. A diagram with a decision but no arrow going back is not a loop — it's a selection. This is the single most common error in flowchart questions.

2. Branches must merge. After an IF-ELSE, both paths rejoin before the flow continues. Two separate STOP terminators for the two branches is poor form and usually loses a mark.