B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python Break and Continue with Examples | LiveCodeProgramming
Python Installation First Python Program Control Flow if...else nested if else match-case logical-operators for loops while loops break / continue Functions Defining Functions Function Arguments Function return value Lambda Function Recursion Exception Handling Exception Handling Try Except Else and Finally Raise Exception Custom Exceptions File Handling File in python Read File Write File Append File Delete File Exception Handling

Python Break and Continue

In Python, break is used to exit a loop immediately, and continue is used to skip the current iteration and continue with the next one.

Python में break का उपयोग loop को तुरंत बंद करने के लिए होता है और continue का उपयोग current iteration को छोड़ने और अगले पर जाने के लिए किया जाता है।

Break Statement Examples
  1. Stop loop when number is 3
    for i in range(1, 6):
        if i == 3:
            break
        print(i)
    Output:
    1
    2
  2. Break inside while loop
    i = 1
    while i <= 5:
        if i == 4:
            break
        print(i)
        i += 1
    Output:
    1
    2
    3
Continue Statement Examples
  1. Skip number 3
    for i in range(1, 6):
        if i == 3:
            continue
        print(i)
    Output:
    1
    2
    4
    5
  2. Continue in while loop
    i = 0
    while i < 5:
        i += 1
        if i == 3:
            continue
        print(i)
    Output:
    1
    2
    4
    5
Break and Continue Together
for i in range(1, 10):
    if i == 5:
        continue
    if i == 8:
        break
    print(i)
Output:
1
2
3
4
6
7