Python break and continue Statement

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 cheking test expression. The break and continue statements are used in these cases.

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 it is inside a nested loop (loop inside another loop), break will terminate the innermost loop.

Syntax of break

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

Example: Python break 


# Program to show the use of break statement inside loop

for val in "string":
    if val == "i":
        break
    print(val)

print("The end")
Output

s
t
r
The end
In this program, we iterate through the "string" sequence. We check if the letter is "i", upon which we break from the loop. Hence, we see in our output that all the letters up till "i" gets printed. After that, the loop terminates.

continue statement

The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.

Syntax of Continue

continue

Flowchart of continue 

Flowchart of continue statement in Python

The working of continue statement in for and while loop is shown below.
How continue statement works in python

Example: Python continue


# Program to show
# the use of continue
# statement inside loops

for val in "string":
    if val == "i":
        continue
    print(val)

print("The end")
Output

s
t
r
n
g
The end
This program is same as the above example except the break statement has been replaced with continue. We continue with the loop, if the string is "i", not executing the rest of the block. Hence, we see in our output that all the letters except "i" gets printed.

2 comments: