Chapter 5: Conditional Statements
Learn how Python programs make decisions using if, if-else, if-elif-else, nested conditions, comparison operators, and logical operators.
5.1 Chapter Overview
Conditional statements allow a Python program to make decisions. In real life, decisions are made every day: if marks are above the passing score, the student passes; if payment is completed, registration is confirmed; if attendance is enough, the student is eligible for a certificate.
In programming, conditional statements control which block of code should run based on whether a condition is True or False.
5.2 Chapter Objectives
- Understand the purpose of conditional statements.
- Use if statements to execute code based on a condition.
- Use if-else statements for two-way decisions.
- Use if-elif-else statements for multiple decisions.
- Apply comparison operators in conditions.
- Combine multiple conditions using logical operators.
- Develop interactive Python applications using decision logic.
Learning Outcomes
- Write Python programs that make decisions.
- Use indentation correctly in conditional statements.
- Create pass/fail, grade, eligibility and fee decision programs.
- Apply nested conditional statements in practical examples.
5.3 What is Decision Logic?
Decision logic is the process of checking a condition and choosing what action to perform. A condition is an expression that gives a result of either True or False.
Example Condition
marks >= 50
If marks are 50 or above, the condition is True. If marks are below 50, the condition is False.
5.4 Comparison Operators Used in Conditions
| Operator | Meaning | Example |
|---|---|---|
| == | Equal to | marks == 100 |
| != | Not equal to | status != "Paid" |
| > | Greater than | age > 18 |
| < | Less than | marks < 50 |
| >= | Greater than or equal to | attendance >= 80 |
| <= | Less than or equal to | fee_balance <= 0 |
5.5 The if Statement
The if statement is used when a block of code should run only if a condition is True.
Syntax
if condition:
statement
Example
marks = 80
if marks >= 50:
print("You have passed.")
You have passed.
5.6 The if-else Statement
The if-else statement is used when there are two possible actions. If the condition is True, the if block runs. Otherwise, the else block runs.
Syntax
if condition:
statement_if_true
else:
statement_if_false
Example: Pass or Fail
marks = 45
if marks >= 50:
print("Pass")
else:
print("Fail")
Fail
5.7 Taking User Input with if-else
Conditional statements become more useful when combined with user input.
marks = float(input("Enter your marks: "))
if marks >= 50:
print("Result: Pass")
else:
print("Result: Fail")
Enter your marks: 75
Result: Pass
5.8 The if-elif-else Statement
The if-elif-else statement is used when a program has more than two possible decisions.
Syntax
if condition1:
statement1
elif condition2:
statement2
elif condition3:
statement3
else:
final_statement
Example: Grade System
marks = float(input("Enter marks: "))
if marks >= 80:
print("Grade A")
elif marks >= 65:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")
Enter marks: 82
Grade A
5.9 Order of Conditions
In an if-elif-else structure, Python checks conditions from top to bottom. Once a True condition is found, Python executes that block and skips the rest.
5.10 Nested Conditional Statements
A nested conditional statement means placing one if statement inside another if statement. This is used when a second decision depends on the first decision.
Example: Certificate Eligibility
marks = float(input("Enter marks: "))
attendance = float(input("Enter attendance percentage: "))
if marks >= 50:
if attendance >= 80:
print("Eligible for certificate")
else:
print("Not eligible due to low attendance")
else:
print("Not eligible due to failed marks")
Enter marks: 70
Enter attendance percentage: 90
Eligible for certificate
5.11 Logical Operators in Conditional Statements
Logical operators are used to combine multiple conditions.
| Operator | Meaning | Example |
|---|---|---|
| and | Both conditions must be True. | marks >= 50 and attendance >= 80 |
| or | At least one condition must be True. | has_ic or has_passport |
| not | Reverses the condition result. | not is_absent |
Example using and
marks = float(input("Enter marks: "))
attendance = float(input("Enter attendance: "))
if marks >= 50 and attendance >= 80:
print("Eligible for certificate")
else:
print("Not eligible for certificate")
5.12 Real-World Example: Login Decision
Conditional statements are commonly used in login systems to check username and password.
username = input("Enter username: ")
password = input("Enter password: ")
if username == "admin" and password == "pdtc123":
print("Login successful")
else:
print("Invalid username or password")
5.13 Real-World Example: Course Fee Status
This example checks whether a student has completed fee payment.
course_fee = float(input("Enter course fee: "))
amount_paid = float(input("Enter amount paid: "))
balance = course_fee - amount_paid
if balance <= 0:
print("Payment completed")
else:
print("Outstanding Balance: RM", balance)
Enter course fee: 1500
Enter amount paid: 1000
Outstanding Balance: RM 500.0
5.14 Common Beginner Mistakes
| Mistake | Wrong Example | Correct Example |
|---|---|---|
| Missing colon | if marks >= 50 | if marks >= 50: |
| Wrong indentation | print() not aligned properly | Indent code inside if block. |
| Using = instead of == for comparison | if name = "Amin": | if name == "Amin": |
| Wrong condition order | Checking marks >= 50 before marks >= 80 in grading | Check highest grade first. |
5.15 Hands-On Practice
Activity 1: Pass or Fail Checker
Create a Python program that asks the user to enter marks and displays Pass if marks are 50 or above, otherwise Fail.
Activity 2: Grade Calculator
Create a program that accepts marks and displays Grade A, B, C or Fail.
Activity 3: Age Eligibility Checker
Create a program that asks for age and checks whether the user is eligible to register for a professional course.
Mini Project: Certificate Eligibility System
Create a Python program that asks for student name, marks and attendance percentage. The program should display whether the student is eligible for certification.
5.16 Final Assessment Quiz
Answer the following questions. Correct Answer = +1 Mark Wrong Answer = -0.5 Mark
1. Conditional statements allow Python programs to make decisions.
2. The else block runs when the if condition is True.
3. The elif statement is used when there are multiple conditions to check.
4. Python uses indentation to define the code block inside an if statement.
5. The logical operator and requires both conditions to be True.
Your Score: 0
Final Practical Assessment
Develop an interactive Python program named certificate_eligibility.py. The program must ask for student name, marks and attendance percentage. It must display whether the student is eligible for certificate, not eligible due to marks, or not eligible due to attendance.
5.17 Chapter Summary
In this chapter, learners studied conditional statements in Python. They learned how to use if, if-else, if-elif-else, nested conditions, comparison operators and logical operators to create decision-making programs.