Control Statements in Python – For, While, Break, Continue, and Pass

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

for loop , for loop statement, flowchart, python For loop While break continue and pass statements

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:

python For loop While break continue and pass statements.
while loop, while statements, while loop 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:

Featurebreak 🚫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)”

for more…

_________________________________________________

lets give the MCQ test to know how much you understand it!!!

Welcome to your MCQs to Test Your Knowledge !!!

What is the correct syntax of a for loop in Python?

Which loop is best when the number of iterations is unknown?

What does the break statement do in Python?

What happens if we use continue inside a loop?

What does the pass statement do?

What will be the output of this code? for i in range(1, 6): if i == 3: break print(i)

What will be the output of this code? for i in range(1, 6): if i == 3: continue print(i)

Which of the following is NOT a control statement in Python?

A while loop executes until:

Which of the following is true about Python loops?

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!”

Comments

One response to “Control Statements in Python – For, While, Break, Continue, and Pass”

  1. Dhondiba Savant Avatar
    Dhondiba Savant

    Sahi hai

Leave a Reply

Your email address will not be published. Required fields are marked *