Flowcharts and Trace Tables: Follow Every Decision Before Giving the Output


A flowchart tells you which instruction happens next. A trace table records the changing values while you follow that path. To find the output reliably, use the current values at each decision, execute only the selected branch, and write output only when an output instruction is reached.

These worked examples use flowcharts and trace tables for two original algorithms. The first makes one decision. The second repeats a calculation with a decision checked before each loop pass. All inputs are supplied whole numbers; no executable program or software installation is needed. A pencil-and-paper dry run is sufficient.

Read symbols and assignments precisely

Conventions used in the two diagrams and their text alternatives.
Symbol or notation Meaning in this lesson
Rounded start/end shape The beginning or termination of this algorithm
Parallelogram Input a value or output a value
Rectangle Perform a process such as assigning or updating a variable
Diamond Test a condition and follow the labelled Yes or No arrow
Arrow The next instruction in the direction shown
← Assign the value of the expression on the right to the variable on the left

For example, i ← i + 1 means use the old value of i, add one, and store the result back in i. It is an update, not an algebraic statement claiming that a number equals itself plus one. A decision such as i ≤ n compares the values currently stored; it does not change either variable.

Our trace tables show the state after every listed instruction or test. A dash means “not assigned yet” or “no output at this step”, as labelled. An unchanged variable is repeated for clarity. Some examination tables ask for changed values only; follow their supplied layout while keeping the same execution order.

Algorithm A: select one of two calculations

Algorithm A: a two-way selectionStart; input integer x; test x greater than or equal to 12. Yes: set y to x minus 3. No: set y to x plus 4. Both paths join, output y, then end. Execute only one branch. The article supplies all instruction and trace-table data in native text. 2026-10-06T14:45:09.285967 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ Algorithm A: choose one branch Start Input x x ≥ 12? y ← x + 4 y ← x − 3 Output y End No Yes
Original selection flowchart. Yes and No labels identify the branches independently of colour.

Complete text alternative

  1. Start.
  2. Input integer x.
  3. Test whether x ≥ 12.
  4. If Yes, assign y ← x − 3. If No, assign y ← x + 4. Execute only one of these assignments.
  5. Output y.
  6. End.

The threshold belongs to the Yes branch because the condition includes equality. Both branches assign y before it is output. There is no loop in this diagram: the joined arrows continue to output, not back to the decision.

Worked analysis 1: trace the three boundary cases

Use x = 11, x = 12 and x = 15 as three independent runs. Reset the variables at the start of each run; do not carry y from the previous input into the next.

Full selection traces. “Not set” means y has not yet been assigned in that run.
Input case Executed step x y Decision or output
11 Input 11 Not set No output
11 Test x ≥ 12 11 Not set No
11 y ← x + 4 11 15 No output
11 Output; end 11 15 Output 15
12 Input 12 Not set No output
12 Test x ≥ 12 12 Not set Yes
12 y ← x − 3 12 9 No output
12 Output; end 12 9 Output 9
15 Input 15 Not set No output
15 Test x ≥ 12 15 Not set Yes
15 y ← x − 3 15 12 No output
15 Output; end 15 12 Output 12

The outputs, in input order, are 15, 9 and 12. Input twelve is especially informative: it distinguishes “at least twelve” from “greater than twelve”. Do not assume that a larger input always gives a larger output across the threshold; the two branches use different calculations.

Algorithm B: sum the first n positive multiples of three

For this algorithm, n is a non-negative integer. The variable i counts the next term to add; total stores the accumulated sum. For n = 3, the intended terms are 3, 6 and 9. For n = 0, there are no terms and the intended sum is zero.

Algorithm B: pre-test sum loopStart; input non-negative integer n; set i to 1 and total to 0; test i less than or equal to n. Yes: add 3 times i to total, increment i by 1, then return to the test. No: output total and end. Test before each body pass. The article supplies all instruction and trace-table data in native text. 2026-10-06T14:45:09.360200 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ Algorithm B: pre-test loop Start Input n (n ≥ 0) i ← 1; total ← 0 i ≤ n? total ← total + 3 × i i ← i + 1 Output total End Yes Return to test No
Original pre-test loop. The return arrow goes to the decision, so the latest i is tested before another addition.

