Notes
Categories

`break` and `continue` in Python [ English ]

< Prev Next >

1. Introduction

In Python loops (for and while), break and continue are used to control the flow of execution.

2. break Statement

2.1 Definition

The break statement is used to exit the loop immediately, even if the loop condition is still True.

2.2 Syntax

for i in range(...):
    if condition:
        break

2.3 Example

for i in range(1, 6):
    if i == 3:
        break
    print(i)

Output

1
2

2.4 Explanation

2.5 Example with while

i = 1

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

2.6 Key Points

3. continue Statement

3.1 Definition

The continue statement is used to skip the current iteration and move to the next iteration of the loop.


3.2 Syntax

for i in range(...):
    if condition:
        continue

3.3 Example

for i in range(1, 6):
    if i == 3:
        continue
    print(i)

Output

1
2
4
5

3.4 Explanation

3.5 Example with while

i = 0

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

3.6 Key Points

4. Difference Between break and continue

Feature break continue
Function Stops loop completely Skips current iteration
Loop Execution Ends immediately Continues next iteration
Use Case Exit loop early Skip unwanted values

5. Practical Example

for i in range(1, 10):
    if i == 5:
        break
    print(i)
for i in range(1, 10):
    if i == 5:
        continue
    print(i)

6. Summary

< Prev Next >