IB COMPUTER SCIENCE | B1.1.4 Tracing Flowcharts

Lesson objective

Trace flowcharts using standard symbols, following decisions, variable updates, input consumption and output through sequence, selection and loops.

Learn

B1.1.4 | TRACING FLOWCHARTS

01 | FOLLOW THE ROUTE, NOT YOUR INSTINCT

A flowchart is a map of an algorithm. Its symbols show actions and decisions; its arrows tell you which action happens next. Tracing means acting as the algorithm’s executor, one step at a time.

The IB command term trace means to follow and record the actions of an algorithm. You may need to record changing variable values, decisions, input consumption and outputs. Guessing the final answer without following the route can miss a loop or an important boundary case.

This common SL/HL lesson starts with the symbols, then works through sequence, selection, counted repetition, pre-condition loops and post-condition loops. You can trace all five examples interactively.

02 | SIX SYMBOLS YOU NEED

START / END: a rounded terminal marks entry or termination.

PROCESS: a rectangle contains an operation, such as Total ← 0.

INPUT / OUTPUT: a parallelogram reads data or produces an output.

DECISION: a diamond tests a condition; follow the labelled branch.

FLOWLINE: an arrow shows the direction of execution.

A

CONNECTOR: a labelled circle links to the matching connector, avoiding a long or crossing line.

A shape tells you the kind of action, but its text tells you what to do. Two parallelograms can have different roles: INPUT Number reads a value; OUTPUT Number displays the current value.

03 | ARROWS AND CONNECTORS CONTROL THE PATH

Begin at START and follow each arrowhead. A decision normally has two outgoing branches, labelled YES/NO or TRUE/FALSE. Evaluate the condition using the current values, then follow only the matching branch.

The line’s direction matters more than where the next box appears on the page. A loop arrow may take you upwards to an earlier symbol. Do not jump to the nearest box or follow both outcomes of a decision.

A connector transfers flow to the matching label. For example, a line ending at connector A continues from the corresponding A elsewhere in the same chart. It does not reset variables or perform a calculation. A line crossing without a junction mark does not automatically connect two routes.

04 | VARIABLES ARE CURRENT VALUES

A variable is a named storage location whose value can change. In Count ← Count + 1, first read the old Count, add one, then store the result back in Count. If Count was 2, it becomes 3.

The arrow ← denotes assignment in these diagrams. It is not a claim that Count is mathematically equal to Count + 1. Similarly, Total ← Total + Number reads the old total and adds the current number, producing a new running total.

A decision such as Count ≤ Limit compares values; it does not change them. A variable that has not been assigned a value is “not set” in the trace, not automatically zero.

05 | INPUTS, OUTPUTS AND PROMPTS

INPUT Number consumes the next supplied input and stores it in Number. Reaching that symbol a second time consumes the next input, rather than reusing the first one. OUTPUT Number displays the value already stored and does not ask for another.

An output such as OUTPUT "Enter a number" is a prompt: it displays literal text. It is separate from reading the response. Quotation marks distinguish literal text from a variable name.

Record outputs in the order they occur, including repeated outputs. If a flowchart outputs Count three times, the output sequence matters; do not record only Count’s final value.

06 | SET UP A TRACE TABLE

Step Symbol / action Current values after action Decision or output
1 START Number not set Begin
2 INPUT Number, supplied value 4 Number = 4 Input 4 consumed
3 Double ← Number × 2 Number = 4; Double = 8 No output yet
4 Result ← Double + 1 Result = 9 No output yet
5 OUTPUT Result Values unchanged Output 9

This lesson records one row for every executed symbol, including decisions and START/END. Other tasks may provide a table that records only changes or selected steps. Follow its headings and instructions, while still checking every action mentally.

07 | SEQUENCE: DOUBLE AND ADD ONE

Sequence means actions happen in the arrow order. Read one number, double it, add one and output the result. Follow the actual assignments: do not replace the calculation with a different expression that changes the order.

