Understanding Boolean Expressions and Comparison Operators
An if statement checks a boolean condition and executes an indented block only if that condition is true.
Why Programs Need Decisions
A program that performs exactly the same actions every time cannot respond to different situations. An if statement gives a program a decision point: it checks whether a condition is true, then either runs a selected block of code or skips that block.
What do you think happens?
Suppose x has a value greater than zero. What happens when Python reaches an if statement whose condition is x > 0?
Reveal answer
Answer: The indented block runs
The condition x > 0 evaluates to true when x is greater than zero. Python executes the indented block immediately following the if header.
Tracing the Decision Point
Python first evaluates the condition. If x > 0 is true, it runs the indented print statement. If the condition is false, Python skips the entire indented block and continues with the next statement after the block. In either case, execution eventually reaches the statement at the outer indentation level.
When x is greater than 0:
x is positive
Finished checking
When x is not greater than 0:
Finished checkingThe Three-Part If Header
An if statement has three essential parts in order: the if keyword, a condition that evaluates to true or false, and a colon. The colon marks the end of the header and is followed by the indented statements that belong to the if block.
In the example if x > 0:, if is the keyword, x > 0 is the condition, and the colon completes the header. The condition is a boolean expression because it produces either true or false. Without the colon, Python reports a syntax error.
The condition age >= 18 checks whether age is at least 18. That detail matters: using age > 18 would exclude someone whose age is exactly 18. A condition should express the rule you actually intend to check.
Indentation Defines Membership
An if statement is a compound statement because it spans the header and the lines that form its block. Python uses indentation to determine which statements belong to the if block. Statements indented after the colon run only when the condition is true. A statement aligned with the if keyword is outside the block and runs regardless of whether the condition is true or false.
Mistakes Beginners Make
Leaving out the colon
The colon is required to mark the end of the if header.
Fix:
Write if x > 0: before the indented block.Failing to indent the block
Python uses indentation to determine which statements belong to the if block.
Fix:
Indent the statement following the colon.Indenting a statement that should run afterward
The second print statement is part of the if block, so it is skipped when the condition is false.
Fix:
Align a statement with the if keyword when it should run after the block regardless of the condition.Writing a condition that excludes a boundary value
This condition does not include age 18.
Fix:
Use age >= 18 when the intended rule is that someone is eligible at age 18 or older.
Mental Tracing Practice
Before running an if statement, trace it line by line. First identify the condition. Next decide whether it is true or false for the current values. Then determine whether the indented block runs or is skipped. Finally, check which statements appear after the block at the outer indentation level.
Checking an Age Rule
Predict which statement runs for age = 18 in this code: if age >= 18: followed by an indented eligibility message, then a statement aligned with if.
Identify the condition: The condition is age >= 18.
Evaluate the condition: Because the rule includes 18, the condition is true when age is 18.
Trace the block: The indented eligibility message runs because the condition is true.
Continue afterward: The statement aligned with if is outside the block, so it runs after the if block.
Both the indented eligibility message and the following outer-level statement run.
Write an if statement that checks whether x is greater than zero. When the condition is true, make the indented block print that x is positive. Add a second print statement outside the block so it runs afterward in either case.
Hints
- Begin with the if keyword.
- Use x > 0 as the condition and place a colon at the end of the header.
- Indent only the message that should run when the condition is true.
Key Takeaways
- An if statement makes a decision by evaluating a boolean condition as true or false.
- The if 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 it.
- Indentation determines which statements belong to the if block and which statements run afterward.
- Trace an if statement by evaluating its condition first, then following the appropriate program path.