Day 8: Loop Control Statements
Master the use of break
, continue
, and pass
in Python loops.
Task
Practice using loop control statements to manage program flow.
Description
Python provides three key loop control statements:
break
- Exits the loop immediately
- Used when a certain condition is met
continue
- Skips the current iteration
- Continues with the next loop iteration
pass
- Acts as a placeholder
- Does nothing when executed
Examples
Using break
1
2
3
4
5
numbers = [1, 2, 3, 4, 5, 6]
for num in numbers:
if num == 4:
break
print(num)
Output:
1
2
3
1
2
3
Using continue
1
2
3
4
for num in numbers:
if num % 2 == 0:
continue
print(num)
Output:
1
2
3
1
3
5
Using pass
1
2
3
4
for num in numbers:
if num == 3:
pass
print(num)
Output:
1
2
3
4
5
6
1
2
3
4
5
6