Computer Science
Grade 9
20 min
Creating Our Own If-Then Statements
Design original 'if-then' statements based on everyday situations.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define and use boolean operators (AND, OR, NOT) to combine multiple conditions.
Construct complex conditional statements using nested if-else blocks.
Implement if-elif-else chains to handle multiple exclusive conditions.
Write functions that encapsulate conditional logic and return a boolean value (True or False).
Translate real-world rules and policies into programmatic if-then logic.
Debug complex conditional statements by tracing the flow of execution.
Ever wonder how a game decides if you've leveled up or unlocked a secret achievement? 🎮 It's not magic; it's custom-built if-then logic that you can create yourself!
In this lesson, we'll move beyond simple checks and learn how to create our own powerful, multi-part if-then statemen...
2
Key Concepts & Vocabulary
TermDefinitionExample
Boolean ExpressionA piece of code that evaluates to one of two possible values: True or False. It's the fundamental building block of any if-statement.In `age = 15`, the expression `age >= 13` evaluates to `True`.
Boolean OperatorsSpecial keywords (`and`, `or`, `not`) used to combine or modify boolean expressions.`is_student and has_id_card` requires both expressions to be True. `is_weekend or is_holiday` requires at least one to be True.
Nested ConditionalAn if-then statement that is placed inside another if-then statement. This allows for more complex, layered checks.First, check `if user_is_logged_in:`. If that's true, you can then check `if user_is_admin:` inside it.
if-elif-else ChainA structure used to check a series of mutually exclusive condition...
3
Core Syntax & Patterns
Combining Conditions Pattern
if (condition_A and condition_B) or not condition_C:
Use parentheses `()` to group conditions and boolean operators `and`, `or`, `not` to build a single, complex logical expression. This is often cleaner than nesting multiple `if` statements.
The if-elif-else Pattern
if condition1:
# code block 1
elif condition2:
# code block 2
else:
# code block 3
Use this pattern to handle a sequence of checks. The conditions are evaluated in order from top to bottom. As soon as one is found to be `True`, its code block is executed, and the rest of the chain is skipped.
The Boolean Function Pattern
def is_condition_met(param1, param2):
# ... complex logic ...
return True_or_False_result
Encapsulate complex logic inside a function that returns...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A library has a fine system: $1 for books 1-14 days overdue, $5 for books 15-30 days overdue, and a $10 flat fee plus suspension for books over 30 days overdue. Which boolean function correctly determines if a user's account should be suspended?
A.def should_suspend(days_overdue):
return days_overdue > 14 and days_overdue < 30
B.def should_suspend(days_overdue):
return days_overdue > 30
C.def should_suspend(days_overdue):
return days_overdue >= 30
D.def should_suspend(days_overdue):
return days_overdue > 1 or days_overdue > 30
Challenging
Analyze this code and determine the final value of `status`. `access_level = 3; is_secure = False; is_admin = False; if access_level > 5 or is_admin: if not is_secure: status = 'A'; else: status = 'B'; else: if access_level > 2 and not is_secure: status = 'C'; else: status = 'D';`
A.C
B.A
C.D
D.B
Challenging
A programmer wants to grant access if a user is an employee OR a VIP member. They wrote: `if is_employee and is_vip: grant_access()`. A VIP who is not an employee cannot get access. What is the bug and how should it be fixed?
A.The bug is using `and` instead of `not`. It should be `if not is_employee...`
B.There is no bug; the code is correct for the requirement.
C.The bug is using `if` instead of `elif`. It should be `elif is_employee...`
D.The bug is using `and` instead of `or`. It should be `if is_employee or is_vip:`
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free