Control statements ( For loop While break continue ) in Python allow us to control the flow of loops in a program. They help decide how many times a loop will run, when to stop, or when to skip certain iterations. In our previous tutorial we have covered the data types and statements In this Tutorial, we will cover:
For loop
While loop
Break statement
Continue statement
Pass statement
Along with syntax, examples, tricks, and outputs, you’ll also find simple explanations that make them easy to remember.
🔹For Loop in Python
A for loop is used to iterate over a sequence (like a list, tuple, string, or numbers using range()). It runs the block of code once for every element in the sequence.
✅ Syntax:
for variable in range(start, stop, step):
# code block
Flowchart

Explanation:
the range(start, stop, step) function is used to generate a sequence of numbers in a loop.
start → The beginning number of the sequence (default = 0).
stop → The end number (exclusive), meaning the loop stops before this value.
step → The increment or decrement between numbers (default = 1).
Examples
✅ Example 1: Print numbers from 1 to 5
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
✅ Example 2: Loop through a list
fruits = [“apple”, “banana”, “cherry”]
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
💡 Trick to remember: For loop = repeat for each item in a collection
🔹While Loop in Python
A while loop runs as long as the condition is True. Once the condition becomes False, the loop stops.
Think of it like:
👉 Keep doing this task while the rule is true.
✅ Syntax:
while condition:
# code block
Flowchart:

Explanation:
condition → A logical expression that is checked before each loop iteration.
If the condition is True, the loop runs.
If the condition is False, the loop stops.
Examples:
✅ Example 1: Print numbers from 1 to 5
count = 1
while count <= 5:
print(count)
count += 1
Output:
1
2
3
4
5
✅ Example 2: Print even numbers up to 10
num = 2
while num <= 10:
print(num)
num += 2
Output:
2
4
6
8
10
💡 Trick to remember:
Use for loop when you know the number of iterations.
Use while loop when the loop depends on a condition.
🔹 Break Statement in Python
The break statement is used to exit a loop immediately, even if the loop condition is still true.
✅ Syntax:
for variable in sequence:
if condition:
break
Explanation:
for variable in sequence → Loops through each item in a sequence (like list, tuple, or string).
if condition: → Checks a specific condition for each item.
break → Immediately exits the loop when the condition is True.
Examples:
✅ Example 1: Stop when i == 5
for i in range(1, 10):
if i == 5:
break
print(i)
Output:
1
2
3
4
Example 2: Real-World Example Search for a name
names = [“John”, “Emma”, “Alex”, “David”]
for name in names:
if name == “Alex”:
print(“Alex found! Stopping search.”)
break
Output:
Alex found! Stopping search.
💡 Trick to remember: Break = stop the loop.
🔹 Continue Statement in Python
The continue statement is used to skip the current iteration and move to the next one.
✅ Syntax:
for variable in sequence:
if condition:
continue
Explanation:
for variable in sequence → Iterates through each item in a sequence (list, tuple, string, etc.).
if condition: → Checks a specific condition for the current item.
continue → Skips the rest of the code in the current loop iteration and moves to the next item.
Examples:
✅ Example: Skip number 3
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1
2
4
5
Example 2: Skip even numbers
for i in range(1, 10):
if i % 2 == 0:
continue # Skip even numbers
print(i)
Output:
1
3
5
7
9
💡 Trick to remember: Continue = skip and move ahead.
Difference Between break
and continue
:
Feature | break 🚫 | continue 🔁 |
---|---|---|
Stops the loop completely | ✅ Yes | ❌ No |
Skips current iteration | ❌ No | ✅ Yes |
Moves to next iteration automatically | ❌ No | ✅ Yes |
🔹 Pass Statement in Python
The pass statement does nothing. It is often used as a placeholder when you don’t want to add code yet.
“Control statements in Python help manage the flow of execution in a program.”
✅ Syntax:
for variable in sequence:
pass
Explanation:
for variable in sequence → Iterates through each item in the sequence.
pass → Does nothing for each iteration.
It is useful for writing empty loops, functions, or classes while planning or debugging your code.
Examples:
✅ Example1: Empty loop
for i in range(1, 4):
pass
print(“Loop executed but nothing happened inside.”)
Output:
Loop executed but nothing happened inside.
Example 2: Placeholder for future code
for num in range(1, 6):
if num % 2 == 0:
pass # To be implemented later
else:
print(f”Odd number: {num}”)
Output:
Odd number: 1
Odd number: 3
Odd number: 5
💡 Trick to remember: Pass = do nothing.
📝 Summary of Control Statements in Python
For Loop → Best when you know the number of iterations.
While Loop → Best when iterations depend on a condition.
Break → Exit the loop immediately.
Continue → Skip the current iteration and continue.
Pass → Do nothing (placeholder).
remember “Control statements in Python help manage the flow of execution in a program.(For loop
While loop
Break statement
Continue)”
_________________________________________________
lets give the MCQ test to know how much you understand it!!!
Q&A part -1
Q1. What is the difference between for and while loop in Python?
Answer:
For loop:
Used when the number of iterations is known or when iterating over a sequence like a list, tuple, string, or range().
Example:
for i in range(5):
print(i) # Output: 0 1 2 3 4
While loop:
Used when the number of iterations is unknown and depends on a condition being True.
Example:
count = 0
while count < 5:
print(count)
count += 1 # Output: 0 1 2 3 4
Q2. Explain the difference between break, continue, and pass with examples.
Answer:
break → Immediately exits the loop completely.
for i in range(5):
if i == 3:
break
print(i)
Output: 0 1 2
continue → Skips the current iteration and moves to the next one.
for i in range(5):
if i == 3:
continue
print(i)
Output: 0 1 2 4
pass → Does nothing. It is used as a placeholder.
for i in range(5):
if i == 3:
pass
print(i)
Output: 0 1 2 3 4
Q&A part -2
Q3. Can we use an else block with loops in Python? Give an example.
Answer:
Yes, the else block executes when the loop finishes normally (without a break statement).
for i in range(5):
print(i)
else:
print(“Loop finished successfully”)
Output:
0
1
2
3
4
Loop finished successfully
Q4. What happens if we put a break inside a nested loop?
Answer:
The break statement only exits the inner loop, not the outer loop.
for i in range(3):
for j in range(3):
if j == 1:
break
print(i, j)
Output:
0 0
1 0
2 0
Q5. How can you simulate a do-while loop in Python since it doesn’t exist?
Answer:
Python does not have a built-in do-while loop, but we can simulate it using a while True loop with a break condition.
while True:
num = int(input(“Enter a number: “))
print(“You entered:”, num)
if num < 0:
break
Explanation:
The code runs at least once, like a do-while loop.
The loop continues until the break condition is met.
_________________________________________________
In this Tutorial, we have covered:
For loop
While loop
Break statement
Continue statement
Pass statement
“Thank you for reading this tutorial! In the next tutorial, we will explore an exciting new topic. Until then, keep practicing and stay consistent with your learning!”
Leave a Reply