Unit 2: Computational Thinking & Programming
Chapter 3: Flow Control — Conditional Statements
1. Flow of Control Overview
In computer programming, Flow of Control refers to the order in which statements are executed at runtime.
1. Sequential Execution 2. Selective (Decision) Execution
[Statement 1] [Condition?]
| / \
v True False
[Statement 2] / \
| [Branch A] [Branch B]
v \ /
[Statement 3] v v
[Next Statement]
Python Block Structure and Indentation
Unlike C++ or Java which use curly braces {} to delimit code blocks, Python uses whitespace indentation (standard: 4 spaces). All statements within an indented block (called a suite) must share the exact same indentation level.
2. Forms of Conditional Statements
A. The Simple if Statement
Executes a block of code only if the specified condition evaluates to True.
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to apply for a voter ID card.")
print("Program completed.")
B. The if-else Statement
Provides two mutually exclusive execution branches:
num = int(input("Enter an integer: "))
if num % 2 == 0:
print(f"{num} is Even.")
else:
print(f"{num} is Odd.")
C. The if-elif-else Ladder
Used when there are multiple mutually exclusive conditions to test sequentially:
marks = float(input("Enter exam percentage: "))
if marks >= 90:
grade = 'A+'
elif marks >= 80:
grade = 'A'
elif marks >= 70:
grade = 'B'
elif marks >= 60:
grade = 'C'
elif marks >= 33:
grade = 'D'
else:
grade = 'E (Needs Improvement)'
print("Assigned Grade:", grade)
D. Nested Conditional Statements
An if or if-else block placed entirely inside another if or else block:
# Finding the largest of three numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b:
if a >= c:
largest = a
else:
largest = c
else:
if b >= c:
largest = b
else:
largest = c
print(f"The largest number among {a}, {b}, {c} is: {largest}")
3. Conditional Expressions (Ternary Operator)
Python supports concise one-line decision statements:
result = value_if_true if condition else value_if_false
x = 25
status = "Adult" if x >= 18 else "Minor"
print(status) # "Adult"
4. Classic Board Exam Programming Patterns
Pattern 1: Checking for a Leap Year
A year is a leap year if: 1. It is divisible by $4$, and 2. It is not divisible by $100$, unless it is also divisible by $400$.
year = int(input("Enter a 4-digit year: "))
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print(f"{year} is a Leap Year.")
else:
print(f"{year} is NOT a Leap Year.")
Pattern 2: Electricity Tariff Calculation (Slab Billing)
Compute the electricity bill according to the following consumption slabs: - First $100$ units: ₹$3.00$ per unit - Next $100$ units ($101-200$): ₹$4.50$ per unit - Above $200$ units: ₹$6.00$ per unit
units = float(input("Enter total units consumed: "))
if units <= 100:
bill = units * 3.00
elif units <= 200:
bill = (100 * 3.00) + ((units - 100) * 4.50)
else:
bill = (100 * 3.00) + (100 * 4.50) + ((units - 200) * 6.00)
print(f"Total Electricity Bill: Rs. {bill:.2f}")
5. Board Exam Practice Questions & Solutions
Q1. Find the output of the following code snippet:
val = 45
if val > 50:
print("Tier 1")
elif val > 30:
print("Tier 2")
elif val > 40:
print("Tier 3")
else:
print("Tier 4")
Answer:
Tier 2
Explanation: Conditions in an if-elif-else chain are evaluated sequentially from top to bottom. As soon as the first condition that evaluates to True is found (val > 30), its block executes, and the rest of the chain is skipped—even though val > 40 is also mathematically true.
Q2. Rewrite the following nested if statement into a single if statement using logical operators:
if marks >= 33:
if attendance >= 75:
print("Eligible for Exam Promotion")
Answer:
if marks >= 33 and attendance >= 75:
print("Eligible for Exam Promotion")
Q3. What is an IndentationError in Python? Give an example.
Answer:
An IndentationError occurs when the indentation of code blocks within a suite does not conform to the expected indentation rules of Python (e.g., forgetting to indent under an if header, or mixing mismatched spaces).
Example:
if 10 > 5:
print("Greater") # IndentationError: expected an indented block after 'if' statement