loading...

When to use continue, pass and break in python.

sharmapacific profile image Prashant Sharma ・1 min read

continue

continue forces the loop to start at the next iteration. In short, continue will jump back to the top of the loop

a = [0, 1, 2]
for element in a:
    if not element:
        continue
    print(element)

output-

1
2

UseCase: The continue statement can be used to skip the current iteration only. Loop does not terminate but continues on with the next iteration.

pass

while pass means there is no code to execute here, it passes execution to the next statement. So pass will continue processing.

a = [0, 1, 2]
for element in a:
     if not element:
         pass
     print(element)

output-

0
1
2

UseCase: You can use pass statement when you have created a method but have not implemented, yet.

break

The break statement provides you with the opportunity to exit out of a loop when a given condition is triggered.

a = [0, 1, 2, 3, 4, 5]
for element in a:
    if element == 3:
        break
    print(element)

output-

0
1
2

UseCase: The break statement can use in a program to break out of a loop for the matching condition.

Posted on by:

Discussion

pic
Editor guide
 

Even more common use case for the pass statement is empty classes that don't override anything from their parents.

It's not unusual to see code like this somewhere in a package to define a local exception hierarchy:

class FooException(Exception):
    pass


class FailedToBar(FooException):
    pass


class FailedToBaz(FooException):
    pass
 

Yes, as no code needs to be executed.