SEQUENCE | DOUBLE AND ADD ONE Follow arrows from START. Decision branches are labelled YES and NO. Loop return arrows run outside the symbols. START INPUT Number Double ← Number × 2 Result ← Double + 1 OUTPUT Result END
SEQUENCE | DOUBLE AND ADD ONE. Read the accompanying numbered method and trace for a text explanation.

For input 4, Double becomes 8, Result becomes 9 and the only output is 9. For input −2, Double becomes −4 and Result becomes −3. The chart works with the supplied number; it does not assume it is positive.

08 | SELECTION: WHICH BRANCH?

Selection chooses a route using a condition. In this fictional admission rule, an age of at least 12 produces Allowed; a lower age produces Not yet. We assume a valid non-negative integer age has already been supplied.

SELECTION | ADMISSION CHECK Follow arrows from START. Decision branches are labelled YES and NO. Loop return arrows run outside the symbols. YES NO START INPUT Age Age ≥ 12? OUTPUT "Allowed" OUTPUT "Not yet" END
SELECTION | ADMISSION CHECK. Read the accompanying numbered method and trace for a text explanation.

For Age = 12, Age ≥ 12 is true, so follow YES and output Allowed. For Age = 11, it is false, so follow NO and output Not yet. Both branches rejoin at END; exactly one message is output for each run.

09 | TEST THE BOUNDARY EXACTLY

Age Age ≥ 12? Branch Output
11 False NO Not yet
12 True YES Allowed
13 True YES Allowed

The equality part of ≥ determines what happens at 12. Reading it as > would give the wrong answer at precisely that boundary. Write down the current comparison rather than answering from what you expect the rule to be.

A decision diamond is a question about the current state. It does not assign 12 to Age, and following NO does not mean the whole algorithm has failed; it selects the other defined behaviour.

10 | LOOPS: FOLLOW THE RETURN ARROW

A loop repeats a section of an algorithm. The repeated actions are the loop’s body. For example, a body might output Count and then increase Count by one. The condition determines whether to enter, repeat or leave that body.

On returning to a decision, evaluate it again using the updated values. Its answer can change. Carry the state forward; do not reset a variable unless an executed process explicitly assigns it again.

A loop is not automatically infinite because an arrow points backwards. Look for the value that changes and how that change can eventually cause an exit.

11 | COUNTED REPETITION: OUTPUT 1 TO LIMIT

This chart explicitly represents a counting loop using an initial value, a comparison and an increment. It outputs the integers from 1 through Limit, inclusive. Limit is a supplied whole number from 0 to 6 in the explorer.

COUNTED REPETITION | OUTPUT 1 TO LIMIT Follow arrows from START. Decision branches are labelled YES and NO. Loop return arrows run outside the symbols. YES NO START INPUT Limit Count ← 1 Count ≤ Limit? OUTPUT Count Count ← Count + 1 END
COUNTED REPETITION | OUTPUT 1 TO LIMIT. Read the accompanying numbered method and trace for a text explanation.

The body is OUTPUT Count followed by Count ← Count + 1. The decision is checked before the body. For Limit = 3, Count starts at 1, and the loop outputs 1, 2, 3. After the third increment Count is 4; 4 ≤ 3 is false, so the chart ends without outputting 4.

12 | A COMPLETE COUNTED-LOOP TRACE

Action Count after action Condition / new output
Initialise with Limit = 3 1 No output
Test Count ≤ Limit 1 1 ≤ 3: YES
OUTPUT Count 1 Output 1
Increment 2 No output
Test; output; increment 3 2 ≤ 3: YES; output 2; then Count becomes 3
Test; output; increment 4 3 ≤ 3: YES; output 3; then Count becomes 4
Test and leave 4 4 ≤ 3: NO; END

The table compresses repeated actions into labelled rows; the explorer shows every symbol separately. Notice the difference between the final variable value, 4, and the final output, 3.

For Limit = 0, the first comparison is 1 ≤ 0, which is false. The body runs zero times and there are no outputs. START and END are control symbols, not printed messages.

