Skip to content

Break or Continue Outside Loop

Sourcery refactoring id: break-or-continue-outside-loop

Description

Remove break or continue statement found outside for or while loop.

Before

def handle_invalid_number(numbers):
    for number in numbers:
        if is_valid(number):
            continue
    break
    handle(number)

After

def handle_invalid_number(numbers):
    for number in numbers:
        if is_valid(number):
            continue
    handle(number)

Alternative

If you intend to break or continue the execution of a loop, make sure that the statement is correctly placed and indented in your code:

def handle_invalid_number(numbers):
    for number in numbers:
        if is_valid(number):
            continue
        break
    handle(number)

Explanation

The break and continue are used to control the behaviour of for and while loops. Using them outside those loops is a SyntaxError.

This error may be very easy to pass through since sometimes it is caused by a wrong indentation.