Table of Contents
- Page 1: Introduction
- Page 2: Control Structures
- Page 3: if Statement
- Page 4: if...else
- Page 5: if...elif...else
- Page 6: Multiple elif
Understanding Program Flow
In our daily lives, we encounter situations where we must follow fixed sequences of steps to complete tasks, while in others we have choices. Understanding these patterns is crucial for programming.
Airport Boarding Process
- Show ticket and identity proof at the main entrance of the Airport.
- Scanning of luggage at the security checkpoint.
- Take your boarding pass from the check-in counter.
- Pass security check including personal screening.
- Finally, board the plane at the designated gate.
This fixed sequence represents sequential flow in programming.
Just like passengers have multiple ways to get boarding passes (counter OR kiosk), programs often need to make decisions:
- IF the passenger chooses counter, THEN follow counter procedure
- ELSE IF passenger chooses kiosk, THEN follow kiosk procedure
- ELSE print error message
Try It Yourself: Python Control Flow
Practice the concepts you've learned by modifying the code below. This interactive editor simulates Python execution in your browser.
Understanding Control Structures
Control structures are the building blocks of programming that determine the flow of execution. They allow programs to make decisions, repeat actions, and handle different scenarios.
Types of Control Structures
- Sequential Structures: Code executes line by line
- Selection Structures: if, if...else, if...elif...else
- Iteration Structures: for loops, while loops
- Jump Structures: break, continue, return
The if Statement
The if statement is the most basic control structure. It allows your program to execute code only when a certain condition is true.
if condition:
# Code to execute if condition is True
statement1
statement2
# ... more statements
The indented block under if only executes when the condition evaluates to True.
Comparison Operators
Comparison operators are used in conditions to compare values. They return either True or False.
Python Comparison Operators
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
Practical Example: Voting Eligibility
Let's examine a complete example to understand the control flow of the if statement. This program checks if a person is eligible to vote based on their age.
Complete Python Program
if 20 >= 18:→ True ✓
if 18 >= 18:→ True ✓
if 17 >= 18:→ False ✗
Interactive Test
Try the program with different ages to see how the if statement works:
Test Voting Eligibility
Enter an age to check voting eligibility:
Try ages like: 16, 18, 20, 25
Working with Alternative Paths
The if...else statement allows writing two alternative paths. The control condition determines which path gets executed.
if test_expression:
# Body of if (executes when True)
statement1
statement2
else:
# Body of else (executes when False)
statement3
statement4
if...else statement evaluates the test expression and executes the body of if only when the condition is True. If the condition is False, the body of else is executed.
Example 1.2: Modified Voting Eligibility
Let's modify Example 1.1 to illustrate the use of if...else structure:
if vs if...else Comparison
if Statement Only
if age >= 18:
print("Eligible")
When False: Nothing happens
if...else Statement
if age >= 18:
print("Eligible")
else:
print("Not Eligible")
When False: else block executes
Practice if...else Statements
Write and test your own if...else code:
Handling Multiple Conditions
When you have multiple independent conditions to check, you can use elif statements (short for "else if") between the if and else control structures.
if condition1:
# Executes when condition1 is True
statement1
elif condition2:
# Executes when condition1 is False and condition2 is True
statement2
elif condition3:
# Executes when both condition1 and condition2 are False
# and condition3 is True
statement3
else:
# Executes when all conditions are False
statement4
- Only one block executes (the first True condition)
- Conditions are checked from top to bottom
- Once a True condition is found, the rest are skipped
- The
elseblock is optional
Interactive Grade Calculator
A practical example of if...elif...else with a grade calculator:
Calculate Your Grade
Enter your score (0-100):
Try scores like: 95, 85, 75, 65, 55
Practice if...elif...else
Advanced if...elif...else Structure
You can have multiple elif blocks to handle many independent conditions. Among the several if...elif...else blocks, only one block is executed according to the condition.
Key Concepts
Only One Block Executes
First True condition wins, rest are skipped
Order Matters
Conditions checked from top to bottom
Multiple elif Blocks
You can have as many elif as needed
Single else Block
Only one else allowed per if statement
Example 1.3: Age Category Classifier
Let's create a comprehensive age classifier with multiple elif blocks:
if age < 13:→ True ✓
elif age < 20:→ True ✓
elif age < 60:→ True ✓
