Python Break

The break statement in Python terminates the loop and continues to execute the remaining code if there is any. It is used most of the time to search for a value in an algorithm.

Let us have an example of break statement within a for loop

Example

Python break for loop

Flow Chart

Python Break Flow Chart

The break statement can also be used in a while loop as well in the following manner.

Example

Python break while loop

As you saw in the example above, we used the break statement to terminate a while loop before its completion

Example

Python break nested loop

You can see in the example above that even when the break statement got executed, it did not exit out of the while and for loop.

Continue Statement

continue statement in Python is the opposite of the break statement, when the continue statement is executed, instead of exiting the loop, the compiler goes back to the beginning of the loop and continues the next iteration.

Example

Python continue statement

The code above will not print the value 5 because the compiler did not reach the print statement. Here is a flow chart of the continue  statement to understand the logic better.

Flow Chart

In Python, “print” is used quite often to output variables. Two similar data types can be combined using the “+” operator to give a single data unit.

Python Continue Flow Chart

Tutorials for all brains!