When working with loops in Python, sometimes we need extra control over how the loop executes. By default, a loop runs until its condition becomes false, but what if:
- You want to stop the loop early?
- Or skip one iteration without breaking the loop?
That’s where loop control statements come in.
In Python, the main loop control statements are:
break
– exit the loop completely.continue
– skip the current iteration and move to the next.- (
pass
exists too, but it simply does nothing – it’s a placeholder.)
Let’s understand them with clear examples.
The break
Statement
he break
statement is used to terminate the loop immediately, even if the loop condition is still true.
Example 1: Stopping at a certain value
alphabets = ["a", "b", "c", "d", "e"] for letter in alphabets: if letter == "c": print("Found:", letter) break
Output:
Found: c
👉 As soon as Python finds "c"
, the loop ends. "d"
and "e"
are not checked.
Real-Life Use Cases of break
:
- Search Systems: Instagram searching for a username – stop once found.
- Login Attempts: Stop asking for password after correct entry.
- Monitoring Systems: Stop checking once threshold is reached (e.g., temperature too high).
The continue
Statement
The continue
statement skips the current iteration and goes directly to the next one.
Example 2: Skipping a specific value
alphabets = ["a", "b", "c", "d", "e"] for letter in alphabets: if letter == "c": continue print("Letter:", letter)
Output:
Letter: a Letter: b Letter: d Letter: e
👉 Here, "c"
is skipped, but the loop continues with "d"
and "e"
.
Real-Life Use Cases of continue
:
- Skipping invalid data: If a post is flagged as spam, Instagram skips it but continues loading other posts.
- Skipping blocked users: While fetching comments, skip the blocked ones but show others.
- Data filtering: Skip unwanted entries while processing a list.
🔹 Difference Between break
and continue
Feature | break | continue |
---|---|---|
Effect | Stops the loop entirely | Skips one iteration, loop continues |
Control jumps | Outside the loop | Back to loop condition for next cycle |
Use case | When no further checks are needed | When some checks should be ignored |
Practice Questions (with Code)
Here are 5 important practice problems for each concept.
🟢 Break – Practice Problems
- Stop loop when number is found
numbers = [1, 2, 3, 4, 5] for n in numbers: if n == 3: print("Found:", n) break
- Search for a name in a list
names = ["Amit", "Ravi", "Sneha", "John"] for name in names: if name == "Sneha": print("Found Sneha!") break
- Stop password attempts after correct entry
password = "1234" for attempt in range(3): entered = input("Enter password: ") if entered == password: print("Access Granted") break
- Stop at first negative number
nums = [10, 20, -5, 30, 40] for n in nums: if n < 0: print("Negative number found:", n) break
- Find prime number and stop
nums = [4, 6, 8, 11, 15] for n in nums: if n % 2 != 0 and n % 3 != 0: print("First prime-like number:", n) break
🟡 Continue – Practice Problems
- Skip printing even numbers
for i in range(1, 6): if i % 2 == 0: continue print("Odd:", i)
- Skip absent students
students = ["Amit", "Absent", "Sneha", "Ravi"] for s in students: if s == "Absent": continue print("Present:", s)
- Skip zero in division
nums = [10, 0, 5, 2] for n in nums: if n == 0: continue print("Division result:", 100 / n)
- Skip certain letters
for ch in "python": if ch == "o": continue print(ch)
- Skip blocked users
users = ["Uday", "Blocked", "Codes", "Dev"] for u in users: if u == "Blocked": continue print("Showing user:", u)
Best Youtube Videos
Break, Continue, Pass – Telusko
Break, Continue – Neso Academy
Break Continue Pass in Python – Jenny’s Lectures