Understand Boolean Expressions
The most useful tracing question is: What is the condition's Boolean result? Once that result is known, the executing branch is determined.
Try it: if / elif / else
How Python tests if / elif conditions from the top, runs only the first true branch, and how and / or stop evaluating as soon as the answer is known (short-circuiting).
How it works
- Python evaluates the if condition first.
- If it is False, it tries each elif in order; the first True one runs and the rest are skipped without being tested.
- If none is True, the else branch runs.
- and stops at the first False, or stops at the first True — the right side may never be evaluated.
Default run (5 steps): score = 84; A = 90, B = 80, C = 70. Python tests the conditions from the top. … print(grade) shows B.
Loading the simulation…
One Decision, Two Paths
Many programs need to choose between two alternatives. An if-else statement makes this choice by using a condition. If the condition is true, one block of code runs. If the condition is false, the other block runs. This is called alternative execution because the program follows one of two possible execution paths.
Evaluate Before Executing
Python does not choose a branch at random and does not execute both blocks. It first evaluates the condition. A condition is a Boolean expression, so its result is either True or False. A True result sends execution into the block under if. A False result sends execution into the block under else. After the selected block finishes, execution continues with the statement that follows the if-else statement.
The most useful tracing question is: What is the condition's Boolean result? Once that result is known, the executing branch is determined.
Writing the Two Blocks
An if-else statement has two code blocks. The first follows if and runs when the condition is true. The second follows else and runs when the condition is false.
The structure is ordered: write if, then the condition, then a colon. Put the first block at the required indentation. Next write else followed by a colon, and put the second block at its indentation. The colons and indentation show Python where the statement and each block begin.
Tracing an Even-or-Odd Decision
Choosing a Branch for an Input
Trace an if-else statement that classifies an input as even or odd.
- 1Start with the input
Use the given number as the input for the even-or-odd example.
- 2Evaluate the condition
Determine whether the condition about the input is True or False before considering either block.
- 3Choose one branch
If the condition is True, follow the if block. If the condition is False, skip the if block and follow the else block.
- 4Continue afterward
Exactly one block has executed, so tracing continues with the statement after the if-else statement.
The condition determines the result: a True condition selects the if block, and a False condition selects the else block.
The important tracing habit is to separate the stages. First identify the input. Next evaluate the condition. Finally follow only the branch that matches the result. Do not trace statements from the skipped branch as if they had executed.
Common Branching Mistakes
Common mistake Leaving out the colon after the condition.
What it looks likeThe condition is written without the colon that should end its line.
Why it failsThe required if-else structure includes a colon after the condition.
Do this insteadEnd the condition line with a colon.
Common mistake Leaving out the colon after else.
What it looks likeThe alternative branch is written as else without a colon.
Why it failsThe else part also requires a colon before its block.
Do this insteadWrite else followed by a colon.
Common mistake Using incorrect indentation for a block.
What it looks likeStatements intended to belong to the if or else block are not placed at the required indentation.
Why it failsProper indentation identifies the code belonging to each block.
Do this insteadKeep the statements in each block properly indented.
Common mistake Assuming both blocks execute.
What it looks likeTracing statements from the if block and the else block for one condition.
Why it failsAn if-else statement executes exactly one of its two blocks.
Do this insteadEvaluate the condition first, then trace only the matching branch.
Common mistake Choosing a branch before evaluating the condition.
What it looks likeFollowing the if block simply because it appears first.
Why it failsPython evaluates the condition before selecting a branch.
Do this insteadRecord whether the condition is True or False before tracing execution.
Branch Selection Practice
What do you think happens?
A condition in an if-else statement evaluates to False. Which block executes?
Reveal answer
Only the else block
Python evaluates the condition first. When the result is False, it skips the if block and executes the else block.
For an if-else statement whose condition evaluates to True, describe the execution path in order. Include the condition evaluation, the selected block, the skipped block, and what happens after the selected block finishes.
Hints
- Begin by naming the Boolean result.
- Remember that the block under else is skipped when the condition is True.
- Finish by stating that execution continues after the if-else statement.
Inspect the structure of an if-else statement and list the four syntax features you should check: the condition, the colon after the condition, the colon after else, and the indentation of both blocks.
Hints
- Check the line beginning with if.
- Check the line beginning with else.
- Check the indentation beneath each branch.
Key Takeaways
- 1Alternative execution uses if-else to choose between two code blocks.
- 2Python evaluates the condition before selecting a branch.
- 3A True condition executes the if block, while a False condition executes the else block.
- 4Exactly one block executes for each if-else statement.
- 5Correct syntax requires a colon after the condition and after else, along with proper indentation for each block.
At a glance
- An if-else statement provides two alternative execution paths.
- The condition is a Boolean expression that evaluates to True or False.
- Trace the statement by evaluating the condition first and then following the matching branch.
- The if block runs for True; the else block runs for False.
- Colons and proper indentation are required parts of the structure.