Full teaching narration is free with Private Starter.Create free account
Back to curriculum
Computer ScienceGrade 7· U.S. National — Common Core & NGSS
Aligned to:U.S. educational frameworks

Tracing and Debugging a Conditional Algorithm

Students trace a flowchart with sample inputs, identify a logic error in its conditional steps, and revise the algorithm to produce the intended outputs.

Tracing and Debugging a Conditional Algorithm

Illustrations are auto-generated and may be placeholders. They can be refreshed to match the narration.

Full teaching narration is included free with a Private Starter account.Create free account

Define Inputs, Conditions, and Outputs

An algorithm is a set of steps for solving a problem. In this example, a rewards program assigns a level based on a student’s points. The input is the number of points. A condition is a true-or-false question about that input, such as “Are the points at least 80?” The output is the assigned level. The intended rules are Gold for 80 or more points, Silver for 50 through 79 points, and Bronze for fewer than 50 points. For example, an input of 90 should produce Gold. An input of 65 should produce Silver, and an input of 40 should produce Bronze. Clearly defining these expected results gives us a goal for tracing and debugging the algorithm.

A rewards diagram shows point inputs passing through a true-or-false condition to Gold, Silver, or Bronze outputs.
A rewards diagram shows point inputs passing through a true-or-false condition to Gold, Silver, or Bronze outputs.Source: Illustrated for this lesson

Trace the Algorithm by Hand

Tracing means following an algorithm one step at a time with a specific input. The flawed algorithm first asks, “Are the points at least 50?” If yes, it outputs Silver and stops. If no, it asks, “Are the points at least 80?” If yes, it outputs Gold; otherwise, it outputs Bronze. Trace an input of 90. Because 90 is at least 50, the first condition is true. The algorithm immediately outputs Silver and stops, so it never checks whether 90 is at least 80. Now trace 40. The first condition is false, and the second condition is also false, so the output is Bronze. Recording each decision helps reveal exactly how the algorithm behaves.

The flawed flowchart traces 90 to Silver and 40 to Bronze through Yes and No branches.
The flawed flowchart traces 90 to Silver and 40 to Bronze through Yes and No branches.Source: Illustrated for this lesson

Compare Expected and Actual Results

To test an algorithm, compare its actual outputs with the outputs required by the rules. A trace table makes mismatches easy to notice. For an input of 40, the expected output is Bronze, and the flawed algorithm also produces Bronze. For 65, the expected output is Silver, and the algorithm produces Silver. For 90, however, the expected output is Gold, while the actual output is Silver. This mismatch proves that the algorithm has a logic error even though it works for some inputs. Testing only 40 or 65 might incorrectly suggest that everything is correct. Useful test inputs should represent every range, especially values near boundaries such as 49, 50, 79, and 80.

A trace table compares expected and actual rewards for several inputs and highlights the mismatch for 90.
A trace table compares expected and actual rewards for several inputs and highlights the mismatch for 90.Source: Illustrated for this lesson

Locate the Logic Error

A logic error occurs when an algorithm runs but follows rules that do not produce the intended result. Here, the first condition checks whether the points are at least 50. Every Gold score is also at least 50, so scores of 80 or more are sent to Silver immediately. The later Gold condition can never be reached by a score that qualifies for Gold. This is called an unreachable branch. The comparisons themselves are reasonable, but their order is wrong. To locate the error, ask where the first incorrect decision occurs during the trace. For an input of 90, the problem happens at the first decision because its Yes branch assigns Silver too soon. The algorithm must check the more restrictive Gold condition before the broader Silver condition.

The flawed flowchart highlights the first decision sending a 90 to Silver and leaving the Gold path unreachable.
The flawed flowchart highlights the first decision sending a 90 to Silver and leaving the Gold path unreachable.Source: Illustrated for this lesson

Revise and Retest the Algorithm

Revise the algorithm by checking the highest point range first. The corrected steps ask, “Are the points at least 80?” If yes, output Gold. If no, ask, “Are the points at least 50?” If yes, output Silver; otherwise, output Bronze. Retest the same samples to confirm the repair. An input of 90 now takes the first Yes branch and produces Gold. An input of 65 answers No to the Gold question and Yes to the Silver question, producing Silver. An input of 40 answers No to both questions and produces Bronze. Also test boundary values: 80 should be Gold, 50 should be Silver, and 49 should be Bronze. Careful retesting shows whether the revision fixed the error without creating a new one.

The corrected flowchart places the Gold check before the Silver check and shows successful tests for each reward level.
The corrected flowchart places the Gold check before the Silver check and shows successful tests for each reward level.Source: Illustrated for this lesson