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.
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.
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.
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.
A True Condition
Trace the output when the condition has the value true.
- 1Evaluate
The condition is true, so the true branch is selected.
- 2Choose one branch
The statement in the true branch runs. The statement in the false branch does not run.
- 3Continue
After the selected branch, execution continues with the statement after the complete if-else block.
Exactly one branch runs, followed by the statement after the if-else block.
true branch
after blockWhen 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?
Reveal 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.
false branch
after blockThe 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 likeif condition true_branch_statementWhy it failsThe syntax requires a colon after if and after else.
Do this insteadWrite if condition: and else:.
Common mistake Using inconsistent indentation inside the branches.
What it looks likeif condition: true_branch_statement another_statementWhy it failsBoth blocks must be indented consistently so the branch statements are grouped correctly.
Do this insteadUse the same indentation for statements belonging to the same branch.
Common mistake Tracing both branches as if both execute.
What it looks likeTreating the true-branch output and false-branch output as simultaneous results.
Why it failsThe branches are mutually exclusive. Exactly one branch executes because the condition is either true or false.
Do this insteadEvaluate the condition first and follow only the matching branch.
Common mistake Reversing the intended branch logic.
What it looks likePutting the action intended for a false condition in the true branch.
Why it failsThe branch selected by the condition determines which statements execute, so swapping branch contents changes the program's response.
Do this insteadCheck what should happen for both condition outcomes, then place each action in the matching branch.
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
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.
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
- 1An if-else statement creates two mutually exclusive branches.
- 2Exactly one branch executes: the true branch when the condition is true, or the false branch when the condition is false.
- 3Tracing begins by evaluating the condition and then following only the matching branch.
- 4After the selected branch finishes, execution continues with statements after the complete if-else block.
- 5The 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.