Control Flow
Control flow determines the order in which statements are executed in a Python program. By using conditions and loops, a program can make decisions, repeat tasks, and execute different blocks of code based on a particular situation.
What is Control Flow?
Normally, a Python program executes statements from top to bottom. However, real-world programs need to make decisions and repeat certain operations.
Control flow statements allow us to change the normal execution of a program.
For example, an application may need to check whether a student has passed an exam before displaying a certificate. This decision can be implemented using an if statement.
Basic Control Flow
Example: marks >= 40
Based on True / False
Types of Control Flow Statements
Python provides several ways to control the execution of a program.
| Category | Statements | Purpose |
|---|---|---|
| Conditional Statements | if, elif, else | Make decisions |
| For Loop | for | Repeat code for each item |
| While Loop | while | Repeat code while a condition is true |
| Loop Control | break, continue, pass | Control loop execution |
Conditional Statements
Conditional statements allow a program to execute different blocks of code depending on whether a condition is true or false.
Python mainly provides three conditional keywords: if, elif, and else.
if Statement
The if statement executes a block of code only when its condition is true.
Syntax
if condition:
statement
Example
age = 25
if age >= 18:
print("You are eligible to vote.")
Output
You are eligible to vote.
if-else Statement
The else block is executed when the if condition is false.
age = 16
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Output
You are not eligible to vote.
if-elif-else Statement
The elif keyword is used when there are multiple conditions to check.
marks = 75
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
elif marks >= 40:
print("Grade C")
else:
print("Fail")
Output
Grade A
if → elif → else Decision Flow
Check first condition
Check another condition
Execute when no condition is true
Nested if Statement
An if statement placed inside another if statement is called a nested conditional statement.
age = 25
has_id = True
if age >= 18:
if has_id:
print("Entry allowed.")
else:
print("ID required.")
else:
print("Entry not allowed.")
Output:
Entry allowed.
For Loop
A for loop is used to iterate over items in a sequence such as a list, tuple, string, set, dictionary, or range.
Syntax
for variable in sequence:
statement
Example
courses = ["Python", "C#", "Java"]
for course in courses:
print(course)
Output
Python
C#
Java
for Loop with range()
The range() function generates a sequence of numbers that can be used with a for loop.
for number in range(1, 6):
print(number)
Output
1
2
3
4
5
The ending value 6 is not included.
While Loop
A while loop repeatedly executes a block of code as long as its condition remains true.
Syntax
while condition:
statement
Example
number = 1
while number <= 5:
print(number)
number += 1
Output
1
2
3
4
5
for Loop vs while Loop
| for Loop | while Loop |
|---|---|
| Used when iterating over a sequence | Used when repetition depends on a condition |
| Common with lists and range() | Common when the number of iterations is not known in advance |
| Usually simpler for fixed iteration | Requires careful condition management |
break Statement
The break statement immediately terminates the loop.
for number in range(1, 10):
if number == 5:
break
print(number)
Output
1
2
3
4
continue Statement
The continue statement skips the current iteration and moves to the next iteration of the loop.
for number in range(1, 6):
if number == 3:
continue
print(number)
Output
1
2
4
5
pass Statement
The pass statement does nothing. It is used as a placeholder when a statement is syntactically required but the implementation is not ready yet.
for number in range(1, 4):
if number == 2:
pass
print(number)
Output
1
2
3
Nested Loops
A loop inside another loop is called a nested loop.
Nested loops are useful for working with tables, matrices, patterns, and multidimensional data.
for row in range(1, 4):
for column in range(1, 4):
print(row, column)
Example 📩🫠
Suppose CIIT Training Institute wants to determine whether a student is eligible for a certificate based on marks and attendance.
marks = 78
attendance = 82
if marks >= 40 and attendance >= 75:
print("Certificate Eligible")
else:
print("Certificate Not Eligible")
Output
Certificate Eligible
Menu Example 🤓🌍
Control flow can also be used to build a simple menu-based application.
choice = 2
if choice == 1:
print("Python Course")
elif choice == 2:
print("C# Course")
elif choice == 3:
print("Java Course")
else:
print("Invalid Choice")
Output
C# Course
Common Mistakes
- Forgetting the colon : after if, elif, else, for, or while.
- Using incorrect indentation inside a block.
- Creating an infinite while loop by forgetting to update the loop variable.
- Using = when a comparison requires ==.
- Forgetting that range() excludes its ending value.
= vs ==
One common beginner mistake is confusing assignment and comparison operators.
| Operator | Purpose | Example |
|---|---|---|
| = | Assigns a value | age = 25 |
| == | Compares two values | age == 25 |
Indentation in Control Flow
Python uses indentation to define a block of code. Unlike some languages, Python does not use curly braces to define blocks.
age = 25
if age >= 18:
print("Adult")
print("Eligible")
Both print statements belong to the if block because they are indented.
CIIT Learning Point
Control flow is the foundation of program logic. First become comfortable with if, elif, else, then practice for and while loops. Finally, use break, continue, and pass to control loop behavior.
CIIT Practice Task
Create a Python program that accepts a student's marks and displays the grade using if-elif-else.
Use the following grading rules:
- 90 or above → A+
- 75 to 89 → A
- 60 to 74 → B
- 40 to 59 → C
- Below 40 → Fail
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
elif marks >= 40:
print("Grade C")
else:
print("Fail")
Output:
Enter your marks: 85
Grade A
After completing this task, try creating the same program using a different set of conditions.
Summary :
Control flow statements control the execution of Python programs. You learned how to make decisions using if, elif, and else, repeat operations using for and while loops, and control loop execution using break, continue, and pass. You also learned nested conditions, nested loops, indentation, operator usage, and real-world decision-making examples.