Concepts / Understanding Boolean Conditions

Understanding Boolean Conditions

An if-else statement creates two mutually exclusive branches: one executes if the condition is true, the other if it is false.

  • Programming

Two Possible Paths

Programs often need to respond differently depending on a condition. An if-else statement provides two alternative paths: one branch runs when the condition is true, and the other branch runs when the condition is false. These branches are mutually exclusive, so exactly one of them executes.

conditiontrue or falsetrue branchcondition is truefollowing statementfalse branchcondition is falsetruefalsethenthen
What happens to program control when a Boolean condition is true versus false, and why does exactly one branch execute?

Reading the Structure

An if-else statement has four important parts. First comes if, followed by a condition. The condition determines which path is selected. A colon follows the if condition, and the consistently indented statements beneath it form the true branch. The else part introduces the alternative branch; else also requires a colon, and its consistently indented statements form the false branch. Statements after both branches belong to the code that follows the complete if-else statement.

python
ifstarts conditionconditionevaluated first:ends headertrue branchindented statementselsealternative pathfalse branchindented statementsfollowing statementafter if-else
What contains the condition, true branch, false branch, and the statements that follow the complete if-else statement?

Tracing a Selected Branch

To trace an if-else block, do not read both branches as if both will run. Evaluate the condition first. If its result is true, follow only the true branch. If its result is false, follow only the false branch. After the selected branch finishes, continue with the statements that follow the complete if-else statement.

Worked example

A True Condition

Trace the output when the condition has the value true.

  1. 1
    Evaluate

    The condition is true, so the true branch is selected.

  2. 2
    Choose one branch

    The statement in the true branch runs. The statement in the false branch does not run.

  3. 3
    Continue

    After the selected branch, execution continues with the statement after the complete if-else block.

Result

Exactly one branch runs, followed by the statement after the if-else block.

python
Output
true branch
after block
evaluate conditiontrue branchselected when trueafter blockfalse branchselected when falsetruefalsethenthen
How does execution move from evaluating the condition to the selected branch and then continue after the if-else block?

When the Condition Is False

What do you think happens?

A condition is false. Which branch executes, and does the statement after the if-else block still execute?

  • Only the true branch executes

  • Only the false branch executes

  • Both branches execute

  • Neither the selected branch nor the following statement executes

Reveal answer
Answer:

Only the false branch executes, and execution then continues with the statement after the complete if-else block.

The false result selects the false branch. The two branches are mutually exclusive, and tracing continues after the selected branch.

python
Output
false branch
after block

The false case follows the same tracing method as the true case. First evaluate the condition, then enter only the branch that matches the result. The existence of an else branch ensures that the false outcome has its own path instead of being ignored.

Mistakes in Branching Logic

  • Common mistake

    Leaving out the colon after if or else.

    What it looks like
    if condition
        true_branch_statement
    Why it fails

    The syntax requires a colon after if and after else.

    Do this instead

    Write if condition: and else:.

  • Common mistake

    Using inconsistent indentation inside the branches.

    What it looks like
    if condition:
        true_branch_statement
          another_statement
    Why it fails

    Both blocks must be indented consistently so the branch statements are grouped correctly.

    Do this instead

    Use the same indentation for statements belonging to the same branch.

  • Common mistake

    Tracing both branches as if both execute.

    What it looks like

    Treating the true-branch output and false-branch output as simultaneous results.

    Why it fails

    The branches are mutually exclusive. Exactly one branch executes because the condition is either true or false.

    Do this instead

    Evaluate the condition first and follow only the matching branch.

  • Common mistake

    Reversing the intended branch logic.

    What it looks like

    Putting the action intended for a false condition in the true branch.

    Why it fails

    The branch selected by the condition determines which statements execute, so swapping branch contents changes the program's response.

    Do this instead

    Check what should happen for both condition outcomes, then place each action in the matching branch.

Best practice

When debugging an if-else statement, write down the condition's result before reading either branch. Then mark the selected branch, cross out the unselected branch for that trace, and continue at the first statement after the complete if-else block.

Branch Tracing Practice

EASY

Trace this statement without running it. Decide which branch executes and what appears after the block when the condition is false: if condition: true branch statement; else: false branch statement; following statement.

Hints
  • Start by recording the condition as false.
  • Select the false branch and ignore the true branch for this trace.
  • After that branch, include the following statement.
MEDIUM

Rewrite an if-else block that is missing the colon after else and has inconsistent indentation. Then explain which statements belong to the true branch and which belong to the false branch.

Hints
  • Check both if and else for their required colons.
  • Make the statements within each branch consistently indented.
  • Trace each possible condition result separately.

Key Takeaways

  1. 1
    An if-else statement creates two mutually exclusive branches.
  2. 2
    Exactly one branch executes: the true branch when the condition is true, or the false branch when the condition is false.
  3. 3
    Tracing begins by evaluating the condition and then following only the matching branch.
  4. 4
    After the selected branch finishes, execution continues with statements after the complete if-else block.
  5. 5
    The syntax requires a colon after if and else, and both branch blocks must be indented consistently.

At a glance

  • Boolean conditions select between two alternative execution paths.
  • The true and false branches are mutually exclusive, so exactly one executes.
  • To trace a block, evaluate the condition first, follow the matching branch, and then continue after the block.
  • Correct colons and consistent indentation are required for the if-else structure.
  • Testing both condition outcomes helps reveal reversed logic and other branching mistakes.

More concepts starting with U