Python Loop Control


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.


🔹 Break Statement

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


🔹 Continue Statement

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


🔹 Pass Statement

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.


🔹 Break in While Loop

i = 1

while i <= 5:
    if i == 3:
        break
    print(i)
    i += 1

🔹 Continue in While Loop

i = 0

while i < 5:
    i += 1
    if i == 3:
        continue
    print(i)

🔹 Pass in Loop

numbers = [1, 2, 3, 4]

for n in numbers:
    if n == 3:
        pass
    print(n)

🧪 Try It Yourself

Use break, continue, and pass in both for and while loops to control flow based on conditions.


Practice Questions

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.


Go Back Top