13 | PRE-CONDITION LOOP: TOTAL UNTIL ZERO

A pre-condition loop checks its continuation condition before each execution of the body. Here zero is a sentinel: an input value indicating that entry should finish. The chart adds nonzero values and outputs their total when zero is read.

PRE-CONDITION | TOTAL UNTIL ZERO Follow arrows from START. Decision branches are labelled YES and NO. Loop return arrows run outside the symbols. YES NO START Total ← 0 INPUT Number Number ≠ 0? Total ← Total + Number INPUT Number OUTPUT Total END
PRE-CONDITION | TOTAL UNTIL ZERO. Read the accompanying numbered method and trace for a text explanation.

Total is set to 0 before the first input. If Number ≠ 0, add Number to the old Total, then read another Number. Return directly to the decision, keeping the running total. When Number is 0, follow NO to OUTPUT Total and END.

14 | TRACE THE RUNNING TOTAL

Event for inputs 4, 7, 0 Number Total Effect
Initialise Not set 0 No output
Read first input 4 0 Next comparison is true
Add current number 4 4 0 + 4 stored in Total
Read next; add 7 11 4 + 7 stored in Total
Read sentinel 0 11 Condition is false; no addition
Output total 0 11 Output 11 once

Total ← Total + Number keeps what was accumulated and adds the new contribution. Total ← Number would replace the accumulated amount, losing the earlier inputs.

The sentinel is read but not added, because the false branch leaves before the addition. Zero would not change this sum anyway, but the route matters: a count of processed values must not accidentally count the sentinel.

15 | ZERO FIRST AND THE MISSING UPDATE

If the first input is 0, the condition is false immediately. The body runs zero times, and the chart outputs the initial total of 0. A pre-condition loop can therefore skip its body completely.

If the second INPUT Number inside the body were removed, Number would never change after a nonzero first input. The same true comparison and addition would repeat indefinitely.

If Total ← 0 were placed inside the repeated body, earlier contributions would be lost. Trace the arrows to see which initialisations happen once and which operations repeat.

16 | POST-CONDITION LOOP: GET A POSITIVE NUMBER

A post-condition loop performs its body before testing whether it should stop. Here the body reads Number. The decision asks whether Number > 0. YES leaves the loop; NO returns to the input.

POST-CONDITION | ACCEPT A POSITIVE NUMBER Follow arrows from START. Decision branches are labelled YES and NO. Loop return arrows run outside the symbols. NO YES START INPUT Number Number > 0? OUTPUT Number END
POST-CONDITION | ACCEPT A POSITIVE NUMBER. Read the accompanying numbered method and trace for a text explanation.

For inputs −2, 0, 5, the chart reads −2 and repeats, reads 0 and repeats, then reads 5 and exits. It outputs 5 once. The input body runs at least once; the output is outside the loop.

The stopping condition is not a continuation condition. In this chart, true means stop repeating. Always follow the labels rather than assuming YES must loop.

17 | COMPARE THE LOOP ROUTES

Feature Pre-condition total example Post-condition positive-input example
Test position Before the repeated addition/read body After the input body
Meaning of true Continue while Number ≠ 0 Stop when Number > 0
Minimum body executions Zero One
Exit arrow NO YES
Variable update Read the next Number inside the body Read Number before each test

These examples have different purposes as well as different loop structures. Do not claim they produce equivalent results for the same data. To trace any chart, locate its body, test position, updated values and actual exit branch.

18 | CONNECTORS DO NOT CHANGE VALUES

A connector is a visual continuation, not an input or processing step. If an arrow leads to connector A, resume at its matching A and retain the current values. A connector can be used to keep a long chart readable.

BEFORE CONNECTOR A

Total = 11. Follow the arrow into the circle labelled A.

MATCHING CONNECTOR A

Resume from the matching A. Total is still 11.

NEXT ACTION

OUTPUT Total displays 11. The connector has performed no arithmetic.

