
Table of Contents(目次)
for Loop Control Statements (break / continue / else)
You’ve probably understood the basics of for loops from previous lectures. But have you ever wanted to "stop the loop midway," "skip just this iteration," or "perform a special action after the loop finishes"?
Python provides three handy control statements for such cases.
Statement | Behavior | Common Use Case |
---|---|---|
break | Completely exits the loop | When you want to exit once a condition is met |
continue | Skips only the current iteration | When you want to skip iterations that don’t meet a condition |
else | Executed if the loop runs to completion (i.e., break was never executed) | For handling “not found” cases in search or validation |
Let’s see how each statement behaves, both when it executes and when it doesn’t.
1. break: Stop the loop midway
When the condition is met
break
forces the loop to exit when you determine that further iterations are unnecessary.
For example, searching for a specific employee ("Nico") in a dict
of employee names and IDs, and stopping the process once found:
employees = {
'Liam': 101,
'Olivia': 102,
'Nico': 103,
'Emma': 104,
'Noah': 105
}
target_name = 'Nico'
for name, emp_id in employees.items():
print(f'Checking {name}...')
if name == target_name:
print(f'{name}\'s employee ID is {emp_id}')
break
print('Loop finished')
Output:
Checking Liam...
Checking Olivia...
Checking Nico...
Nico's employee ID is 103
Loop finished
Once break
executes, the loop exits, so "Emma" and "Noah" are not checked.
When the condition is not met
If the dict
doesn’t contain "Sara," break
never executes, and the loop continues until the end.
employees = {
'Liam': 101,
'Olivia': 102,
'Nico': 103,
'Emma': 104,
'Noah': 105
}
target_name = 'Sara'
for name, emp_id in employees.items():
print(f'Checking {name}...')
if name == target_name:
print(f'{name}\'s employee ID is {emp_id}')
break
print('Loop finished')
Output:
Checking Liam...
Checking Olivia...
Checking Nico...
Checking Emma...
Checking Noah...
Loop finished
2. continue: Skip certain iterations
When the condition is met
continue
is used when you want to skip the current iteration and proceed to the next loop.
For example, if you want to exclude certain employees from processing, you can use continue
. Here, we skip "Nico" and "Emma" while processing the others normally:
employees = ['Liam', 'Olivia', 'Nico', 'Emma', 'Noah']
skip_names = ['Nico', 'Emma']
for name in employees:
print(f'Starting processing for {name}...')
if name in skip_names:
print(f'→ {name} is skipped')
continue
print(f'Updating data for {name}')
print('All processing completed')
Output:
Starting processing for Liam...
Updating data for Liam
Starting processing for Olivia...
Updating data for Olivia
Starting processing for Nico...
→ Nico is skipped
Starting processing for Emma...
→ Emma is skipped
Starting processing for Noah...
Updating data for Noah
All processing completed
When the name is "Nico" or "Emma," continue
executes, skipping print(f'Updating data for {name}')
. The loop continues with the next iteration.
When the condition is not met
If no iteration meets the continue
condition, all iterations execute normally. This helps you understand that "if nothing is skipped, everything runs as usual."
employees = ['Liam', 'Olivia', 'Nico', 'Emma', 'Noah']
skip_names = ['Sara', 'Tom']
for name in employees:
print(f'Starting processing for {name}...')
if name in skip_names:
print(f'→ {name} is skipped')
continue
print(f'Updating data for {name}')
print('All processing completed')
Output:
Starting processing for Liam...
Updating data for Liam
Starting processing for Olivia...
Updating data for Olivia
Starting processing for Nico...
Updating data for Nico
Starting processing for Emma...
Updating data for Emma
Starting processing for Noah...
Updating data for Noah
All processing completed
3. else: Executes after loop completes
else
executes only if the loop wasn’t terminated by break
.
Even if continue
executes, the else
block runs as long as break
hasn’t interrupted the loop.
Note: The following code contains both an else
corresponding to the for
loop and an else
corresponding to the if
statement. In such cases, you can determine which else
belongs to which statement based on the indentation level. Specifically, the else
on line 9 corresponds to the if
, while the else
on line 12 corresponds to the for
loop.
When break is not triggered and else executes
Combining for
with else
, the else
block runs only if the loop reaches the end without hitting break
.
employees = ['Liam', 'Olivia', 'Nico', 'Emma', 'Noah']
target_names = ['Sophia', 'Mason']
for name in employees:
print(f'Checking {name}...')
if name in target_names:
print(f'{name} was found')
break
else:
print('Skipping...')
continue
else:
print('No employees found')
print('All search processing completed')
Output:
Checking Liam...
Skipping...
Checking Olivia...
Skipping...
Checking Nico...
Skipping...
Checking Emma...
Skipping...
Checking Noah...
Skipping...
No employees found
All search processing completed
Since none of the target employees were found, break
never executed and else
ran. continue
does not prevent else
from executing.
When break is triggered and else does not execute
employees = ['Liam', 'Olivia', 'Nico', 'Emma', 'Noah']
target_names = ['Sophia', 'Nico']
for name in employees:
print(f'Checking {name}...')
if name in target_names:
print(f'{name} was found')
break
else:
print('Skipping...')
continue
else:
print('No employees found')
print('All search processing completed')
Output:
Checking Liam...
Skipping...
Checking Olivia...
Skipping...
Checking Nico...
Nico was found
All search processing completed
Since "Nico" was found and break
executed, else
was skipped. This shows that for ... else
behavior depends on whether the loop exited early.
Summary
Statement | Behavior | Execution Condition | Common Use Case |
---|---|---|---|
break | Completely exits the loop | Condition is met | When search result is found |
continue | Skips the current iteration | Only for iterations meeting the condition | When you want to set exclusion criteria |
else | Executes after the loop completes | break was never executed | Handling “not found” in search or validation |
break
= "stop", continue
= "skip", else
= "execute after completion". If the condition isn’t met, the statements do not execute, and the loop continues normally.