Break\Continue#

Overview#

The flow control takes decisions about the statements inside loops, and there are two main controllers: break and continue.

Break#

The break statement terminates a loop’s execution based on a specific condition, and moves your program to the next statement after the loop (which can be another loop continuation)

General Syntax

for item in sequence:
    if condition:
        break
    # Code to be executed

Examples

numbers = [1, 2, 3, 4, 5]
for num in numbers:
    if num == 3:
        break
    print(num)
1
2

Continue#

The continue statement skips the remaining block of code within the loop based on a specific condition, and moves to the next iteration by continuing the loop.

General Syntax

for item in sequence:
    if condition:
        continue
    # Code to be executed

Examples

numbers = [1, 2, 3, 4, 5]
for num in numbers:
    if num == 3:
        continue
    print(num)
1
2
4
5