Complete text alternative

  1. Start and input non-negative integer n.
  2. Assign i ← 1 and total ← 0.
  3. Test whether i ≤ n.
  4. If No, go to step 7.
  5. If Yes, assign total ← total + 3 × i.
  6. Assign i ← i + 1, then return to step 3.
  7. Output total and end.

Worked analysis 2: trace every step for n = 3

Complete execution trace after input n = 3. The output column stays empty until the No branch is taken.
Step Instruction or test i total Decision Output
1 Input n = 3 Not set Not set — —
2 i ← 1; total ← 0 1 0 — —
3 Test i ≤ n 1 0 Yes —
4 total ← total + 3 × i 1 3 — —
5 i ← i + 1 2 3 — —
6 Test i ≤ n 2 3 Yes —
7 total ← total + 3 × i 2 9 — —
8 i ← i + 1 3 9 — —
9 Test i ≤ n 3 9 Yes —
10 total ← total + 3 × i 3 18 — —
11 i ← i + 1 4 18 — —
12 Test i ≤ n 4 18 No —
13 Output total; end 4 18 — 18

The loop body runs three times, but the decision is evaluated four times. The final unsuccessful test is essential: it explains why another term is not added. The final i is 4, yet 3 × 4 is never included. At that point the path leaves the loop before the addition rectangle.

Check independently using the intended terms: 3 + 6 + 9 = 18. The intermediate totals 3 and 9 are internal states, not printed outputs. Only one output instruction is reached, so this algorithm prints one number for each input n.

Worked analysis 3: change the boundary and diagnose the error

Suppose the condition is changed from i ≤ n to i < n while everything else remains unchanged. For n = 3, the tests at i = 1 and i = 2 succeed, but the test at i = 3 fails. Only 3 and 6 are added, giving 9 instead of 18. The final required term is missing.

This is an off-by-one error relative to the stated goal. The changed algorithm may still terminate neatly; finishing does not prove it solves the intended problem. A trace with n = 1 exposes the error quickly: the altered condition fails immediately, producing 0 instead of 3.

A different error is removing i ← i + 1. With n = 3, i remains 1, so i ≤ n stays true and the body repeatedly adds 3. The output instruction is never reached. After a few rows demonstrate the repeated control state, explain the non-termination rather than filling pages with unending rows.

Three solved practice tasks

Task 1: a new selection input

Question: Trace Algorithm A for x = 8.

Solution: Input sets x to 8; y is not yet set. The test 8 ≥ 12 is No. Assign y ← 8 + 4, giving 12. Output 12 and end. The subtraction branch is not executed.

Task 2: a loop with zero passes

Question: Trace Algorithm B for n = 0.

Solution: Input n = 0; initialise i = 1 and total = 0. Test 1 ≤ 0: No. Output 0 and end. The addition and increment instructions are never reached. This is possible because the test comes before the body; moving it after the body would change this boundary behaviour.

Task 3: two passes and one final test

Question: Trace Algorithm B for n = 2, recording each state change.

Solution: Start with i = 1, total = 0. Test 1 ≤ 2: Yes; add 3, making total = 3; increment i to 2. Test 2 ≤ 2: Yes; add 6, making total = 9; increment i to 3. Test 3 ≤ 2: No; output 9. There are two body passes and three decision tests.

Frequently asked questions

What does a trace table show?
It records variable states, decisions and outputs while an algorithm is followed with specified input.
Should I execute both branches of a diamond?
No. Evaluate the condition and follow only the branch whose label matches the result.
Is an assignment the same as an output?
No. An assignment changes a stored value. Output occurs only at an output instruction.
Why record the final failed loop test?
It shows the exit condition was reached and explains why the next body pass does not happen.
Can a pre-test loop run zero times?
Yes. If its condition is false on the first test, execution skips the body.
Why test the exact threshold?
It distinguishes strict comparisons from inclusive ones, such as greater than from greater than or equal to.
What if the control variable never changes?
Check whether another change can make the condition false. In Algorithm B with positive n, removing the increment prevents normal termination.

The Cambridge International pseudocode guide is a reference for pre-condition and post-condition loop semantics, not a statement of KCSE notation requirements. Continue through the Computer Studies topical revision collection; resource contents may require membership. The diagrams, inputs, traces and answers here are original learning material.