What is a the use of break and continue in Python

What is the use of break and continue in Python?

In Python, break and continue statements can alter the flow of a normal loop.
Loops iterate over a block of code until test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without checking test expression.
The break and continue statements are used in these cases.

Python break statement

The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.
If break statement is inside a nested loop (loop inside another loop), break will terminate the innermost loop.

Syntax of break


Flowchart of break

Flowchart of break statement in Python
The working of break statement in  for loop and while loop is shown below.
How break statement works in Python?

Post a Comment