Control Flow in C: 7 Mark-Losing Mistakes and Precise Fixes

Trace seven common C control-flow mistakes step by step, see exactly where execution changes course, and learn a short routine for checking every branch and loop.

KnowledgeGate Team

Exam prep & CS education

Updated 14 Nov 20265 min read
Control Flow in C: 7 Mark-Losing Mistakes and Precise Fixes

Learners often execute the control flow they intended, not the path the C program takes. One broad condition, missed update or absent break can make a snippet choose the wrong branch or never terminate. Across CS Fundamentals, the reliable method is to write the state before the test, evaluate the condition, mark the chosen transfer, and apply only the update that runs.

1. C control-flow mistake 1: putting the broad branch before the specific one

Why it happens: An else-if chain resembles a list of labels, tempting learners to expect all true conditions to be compared. C takes the first true branch. Conditionals in C: if, else-if and switch with examples explains the rule in detail.

What goes wrong: With int value=78; const char *label; and if(value>=0) label="non-negative"; else if(value>=75) label="high"; else label="negative";, the first test makes label equal "non-negative". The also-true value>=75 test is never reached.

What to do instead: Order guards from narrow to broad. Testing value >= 75 first produces "high". Check reachability, not isolated truth.

2. C control-flow mistake 2: treating do-while as a pre-test loop

Why it happens: while and do-while use similar condition syntax, so their test positions get mentally merged.

What goes wrong: Trace int n=0,count=0; do { count++; n--; } while(n>0);. The body first makes count = 1 and n = -1. Then n > 0 is false. Final state: (n, count) = (-1, 1), not (0, 0).

What to do instead: Use while when zero executions must be possible. For int n=0,count=0; while(n>0){ count++; n--; }, the pre-test is false and final state is (0, 0). Name whether the test is before or after the body.

3. C control-flow mistake 3: translating a verbal bound into the wrong operator

Why it happens: “Five values” becomes i < 5 without checking the start or first excluded value. Compare the constructs in Loops in C: for, while and do-while with worked examples.

What goes wrong: In int sum=0; for(int i=1;i<5;i++) sum+=i;, entries are 1, 2, 3, 4. The first failure is 5; the sum is 1 + 2 + 3 + 4 = 10.

What to do instead: For 1 through 5, use i <= 5. Entries are 1, 2, 3, 4, 5, failure first occurs at 6, and the sum is 1 + 2 + 3 + 4 + 5 = 15. List entries before using a formula.

4. C control-flow mistake 4: letting continue skip the only update

Two trace lanes show continue freezing i=2 and the corrected loop ending with sum 8.

Why it happens: A for loop updates after continue. Learners transfer that behaviour to a while loop, where the update is an ordinary body statement.

What goes wrong: In int i=0,sum=0; while(i<5){ if(i==2) continue; sum+=i; i++; }, entries 0 and 1 leave (i, sum) at (1, 0) and (2, 1). At i = 2, continue skips i++. State (2, 1) repeats forever, with no final sum.

What to do instead: Update on the continuing path: if(i==2){ i++; continue; }. Entries are 0, 1, 2, 3, 4; 2 is skipped; contributions total 0 + 1 + 3 + 4 = 8. The loop exits at (i, sum) = (5, 8).

Side-by-side broken and corrected C while-loop traces show continue freezing (2,1) and the corrected loop ending with sum 8.

5. C control-flow mistake 5: assuming each switch case stops automatically

Why it happens: Case labels look separate, so readers may imagine a break that is absent.

What goes wrong: In int choice=2,total=10; switch(choice){ case 1: total+=5; case 2: total*=2; case 3: total-=3; }, control starts at case 2. The total changes 10 -> 20, then fall-through into case 3 changes it 20 -> 17. The final value is 17, not 20.

What to do instead: Add break; after each case that should stop. After total *= 2;, it makes the final total 20. Fall-through is valid only when intentional and clear.

6. C control-flow mistake 6: expecting break to exit every nested loop

Why it happens: break is remembered as “leave the loop” without identifying its nearest enclosing construct.

What goes wrong: In int hits=0; for(int i=1;i<=3;i++){ for(int j=1;j<=3;j++){ if(j==2) break; hits++; } }, each outer value i = 1, 2, 3 lets j = 1 increment hits; j = 2 exits only the inner loop. Final result: hits = 3, not 1.

What to do instead: To end both loops, initialise int hits=0,stop=0;, set stop=1 before the inner break, and use outer condition i<=3&&!stop. At i=1,j=1, hits=1; at j=2, stop=1; the next outer test fails. Final hits=1.

7. C control-flow mistake 7: writing an unsigned countdown that cannot fail

Why it happens: The signed sequence 3, 2, 1, 0, -1 is imagined for an unsigned object that cannot represent -1.

What goes wrong: In unsigned int i=3,count=0; while(i>=0){ count++; i--; }, i >= 0 is always true. Entries begin 3, 2, 1, 0; decrementing zero wraps to UINT_MAX. The loop continues, so there is no final count.

What to do instead: Use unsigned int count=0; for(unsigned int i=3;;i--){ count++; if(i==0) break; }. It visits 3, 2, 1, 0, breaks before decrementing zero, and finishes with count = 4. Keep UINT_MAX symbolic rather than assuming a width.

8. C control-flow mistakes: the 30-second trace routine and next step

Use this six-step check: identify the controlling construct; write the state before the test; evaluate the test as true or false; mark the selected branch or transfer; apply only the update that executes; state the exit condition or declare non-termination.

A twelve-minute drill should recover "high" from reordered value=78; contrast do-while count 1 with while count 0; recompute sums 10 and 15; reject a final frozen-loop value; recover switch total 17, nested-loop hits 3, and corrected unsigned count 4. Use the Control Flow question bank to test the same trace routine on fresh snippets.

For the full control-flow concept map, see C Programming Flow Control: Branch and Loop Output Traces. It maps sequence, selection, iteration and jumps; the seven traces target mark-losing execution errors. Use the C Language course for structured coverage and practice. Trace the code that exists, one transfer at a time.