If labels or arrows are missing and the route is genuinely ambiguous, do not invent a jump. Check the supplied diagram or explain the ambiguity.

19 | INTERACTIVE: EXECUTE ONE SYMBOL AT A TIME

Choose a chart and enter comma-separated whole-number inputs. Use Load / restart trace to prepare a fresh run, then predict each next action before executing it. The pink symbol is the most recently executed symbol, and the table records values after that action.

Sequence accepts one input from −20 to 20; admission accepts one age from 0 to 120; counting accepts one Limit from 0 to 6. The total example needs a zero sentinel, and the positive-input example needs a positive value. Each list may contain up to eight values from −20 to 20. Values after termination are not consumed.

Changing the algorithm loads its suggested inputs. Changing inputs clears the old trace until you load them.

SEQUENCE | DOUBLE AND ADD ONE Follow arrows from START. Decision branches are labelled YES and NO. Loop return arrows run outside the symbols. START INPUT Number Double ← Number × 2 Result ← Double + 1 OUTPUT Result END
SEQUENCE | DOUBLE AND ADD ONE. Read the accompanying numbered method and trace for a text explanation.
Executed symbols and values after each action
Step Symbol / action Current values Decision / input / output

The next-step button stays below the growing trace table, so you can continue where you finish reading. This is a bounded teaching simulator, not a general flowchart interpreter.

20 | PREDICT, TRACE, THEN CHECK

In your book, predict the output and final values before loading each example: sequence with 6; admission with 11 and then 12; counting with 0 and then 3; total with 4, 7, 0; positive input with −2, 0, 5.

For each decision, write the comparison using the current values and mark its result. For each input, cross out exactly one consumed value. For each output, append it to a separate output list.

Reveal the expected results

Sequence 6: Double = 12, Result = 13, output 13. Admission 11: Not yet; admission 12: Allowed. Counting 0: no outputs, Count = 1; counting 3: outputs 1, 2, 3, final Count = 4. Total 4, 7, 0: Total = 11 and Number = 0, output 11. Positive input −2, 0, 5: Number = 5, output 5, three inputs consumed.

21 | DIAGNOSE THE ROUTE, NOT JUST THE ANSWER

A wrong trace can come from reading the wrong branch, failing to update a value, consuming the wrong input or recording a calculation as output. Locate the first incorrect action, because later values may all depend on it.

For the counting chart, changing ≤ to < with Limit = 3 removes output 3. Removing the increment prevents Count changing and creates a non-terminating loop whenever the initial comparison is true.

For the positive-input chart, changing > 0 to ≥ 0 accepts zero. That is a changed rule, not merely a different way to draw the same algorithm.

22 | COMMON TRACING MISTAKES

Mistake Correction
Resetting a variable when a loop arrow returns Only an executed assignment changes it.
Following both decision branches Evaluate the condition and follow the matching branch only.
Assuming every YES arrow repeats Read the actual branch destination.
Treating an input prompt as supplied data OUTPUT displays a prompt; INPUT reads the next value.
Printing every process result Record output only at executed OUTPUT symbols.
Giving only the final variable values Also record the actions, decisions and output sequence requested.
Counting a sentinel as ordinary data Follow the condition before processing it.

23 | TRANSFER TASK: COUNT VALUES ABOVE FIVE

Adapt the total-until-zero method on paper. Keep a running Count initially 0. For each nonzero input, increase Count only if Number > 5, then read the next input. When zero is read, output Count.

Sketch or describe the extra decision and trace inputs 4, 8, 5, 9, 0. Highlight the difference between the loop condition, which checks for the sentinel, and the selection inside the body, which decides whether to count this value.

Reveal a trace outline

Count starts at 0. Input 4 is nonzero but not above five, so Count stays 0. Input 8 increases it to 1. Input 5 does not meet > 5. Input 9 increases it to 2. Input 0 leaves the loop before the inner selection. Output 2. The algorithm consumes five inputs, counts two qualifying values and outputs once.

24 | A RELIABLE TRACING ROUTINE

