Python Break and Continue


In this section we will learn about the ‘continue’ and ‘break’ statements that are used with the looping constructs and will demonstrate that how these statements affect the looping construct.

The ‘break’ and ‘continue’ statements are used when the user wants to terminate through a loop even if the condition is not false.

 

Python break statement

The break statement is used when the user wants to terminate the loop even if the condition is true. When the break statement is used the control is transferred outside the loop.

An important point to note is that if the break statement is used with inner most loops of the nested loops then the inner most loop will be terminated.

SYNTAX

The following is the syntax of the break statement in Python programming language:

break

The only keyword is used to break through a loop in Python.

 

Flowchart of break statement in Python

flowchart-of-break-statement-in-python

The pictorial representation of the working of break statement with for loop and while loop is shown below:

 

FOR LOOP

for val in sequence:

    if condition:

           break

Outside body of loop     arrors

 

WHILE LOOP

while expression:

    if condition:

           break

Outside body of loop    arrors

In the above structure it is clearly seen that the break statement will terminate the loop either the ‘for’ loop or ‘while’ loop and the control will be transferred outside the body of the loop.

 

EXAMPLE of break statement in Python

Consider the following example, in which the working of break statement is illustrated:

CODE

>>> for i in “Python”:

          if i == “y”:

                   break

          print(i)

OUTPUT

P

In the above example, example we used an if statement in while loop that when the counter variable that was iterating through the given sequence that is a string that is “Python” and when i, the counter variable, becomes equal to y then the break statement should be executed under the ‘if’ statement and if i is not equal to y then the loop should be executed and ‘if’ statement should not be executed.

 

Python Continue Statement

In Python the continue statement is used in the body of the loop. It is used to move control to the start of the body of loop. When this statement is executed in the body of the loop, the remaining statements of current iterations are not executed. The control directly moves to the next iteration.

The syntax of the continue statement is given below:

 

SYNTAX

continue

The only keyword is used to continue through a loop in Python.

 

Flowchart of continue statement:

flowchart-of-continue-statement

The pictorial representation of the working of continue statement with for loop and while loop is shown below:

 

FOR LOOP

for val in sequence:

Inside body of loop

  if condition:             arror

continue

Outside body of loop

 

WHILE LOOP

while expression:

Inside body of loop

    if condition:         arror

           continue

Outside body of loop

In the above structure it is clearly seen that the continue statement will never execute the statement after it this is because when continue statement is executed the control moves back to the start of the body.

 

EXAMPLE of continue statement in Python

Consider the following example, in which the use of continue statement is illustrated:

CODE

for i in “Python”:

          if i == “t”:

                   continue

          print(i)

OUTPUT

P

y

h

o

n

in-the-above-example

In the above example, a for loop is used and there is a condition that if the counter variable that was traversing to the items of the sequence, becomes equal to the character ‘t’ then the loop should continue from the beginning if not then i the counter variable should be printed.

In this way we got all the characters in output except t. Because when counter variable became equal to ‘t’ the loop continued and the print statement was not executed.