5.1.6 · D3C Programming

Worked examples — Control flow — if - else, switch, while, do-while, for, break, continue, goto

2,991 words14 min readBack to topic

The scenario matrix

Control flow has a finite number of "shapes" it can throw at you. If you can trace one example of every row below, you have covered the whole topic.

# Case class The tricky thing Covered by
A if/else chain, all sign branches first-true-wins; ==0 boundary Ex 1
B Assignment-in-condition bug = vs == Ex 2
C switch with intentional fall-through missing break groups cases Ex 3
D switch with an accidental missing break fall-through is a bug here Ex 4
E Loop that runs zero times while tests first Ex 5
F Loop that runs exactly once do-while tests after Ex 5
G forwhile translation with continue the infinite-loop trap Ex 6
H break vs continue in one loop who skips, who leaves Ex 7
I Nested loops + goto escape break only exits one level Ex 8
J Real-world word problem input-validation menu Ex 9
K Exam-style twist fall-through + loop counting combined Ex 10

The columns below trace each of these. The figures show the path execution actually walks, arrow by arrow.


A — the full sign chain

Figure — Control flow — if - else, switch, while, do-while, for, break, continue, goto

B — the classic = vs == trap


C — fall-through used on purpose


D — fall-through as a bug

Figure — Control flow — if - else, switch, while, do-while, for, break, continue, goto

E & F — zero runs vs exactly-one run

Figure — Control flow — if - else, switch, while, do-while, for, break, continue, goto

G — the forwhile continue trap


H — break and continue in one loop

Figure — Control flow — if - else, switch, while, do-while, for, break, continue, goto

I — nested loops need goto (or a flag)

Figure — Control flow — if - else, switch, while, do-while, for, break, continue, goto

J — real-world word problem


K — the exam twist


Recall

Recall Which loop runs its body at least once no matter what?

do-while — it tests after the body. A while with a false condition runs zero times.

Recall Why does the hand-translated

while in Ex 6 hang? continue jumps to the condition, skipping the manual i++ that sits after it, so the counter never advances → infinite loop. In a for, the update C still runs after continue.

Recall When must you reach for

goto over break? When escaping nested loops: break leaves only the innermost loop, goto (or a flag variable) can leave all levels at once.


See also: C Operators · Loops and Time Complexity · Boolean Logic · Functions in C · Recursion

Active recall

In Ex 5, how many numbers does the while (i<3) with i=5 print?
Zero — the condition is false before the body runs.
In Ex 6, why does the while version hang?
continue jumps to the condition and skips the i++ after it, so i never advances.
In Ex 8, why not use break to exit both loops?
break exits only the innermost loop; goto escapes both at once.
In Ex 10, what is the final total?
1121 — the missing break under case 0 adds an extra 10.