Break, Continue, and Pass Statements in Python are also known as jump statements. These are quite often used with for and while loop.

Sometimes because of some conditions or external factors, we want to skip a part of a loop or exit the loop. We can do this using break, continue and pass statements.

Break Statement

Break statement in python is used to exit from the loop when a condition is satisfied.

We use it whenever we want to terminate the execution or iteration of ‘for’ or ‘while’ loop.

Let’s see an example in python which uses a break statement.

Recommended: Python range()

output

In the above program, we first initialized x=0 then we constructed a ‘for statement‘ to output all numbers from 0 to 9.

Within the for loop, there is also a break statement with condition x==4. This mean the loop will terminate when x value gets incremented to 4.

In the output, we got what we expected that the execution of ‘for loop’ stops at 4 and we didn’t get any number greater than or equal to 4 in the output.

Continue Statement

Continue statement in python is used to skip the part of a ‘for’ or ‘while’ loop whenever a condition gets satisfied.

We usually use it when we don’t want to terminate a loop completely but skip some iterations in between based on some conditions.

Let’s see an python example of a continue statement for better intuition.

output

In the above program, we have written a ‘for loop’ to print all numbers from 0 to 9.

But within the loop, there is also an if statement with condition x==4 along with continue.

This means when x is 4 the continue statement forces the loop to go for its next iteration rather than executing the statements which follows (i.e print(x)).

That is why we didn’t get 4 in the output.

The difference in using the continue statement rather than a break statement is that the loop continues to execute despite the ‘if’ condition is true i.e (x==4).

Pass Statement

Pass statement in python is used as a placeholder for implementations inside functions, loops, etc.

It allows bypassing the syntactical requirement i.e the code blocks such as def, if, class, etc cannot be empty.

Let’s see an example using a pass statement.

output

Notice we got output exactly as if there would be no conditional statement in the program. In short, it didn’t had any effect on the program end result.

We use pass statement inside the loop as a placeholder for future implementations. Use it when you don’t have any statement as of now to write inside if statement but is sure that in the future you will surely have the implementation.

I hope now you understood the significance of break, continue, and pass statements in Python and theirs difference as well.

If you have any doubts or suggestions then please comment below.

Leave a Reply