Basic if Statements
An if-else statement creates two mutually exclusive branches: one executes if the condition is true, the other if it is false.
Choosing a Path
Programs often need to respond differently depending on a condition. An if-else statement provides exactly two possible paths: one path runs when the condition is true, and the other runs when the condition is false. These paths are called branches, and the structure is known as alternative execution.
Tracing the Decision
To trace an if-else statement, do not read both branches as though both will run. First evaluate the condition. If it is true, follow the if branch and skip the else branch. If it is false, skip the if branch and follow the else branch. After the selected branch finishes, execution continues with the code after the complete if-else statement.
What do you think happens?
Suppose the condition is true. Which messages are produced: the message in the if branch, the message in the else branch, or both?
Reveal answer
Answer: The if-branch message only.
A true condition selects the if branch. The else branch is skipped, so exactly one branch executes.
Writing the Structure
An if-else statement has an if line, a condition, and a colon. The statements belonging to that branch are placed in an indented block. The else line also requires a colon, and its statements form a second consistently indented block. The two blocks represent the alternative branches.
Reading a Complete Example
Selecting One Message
Trace the two possible outcomes of an if-else statement when the condition is true and when it is false.
Evaluate: The program evaluates the condition before entering either branch.
True result: When the condition is true, the true branch runs and the false branch is skipped.
False result: When the condition is false, the true branch is skipped and the false branch runs.
Continue: After the selected branch, execution reaches the code that follows the if-else block.
Exactly one branch runs for each condition result.
if condition: print("accepted") else: print("rejected") print("finished")
Mistakes That Break Branching
Leaving out the colon after if or else.
The syntax requires a colon after the if line and after the else line.
Fix:
Write if condition: and else:.Using inconsistent indentation inside a branch.
Statements in a block must be indented consistently.
Fix:
Give all statements belonging to the same branch the same indentation.Treating if and else as two independent paths that both run.
The branches are mutually exclusive. The condition selects exactly one of them.
Fix:
Evaluate the condition first, then trace only the matching branch.Tracing the branch without checking the condition.
The condition determines which branch executes.
Fix:
Write down the condition result before following the control flow.
Practice the Trace
Consider this if-else structure: if condition: print("first path") else: print("second path") print("afterward") Describe the output when the condition is true, then describe the output when the condition is false. Explain which branch is skipped in each case.
Hints
- Evaluate the condition before choosing a branch.
- Exactly one branch runs.
- The afterward statement is outside the if-else block, so it follows the selected branch.
When debugging an if-else statement, trace it in three passes: identify the condition result, mark the branch that executes, and then continue to the code after the block. Also check both colons and the indentation of every statement in each branch.
Key Takeaways
- An if-else statement creates two mutually exclusive branches.
- A true condition selects the if branch; a false condition selects the else branch.
- Exactly one branch always executes because the condition is either true or false.
- Tracing means evaluating the condition first, following only the selected branch, and then continuing afterward.
- The syntax requires colons after if and else, with consistently indented branch blocks.
Key Takeaways
- If-else statements provide alternative execution through two mutually exclusive branches.
- The condition determines whether the if branch or the else branch runs.
- Tracing requires evaluating the condition first and skipping the branch that does not match.
- Both branch lines require colons, and their blocks must be indented consistently.
- After one branch finishes, execution continues with the code after the if-else block.