Computers follow instructions, but to make programs flexible, they need to make choices based on conditions. A condition is a statement that can be true or false. These conditions help programs decide what to do next, allowing them to respond to different inputs and situations.
Using IF statements and logical operators, programmers can control the flow of a program, create smarter solutions, and check for errors. Learning how to structure decisions properly is important for writing clear, efficient, and reliable code.
I can understand what conditions and decisions are in programming and why they are important.
I can use IF...THEN...ELSE statements and logical operators to make decisions in a program.
I can recognize common mistakes when writing conditions.
I can understand how logical thinking helps create better, easier-to-maintain programs.
Condition – A Boolean expression that evaluates to either true or false.
Decision – A point in an algorithm where a choice is made based on a condition.
Logical Operators – AND, OR, and NOT, used to form more complex conditions.
Control Flow – The sequence in which program instructions are executed.
Boolean Expression – A logical statement that results in either true or false.
IF Statement – A construct that executes a block of code when a condition is met.
ELSE Statement – Provides an alternative block of code when an IF condition is false.
ELIF (Else If) Statement – Extends an IF statement with additional conditions.
Nested IF Statement – An IF statement placed inside another IF for more complex logic.
CASE/SWITCH Statement – A selection construct for handling multiple conditions efficiently.
Comparison Operators – Used in conditions (>, <, ==, !=, <=, >=) to compare values.
Ternary Operator – A shorthand for IF-ELSE conditions, written as condition ? true_result : false_result.
Short-Circuit Evaluation – Logical operators stop evaluating once the outcome is determined.
Default Case – The fallback condition in a SWITCH statement when no other case matches.
Error Handling in Conditions – Checking unexpected values to prevent crashes or incorrect behavior.
Conditions in programming are Boolean expressions that determine the flow of execution. These expressions evaluate to either true or false, allowing programs to make decisions. Statements like x > 10, y == 5, or (x > 10) AND (y < 20) control which parts of a program execute. Decision structures such as IF...THEN...ELSE help programs select the appropriate path based on conditions. This is crucial for creating dynamic and responsive software.
Control flow is guided by selection constructs, which direct execution based on specified conditions.
# Ask the user for their age
age = int(input("Enter your age: "))
# Use an IF-ELSE statement to determine if they are an adult or a minor
if age >= 18:
print("You are an adult.") # This prints if the condition is true
else:
print("You are a minor.") # This prints if the condition is false
print("Program complete.") # This always runs regardless of the condition
Explanation:
The user inputs their age.
The program checks if age is greater than or equal to 18.
If true, it prints "You are an adult.", otherwise "You are a minor.".
The final print statement always executes after the IF-ELSE block.
Logical operators like AND, OR, and NOT allow more complex conditions.
# Ask the user for their age and whether they have parental consent
age = int(input("Enter your age: "))
parental_consent = input("Do you have parental consent? (yes/no): ").lower()
# Check if the person is allowed to enter a restricted website
if age >= 18 or (age >= 13 and parental_consent == "yes"):
print("Access granted.")
else:
print("Access denied.")
Explanation:
The user enters their age and whether they have parental consent.
The program grants access if:
The user is 18 or older (age >= 18).
OR they are 13 or older AND have parental consent.
The OR (or) operator ensures at least one condition must be true.
The AND (and) operator requires both conditions to be true.
Instead of writing multiple IF-ELSE statements, a list can be used to check if a value exists within a group.
# List of valid usernames
valid_users = ["alice", "bob", "charlie"]
# Ask for a username
username = input("Enter your username: ").lower()
# Check if the username exists in the list
if username in valid_users:
print("Access granted.")
else:
print("Access denied.")
Explanation:
The list valid_users contains valid usernames.
The program checks if the entered username is in the list using if username in valid_users.
This is more efficient than using multiple IF statements for each user.
Dictionaries provide a cleaner alternative to multiple IF-ELSE conditions when mapping inputs to outputs.
# Dictionary mapping numbers to day names
days = {
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday",
7: "Sunday"
}
# Ask the user for input
day_num = int(input("Enter a number (1-7) to get the day of the week: "))
# Get the corresponding day or return "Invalid"
print("The day is:", days.get(day_num, "Invalid day"))
Explanation:
The dictionary days maps numbers to days of the week.
The .get() method retrieves the value for day_num, defaulting to "Invalid day" if the input isn't found.
This approach is more readable than writing multiple IF-ELSE conditions.
Programming decisions must be clear and well-structured to ensure maintainability and efficiency. Poorly written logic can cause bugs, unexpected errors, and hard-to-read code. Key principles include:
✔ Using logical operators to simplify conditions
✔ Using lists and dictionaries to avoid repetitive IF-ELSE blocks
✔ Handling edge cases like incorrect user input
By following these techniques, programmers can write more efficient, bug-free, and scalable code.
What is a condition in programming, and how does it help a program make decisions?
Explain the difference between an IF statement and an IF...ELSE statement.
What happens if a condition is always false in a program? Give an example.
Identify the error in this condition and correct it:
if x = 10:
print("X is ten")
Why is logical thinking important when writing conditional statements? Give an example of a poorly written condition and explain how to improve it.
For each task, refer to the numbered examples above (1-4), copy the code, modify it as instructed, and document your work. Copy the question into your workbook before answering it.
Modify the code as per the task requirements.
Copy your completed code into your workbook - do not use a screenshot.
Add the screenshot to your workbook with a title that includes the relevant structure (e.g., "Using IF-ELSE to Check Age", "Using a List for Usernames").
Write 1-2 sentences explaining the code, describing what it does and how your modification changes its behavior.
1. Expanding IF-ELSE Conditions
Modify example (1) to check if the user is a senior citizen (65 or older).
Title: "IF-ELSE: Checking for Senior Citizens"
Explanation: Describe how the program now checks for three categories—minors, adults, and senior citizens—using an additional condition.
2. Handling Invalid Inputs with Logical Operators
Modify example (2) to ensure that if the user enters anything other than "yes" or "no" for parental consent, the program asks them to enter a valid response.
Title: "IF-ELSE with Logical Operators: Validating Input"
Explanation: Explain how the program prevents incorrect inputs and ensures only "yes" or "no" responses are accepted before proceeding.
3. Expanding a List for Multiple Conditions
Modify example (3) to allow additional users by adding "dave" and "eve" to the valid usernames list.
Title: "Using a List to Store Valid Usernames"
Explanation: Describe how lists make it easier to check for valid users rather than using multiple IF statements.
4. Expanding a Dictionary for Decision Making
Modify example (4) to include an additional option: if the user enters "8", the program should return "Holiday". Numbers greater than 8 should return "Invalid day".
Title: "Using a Dictionary to Map Numbers to Days"
Explanation: Explain how dictionaries simplify condition checking by mapping numbers to days and how adding "8: Holiday" expands functionality.
Copy the question into your workbook before answering it.
1. Movie Ticket Price Calculator (IF-ELSE)
🎟 A cinema offers different ticket prices based on age:
Children (under 12): $5
Teenagers (12-17): $8
Adults (18-64): $12
Seniors (65 and over): $6
👉 Make a flowchart that asks for the user’s age and tells them the ticket price.
2. Password Strength Checker (IF-ELSE + Logical Operators)
🔐 A system checks how strong a password is based on these rules:
Weak → Less than 6 characters
Moderate → At least 6 characters but missing uppercase letters or numbers
Strong → At least 8 characters, includes uppercase letters and numbers
👉 Make a flowchart that takes a password as input and classifies it as Weak, Moderate, or Strong.
3. Student Grade Classifier (Using Lists)
📚 A teacher wants a simple program to classify student grades:
Excellent → 90-100
Good → 75-89
Average → 50-74
Fail → Below 50
👉 Make a flowchart that asks for a student’s score and prints their grade category.
4. Vending Machine Drink Selector (Using a Dictionary)
🥤 A vending machine sells four drinks:
1 → Water
2 → Soda
3 → Juice
4 → Tea
If a user enters any other number, the system should display "Invalid choice."
👉 Make a flowchart that asks the user to enter a number and shows which drink they selected.
🛒 A store applies discounts based on the total amount spent:
No discount for purchases under $50
10% discount for purchases between $50-$100
20% discount for purchases over $100
👉 Make a flowchart that asks for the purchase amount and calculates the final price after applying the discount.
✔ Your completed flowcharts for each problem.
✔ A short explanation (2-3 sentences) for each flowchart describing how it works.
✔ (Optional) Python code based on your flowchart if you want an extra challenge!
💡 Thinking logically and planning with flowcharts first will help you write better programs. Get creative and have fun!