-
Hajipur, Bihar, 844101
Python provides three loop control statements to change the flow of loops:
break
– Exit the loop prematurely
continue
– Skip the current iteration
pass
– Do nothing, acts as a placeholder
These can be used inside for
or while
loops.
Used to stop the loop immediately, even if the condition is still true.
for i in range(10):
if i == 5:
break
print(i)
Output:0 1 2 3 4
Used to skip the current iteration and move to the next.
for i in range(5):
if i == 2:
continue
print(i)
Output:0 1 3 4
Used when a statement is required syntactically, but you don’t want to execute any code.
for i in range(3):
pass
pass
is often used as a placeholder in loops, classes, functions, etc.
i = 1
while i <= 5:
if i == 3:
break
print(i)
i += 1
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
numbers = [1, 2, 3, 4]
for n in numbers:
if n == 3:
pass
print(n)
Use break
, continue
, and pass
in both for
and while
loops to control flow based on conditions.
Q1. Write a Python program to print numbers from 1 to 10, but stop the loop when the number is 7 using break
.
Q2. Write a Python program to print all numbers from 1 to 10, but skip the number 5 using continue
.
Q3. Write a Python program using a while
loop and break the loop when the number equals 4.
Q4. Write a Python program to use continue
to skip even numbers from 1 to 10 and print only odd numbers.
Q5. Write a Python program to create a loop that prints numbers from 1 to 5 and use pass
inside the loop.
Q6. Write a Python program to print only the vowels from the string "PythonLoopControl"
using a loop.
Q7. Write a Python program to loop through numbers from 1 to 20 and break at the first number divisible by 9.
Q8. Write a Python program to loop through numbers from 1 to 10 and skip numbers divisible by 3 using continue
.
Q9. Write a Python program to use pass
to handle a missing condition in a loop (e.g., as a placeholder for future logic).
Q10. Write a Python program to build a login system that keeps asking for a password and breaks the loop when the correct password is entered.
Q1: What does break do in a loop?
Q2: What does continue do?
Q3: What is the purpose of pass?
Q4: Which statement does nothing when executed?
Q5: What happens after continue is executed in a loop?
Q6: Where can you use break?
Q7: What is printed by this code: for i in range(5): if i==2: break; print(i)?
Q8: What is printed if continue is used when i == 2 in a loop from 0–4?
Q9: Can pass be used inside a loop without error?
Q10: What does this code do: for i in range(5): pass?