Using else and elif for Multiple Conditions
An if statement checks a boolean condition and executes an indented block only if that condition is true.
The Decision Path
Programs become useful when they can respond to different situations instead of running exactly the same code every time. An if statement creates a decision point: Python checks a condition, runs an indented block when that condition is true, and skips that block when the condition is false. This is the foundation for tracing programs that later need to distinguish among multiple conditions.
Tracing a Condition
When Python encounters an if statement, 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 indented block immediately following the colon. A false result causes Python to skip the entire indented block and continue with the next statement after that block.
Checking Whether a Number Is Positive
Trace the decision made when x has the value 4.
Read the condition: The condition is x > 0. With x equal to 4, the comparison asks whether 4 is greater than 0.
Evaluate the condition: The condition is true.
Choose the block: Because the condition is true, Python executes the indented statement belonging to the if block.
The message x is positive is printed.
x is positive
Decision completeWhat do you think happens?
Suppose x is -2 in the same structure. What happens to the indented print statement?
Reveal answer
Answer: It is skipped because x > 0 is false.
Python evaluates the condition first. When the result is false, it skips the entire indented block and continues with the next statement after the if block.
Reading the Header
An if statement is a compound statement. Its header contains three essential parts in order: the if keyword, a condition that evaluates to true or false, and a colon. The statements controlled by that header follow on indented lines.
if x > 0: print('x is positive')
Indentation as Grouping
Indentation is part of Python syntax, not merely a formatting preference. Statements indented under the header belong to the if block. Multiple statements with the same indentation belong to that block together. A statement aligned with the if keyword is outside the block, so it runs after the if statement regardless of whether the condition was true or false.
In this example, the two indented print statements are grouped together. If temperature > 20 is true, both run. If it is false, both are skipped. The final print statement is aligned with if, so it is outside the block and runs after the if statement in either case.
Mistakes to Avoid
Leaving out the colon after the condition.
The colon is one of the required parts of the if header.
Fix:
if x > 0:Failing to indent the controlled statement.
Python uses indentation to determine which statements belong to the if block.
Fix:
if x > 0: print('x is positive')Assuming a false condition runs the indented block anyway.
A false condition causes Python to skip the entire indented block.
Fix:
Trace the condition first, then decide whether the block executes.Using a condition that does not express the intended boundary.
The condition age > 18 excludes a person whose age is exactly 18.
Fix:
Use age >= 18 when 18-year-olds should be included.
Mental Trace Practice
For each case, identify the condition, decide whether it is true or false, and state whether the indented block runs: first consider x = 7 for x > 0, then consider x = -1 for x > 0.
Hints
- Evaluate the comparison using the stated value of x.
- A true condition runs the indented block.
- A false condition skips the indented block.
Before running an if statement, trace it line by line. Ask what the condition is, whether it is true or false for the current values, and which indented statements belong to the block. Keep the condition clear and directly connected to the decision you want the program to make.
Key Takeaways
- An if statement checks a boolean condition and makes a decision based on whether that condition is true or false.
- Its header requires the if keyword, a condition, and a colon in that order.
- A true condition runs the following indented block; a false condition skips that block and continues with the next statement.
- Indentation defines which statements belong to the compound if block.
- Clear condition tracing is the starting point for reasoning about more than one possible program path.
Key Takeaways
- An if statement evaluates a boolean condition before deciding what to execute.
- The required header parts are the if keyword, the condition, and the colon.
- Indentation groups one or more statements into the if block.
- When the condition is false, Python skips the entire indented block and continues after it.
- Careful mental tracing provides the foundation for understanding multiple decision paths.