Find START, list the supplied inputs and mark all variables not yet assigned. Execute one symbol, update the current state, record any decision or output, then follow its arrow. Repeat until END or until the algorithm needs unavailable input.

If no input is available, do not invent one. If the chart never reaches END, explain the repeated route and why its state cannot produce an exit.

You should now be able to recognise the standard symbols and follow sequence, selection and different loop structures, recording variables and outputs accurately.

MATCH THE FLOWCHART SYMBOL TO ITS ROLE

Terminology

Terminology

Flowchart

A diagram representing algorithm actions and control flow using symbols and arrows.

Trace

Follow and record the actions of an algorithm.

Terminal

A rounded start/end symbol.

Process symbol

A rectangle containing an operation such as assignment.

Input/output symbol

A parallelogram representing reading data or producing output.

Decision symbol

A diamond containing a condition that selects a branch.

Flowline

An arrow showing the direction of execution.

Connector

A labelled continuation linking parts of a chart without changing state.

Variable

A named storage location whose current value can change.

Assignment

Evaluating an expression and storing its result in a variable.

Initialisation

Giving a variable its starting value before it is used.

Current state

The values and relevant progress at a particular point in execution.

Condition

An expression evaluated as true or false.

Branch

A route selected as the outcome of a decision.

Sequence

Execution of actions in a specified order.

Selection

Choosing a route based on a condition.

Iteration

Repeated execution of part of an algorithm.

Loop body

The actions repeated by a loop.

Pre-condition loop

A loop testing its continuation condition before the body.

Post-condition loop

A loop testing after its body, so the body executes at least once.

Count-controlled repetition

Repetition governed by a count and a specified range or limit.

Increment

Increasing a value, commonly by one.

Sentinel

An input value marking the end of data entry rather than ordinary data.

Running total

An accumulated sum updated by adding each new contribution.

Input consumption

Reading and using the next value from the supplied input sequence.

Output sequence

The values or messages produced, in the order they occur.

Prompt

Output text asking a user to supply input.

Boundary case

A value at or close to a decision limit.

Infinite loop

A repetition that never reaches its exit under the given conditions.

Trace table

A structured record of algorithm actions, values, decisions and outputs.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. Which symbols have the correct meanings?
2. Which follow Count ← Count + 1 when Count starts at 2?
3. Which apply to Age = 12 in the admission chart?
4. Which describe the counting chart with Limit = 3?
5. Which describe Limit = 0?
6. Which describe the total chart with 4, 7, 0?
7. Which describe the positive-input chart with −2, 0, 5?
8. Which statements about connectors are correct?
9. Which can cause a non-terminating run?
10. Which are good tracing practices?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Trace the sequence chart for input 6.

2. Trace the admission chart for ages 11 and 12 in separate runs.

3. Trace the counting chart for Limit = 2.

4. Trace the counting chart for Limit = 0.

5. Trace the total chart for inputs 3, −2, 0.

6. Trace the total chart when the first input is 0.

7. Trace the positive-input chart for inputs −4, 0, 2.

8. Explain the difference between the final Count and its last output for Limit = 3.

9. Explain the effect of removing the repeated input from the total chart.

10. Explain why the post-condition chart reads at least one value.

11. Describe how a labelled connector affects a trace.

12. Trace the count-above-five transfer task for 4, 8, 5, 9, 0.

TRACE IT | CHANGE THE LIMIT

Use the counting flowchart with Limit = 4. Record every decision, the output sequence and the final Count. Then change its condition from Count ≤ Limit to Count < Limit and repeat. Explain which output disappears and why.

Reveal the result

With ≤, output 1, 2, 3, 4; Count finishes at 5. With <, output 1, 2, 3; Count finishes at 4. The condition is false at equality in the modified version, so 4 is not output.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

Selections are kept while this page is open.

Workbook

Workbook

COMING SOON

The workbook for B1.1.4 Tracing Flowcharts is coming soon. Complete the matching task and written questions in your exercise book.