In programming, control flow determines the order in which your code runs. Instead of executing line by line from top to bottom, you can make decisions and repeat tasks.
In Python, the three most common control flow tools are:
if/elsestatementsforloopswhileloops
Let’s go through each with examples.
📝 If You Want to Practice the Code
If you plan to practice the Python code mentioned within this blog post, you’ll need a working Python environment. You can execute the commands in one of the following ways (choose only one option):
- From a Python prompt in your terminal or command line – see Getting Started with Python and Virtual Environment in Python and How to Set One Up
- Using Miniconda and Jupyter Notebook – see How to Install and Set Up Miniconda and Install Jupytor Notebook Windows, How to Install and Set Up Miniconda on MacOS, How to Install and Set Up Miniconda on Linux (Ubuntu), and Getting Started with Jupyter Notebooks.
- On Google Colab (browser-based, no installation required) – see Getting Started with Google Colab.
- Through VS Code – see Setting Up VS Code for Python Development.
👉 Choose whichever environment you’re most comfortable with. If you haven’t set up Python yet, start with one of the installation posts before trying the examples here.
🔹 if/else Statements
An if statement checks a condition. If the condition is True, Python runs the indented code. If not, it skips it.
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
👉 Output: You are an adult.
You can also chain multiple conditions using elif (else if):
marks = 72
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")
🔹 for Loops
A for loop lets you repeat something for each item in a sequence (like a list or string), or a range of numbers.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
👉 Output:
apple
banana
cherry
You can also loop over numbers using range():
for i in range(5):
print(i)
👉 Output: 0 1 2 3 4
🔹 while Loops
A while loop keeps running as long as a condition is True.
count = 1
while count <= 5:
print("Count is:", count)
count += 1
👉 Output:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
⚠️ Be careful: if the condition never becomes False, the loop will run forever.
📝 Mini Task
Try this simple program:
# Ask the user for a number and check if it is even or odd
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even number")
else:
print("Odd number")
# Print numbers from 1 to that number using a for loop
for i in range(1, number + 1):
print(i)
# Keep asking until the user enters 0
while number != 0:
number = int(input("Enter another number (0 to stop): "))
✅ Summary
- if/else → decision making
- for loop → repeat for items or a fixed range
- while loop → repeat while a condition is true
Control flow makes your programs flexible and interactive, letting you handle different situations and repeat tasks efficiently.