Break and Continue

Loop control statements

Interview Relevant: Flow control

Break and Continue

Control loop execution flow.

Code Examples

Break and continue statements.

python
1# break - exit loop entirely
2for i in range(10):
3    if i == 5:
4        break
5    print(i)  # 0, 1, 2, 3, 4
6
7# continue - skip to next iteration
8for i in range(5):
9    if i == 2:
10        continue
11    print(i)  # 0, 1, 3, 4
12
13# break in nested loops
14for i in range(3):
15    for j in range(3):
16        if j == 1:
17            break  # Only breaks inner loop
18        print(i, j)
19
20# else with loops (executed if no break)
21for i in range(5):
22    if i == 10:
23        break
24else:
25    print("No break occurred")

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In