Lessons
Courses
Notes in current language not found. Defaulting to English.

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

1. Introduction

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

  • break → stops the loop completely
  • continue → skips the current iteration and moves to the next

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

12

2.4 Explanation

  • Loop starts from 1
  • When i == 3, break stops the loop
  • Remaining iterations are not executed

2.5 Example with while

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

2.6 Key Points

  • Terminates the loop immediately
  • Used when a condition is met
  • Works with both for and while loops

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

1245

3.4 Explanation

  • When i == 3, continue skips that iteration
  • The loop continues with the next value

3.5 Example with while

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

3.6 Key Points

  • Skips only one iteration
  • Does not stop the loop
  • Useful for ignoring specific values

4. Difference Between break and continue

Featurebreakcontinue
FunctionStops loop completelySkips current iteration
Loop ExecutionEnds immediatelyContinues next iteration
Use CaseExit loop earlySkip 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

  • break → exits the loop immediately
  • continue → skips the current iteration
  • Both are used to control loop behavior
  • Useful for writing efficient and flexible loops