Table of Contents
Toggle
Control flow in Python decides how your program runs step by step.
Imagine it like traffic signals 🚦 guiding cars:
✅ Green light → Go forward (execute code)
✅ Yellow light → Check condition (decide)
✅ Red light → Stop or jump to another route.
Python uses control flow to make decisions (if-else),
repeat tasks (loops), and control loop execution (break, continue, pass).
Decision making allows the program to choose different actions based on conditions. Example: Checking student marks 🎓
marks = 80
if marks < 30:
print("Failed")
elif marks >= 75:
print("Passed with distinction")
else:
print("Passed")
Output: Passed with distinction
✅ Tip: Conditions run top to bottom. The first one that’s True will execute.
Loops repeat a block of code. Think of it like saying: “📢 Repeat this 5 times!”
Used when you know how many times you want to repeat.
words = ["apple", "banana", "cherry"]
for x in words:
print(x)
Output: apple, banana, cherry
Used when you want to repeat until a condition is False.
i = 1
while i <= 5:
print(i)
i += 1
Output: 1 2 3 4 5
Jump statements give you more control inside loops.
for num in range(1, 6):
if num == 3:
break
print(num)
Output: 1 2 (loop stops at 3)
for num in range(1, 6):
if num == 3:
continue
print(num)
Output: 1 2 4 5 (skipped 3)
for num in range(1, 6):
if num == 3:
pass
print(num)
Output: 1 2 3 4 5
✅ Useful when writing code structure but not logic yet.
Python’s control flow is like the steering wheel 🛞 of your program.
With if-else, you make decisions.
With loops, you repeat tasks.
With jump statements, you gain fine control inside loops.
Mastering control flow will help you write cleaner, smarter, and more logical code.
