Concepts / Debugging Conditional Logic

Debugging Conditional Logic

An if statement checks a condition. Its indented code executes only when that condition is true.

  • Programming
Interactive lab

Try it: Debug by Tracing Execution

How to debug a program that runs but gives the wrong answer: compare expected and actual output, trace the lines in the order they really run with a variable watch table (or an inserted debug print), find the first line where a value goes wrong, then choose a fix and re-run to check it.

How it works

  1. State the expected output from the specification, run the program and compare: a difference is the evidence you debug from.
  2. Trace in execution order, not file order: loops repeat lines, if/elif picks one branch, and the watch table records every variable before each line runs.
  3. An inserted print('debug: ...', value) shows the same state in the output; it must not change what the program itself prints.
  4. Look for the first place a value differs from what it should be (an off-by-one range, > instead of >=, a total reset inside the loop, two names for one list, a str from input()). Where the wrong value shows up is only the symptom.
  5. Change one thing with a reason, re-run, and compare again: a fix that makes an error disappear can still leave the output wrong.

Default run (14 steps): Sum 1..n: Add up the whole numbers from 1 to n (n included) and print the sum. Press Play to trace it line by line. … Finished, but the output is wrong: got sum of 1 to 5 is 10, expected sum of 1 to 5 is 15.

Simplified: The lab's own small Python interpreter runs fixed, hand-written programs (you pick the program, the fix and a value; your text is never executed). Every program the lab can show was run in CPython 3.12 and the lab reproduces its output, its traceback text and its line-by-line trace. Six short programs, one editable value each; input() receives the value you type in the lab. Lines starting with "debug:" are ignored when comparing the output with the expected output.

Educational simulation

Loading the simulation…

The Execution Question

When an if statement produces an unexpected result, the most useful question is not simply whether the statement looks correct. Ask which condition the program evaluated, whether that condition was true or false, and which indented lines were therefore allowed to execute. Conditional debugging is the process of tracing that decision and comparing the actual path with the path you expected.

Key point

An if statement checks a condition. Its indented code executes only when that condition is true.

Tracing Both Paths

An if statement creates a decision point in the execution path. First, the condition is checked. If the condition is true, execution enters the indented block and runs its statements. If the condition is false, the indented block is skipped. Tracing means recording this decision before deciding which lines execute next.

Program startConditiontrue or falseIndented bodyrunsContinueNext statementbody skippedtruefalse
What path does the program take when the condition is true versus when it is false?
Worked example

Tracing a true condition

Determine which lines execute when the condition is true.

  1. 1
    Check

    The if statement evaluates its condition. In this example, the condition is true.

  2. 2
    Enter

    Because the condition is true, execution enters the indented body.

  3. 3
    Continue

    After the indented body finishes, execution continues with the next statement.

Result

The indented statements execute. If the same condition were false, those statements would be skipped.

Syntax and Block Boundaries

A correctly formed if statement has a condition followed by a colon. The statements controlled by that condition must appear in the indented code block. Indentation therefore identifies which statements belong to the if statement. A statement that is not indented as part of the block is outside that controlled section.

python
ifconditional keywordconditiondecision being checked:ends conditionindented statementinside blocknext statementoutside block
Which statements are contained inside the if block, and which statement executes afterward?

Following the Condition

conditionevaluatedtrueindented codeexecutesfalseindented codeskippedevaluates asallowsevaluates ascauses
How does the condition's truth value determine whether the indented code runs or is skipped?

The condition is the control point, not the indented statement itself. If the result is true, the indented code runs. If the result is false, the indented code is skipped. To debug an unexpected result, write down the condition's actual truth value first, then mark each indented line as either executed or skipped.

Code walkthrough
score = 4
if score:
    result = "inside"
result = "after"
  1. 1
    The program reaches the if statement and checks the condition.
  2. 2
    If the condition is true, execution enters the indented statement.
  3. 3
    Execution then reaches the statement outside the block.
  • The condition uses score.
  • If the condition is true, the indented assignment runs.
  • The final assignment is outside the indented block.

Finding the Divergence

Debugging becomes systematic when you compare two paths: the expected path and the actual path. Locate the if statement where they first differ. Then inspect the condition's value at that point. If the condition is false when you expected it to be true, the indented block is skipped. If the condition is true when you expected it to be false, the block executes. This first divergence is the place to investigate.

condition trueindented bodyexecutescondition falsenext statementbody skippedtruefalse
Where does execution diverge from the expected path, and what condition value caused the divergence?
Worked example

A skipped block

A learner expects the indented block to execute, but the program continues to the next statement.

  1. 1
    Compare paths

    The expected path enters the indented block, while the actual path skips it.

  2. 2
    Find the decision

    The paths first diverge at the if statement.

  3. 3
    Check the condition

    The actual condition value was false, so the indented code was skipped.

  4. 4
    Correct the investigation

    Inspect why the condition was not true rather than assuming that the indented code executed.

Result

The condition's false value explains the skipped block and identifies the if statement as the first place to debug.

  • Common mistake

    Assuming the indented body always executes

    What it looks like

    Treating the statements inside an if block as unconditional statements

    Why it fails

    The indented body executes only when the condition is true.

    Do this instead

    Determine the condition's truth value before tracing the body.

  • Common mistake

    Ignoring the colon

    What it looks like

    Writing the condition without the colon that begins the block

    Why it fails

    Correct if syntax requires a colon after the condition.

    Do this instead

    Check the end of the condition before checking the body.

  • Common mistake

    Treating indentation as decoration

    What it looks like

    Reading an indented statement as though it were outside the if block

    Why it fails

    Indentation identifies the code controlled by the condition.

    Do this instead

    Mark the indented statements as the conditional block and trace them only on the true path.

  • Common mistake

    Debugging from the final result alone

    What it looks like

    Skipping the condition and inspecting only the statement that produced the unexpected result

    Why it fails

    The important divergence may have happened when the condition selected a different path.

    Do this instead

    Trace from the if statement, record the condition value, and identify the first different execution step.

Practice the Trace

EASY

Trace this conditional statement without running it. State the condition's truth value, whether the indented statement executes or is skipped, and which statement is reached afterward.

Hints
  • Start at the if statement and evaluate its condition.
  • A true condition enters the indented block; a false condition skips it.
  • Check the indentation before deciding whether a statement belongs to the block.
  1. 1
    Locate the if statement.
  2. 2
    Check the colon after the condition.
  3. 3
    Record whether the condition is true or false.
  4. 4
    Run the indented block only on the true path.
  5. 5
    Continue tracing after the block.
  6. 6
    Compare the traced path with the expected behavior and investigate the first divergence.

Key Takeaways

  1. 1
    An if statement checks a condition and executes its indented code only when the condition is true.
  2. 2
    A colon must follow the condition, and indentation defines the controlled block.
  3. 3
    Tracing an if statement means recording the condition's truth value and following the corresponding execution path.
  4. 4
    To debug unexpected behavior, find the first place where the actual path differs from the expected path.
  5. 5
    The condition's value explains whether the indented code ran or was skipped.

At a glance

  • An if statement uses a condition to control execution.
  • True conditions run the indented block; false conditions skip it.
  • The colon and indentation are required parts of the if statement's syntax.
  • Debugging starts by tracing the condition and locating the first divergence from the expected path.

Related concepts

More concepts starting with D