Keywords · Sequence · Selection · Iteration · Trace Tables · Errors
x = 5
== != < > <= >=
7 DIV 2 = 3
7 MOD 2 = 1
celsius * (9/5+32) which is wrong.temp, assigning a = b first destroys the original value of a. Order matters.a = b then b = a is a logic error — it sets both to b's original value.total = 0 must be set before the loop — this is called initialisation.i) automatically increments by 1 each time| Line / Step | x | total | OUTPUT |
|---|---|---|---|
| Start | |||
| Initialise | |||
| Loop 1 | |||
| Loop 2 | |||
| Loop 3 | |||
| Loop 4 | |||
| End |
x <= 4 is FALSE — loop exits and total is output. Table builds click by click →| Step | x | total | OUTPUT |
|---|---|---|---|
| Initialise: x = 1 | 1 | ||
| Initialise: total = 0 | 0 | ||
| ITERATION 1 — check: 1≤4 ✓ | |||
| total = total + x (0+1) | 1 | ||
| x = x + 1 (1+1) | 2 | ||
| ITERATION 2 — check: 2≤4 ✓ | |||
| total = total + x (1+2) | 3 | ||
| x = x + 1 (2+1) | 3 | ||
| ITERATION 3 — check: 3≤4 ✓ | |||
| total = total + x (3+3) | 6 | ||
| x = x + 1 (3+1) | 4 | ||
| ITERATION 4 — check: 4≤4 ✓ | |||
| total = total + x (6+4) | 10 | ||
| x = x + 1 (4+1) | 5 | ||
| check: 5≤4 ✗ — EXIT LOOP → OUTPUT total | 10 |
| Iteration | x | count |
|---|---|---|
| Init | 10 | 0 |
| 1 | 5 | 1 |
| 2 | 2 | 2 |
| 3 | 1 | 3 |
| x=1: exit |
Next up: 2.1.3a — Linear Search