Net-informations.com
SiteMap  | About    

Python Break and Continue statement

Python break statement

It is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression. In such cases we can use break statements in Python. The break statement allows you to exit a loop from any point within its body, bypassing its normal termination expression.

What's the difference between 'break' and 'continue' in Python?

As seen in the above image, when the break statement is encountered inside a loop, the loop is immediately terminated, and program control resumes at the next statement following the loop.

break statement in while loop

output

In the above program, when n==5, the break statement executed and immediately terminated the while loop and the program control resumes at the next statement.

break statement in while loop

output

Python continue statement

Continue statement works like break but instead of forcing termination, it forces the next iteration of the loop to take place and skipping the rest of the code.

continue statement in while loop

output

In the above program, we can see in the output the 3 is missing. It is because when n==3 the loop encounter the continue statement and control go back to start of the loop.

continue statement in for loop

output

In the above program, we can see in the output the 3 is missing. It is because when n==3 the loop encounter the continue statement and control go back to start of the loop.









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.