Python break continue pass

#Day3 – Break, Pass, and Continue in Python

Posted by

The keywords Break, Pass and Continue are used to control the flow of code inside a loop.
First, we will discuss the three keywords individually. Then we will move on to using all three of them inside a while loop and try to figure the output.

Break

The break keyword is used when we want to exit the loop. Once the break statement is executed

  • none of the lines inside the loop below the break statement are executed.
  • There are no more iterations in the loop, the code outside the loop, if any, starts getting executed.
image.png

Once the if condition is satisfied (i =4), we exit the for loop. The last output will be
Num: 4

Pass

The pass keyword is used when we want to ignore specific lines of code inside our loop. It is usually put inside an if/else block. Once the pass statement is executed

  • We just skip the code inside the if or else block depending on where the pass statement is
  • The code inside the loop following the if/else block is executed
  • We move on to the next iteration
image.png

The print statement (Num : {i}) and (Squared Num : {i*i}) will be output during each iteration. The print statement ({i} is less than 3) will only be printed as long as i <=3. Once i>3, the pass statement is executed and we just exit the if/else block.

Continue

The continue keyword is pretty similar to the pass keyword. The only difference being that, once the continue statement is executed, the entire iteration of the loop is ignored. i.e None of the lines inside the loop below the continue statement are executed during that iteration. However, we do not exit the loop, we simply skip that iteration of the loop. Once a continue statement is executed:

  • None of the lines below the statement is executed for that iteration
  • We move on to the next iteration

image.png
We will use the same example as before. We have changed the pass statement to a continue statement. The only difference in the output is that the print statement (Squared Num :{i}) is no longer printed out in each iteration. Since we are using a continue statement, we ignore that print statement.

Example

We will use a while loop and all the above-discussed statements.

image.png

Can you guess what’s the output?
Consider the value of **i **in the while condition

  • i in [0,1,2] : Both print statements are displayed
  • i in [3,4]: Although the pass statement is executed, both print statements are displayed.
  • i in [5,6]: Only the first print statement is displayed. Since the continue statement is executed, all the lines below it are ignored.
  • i in [7,8,9]: Although we put a break statement, the first print statement is still displayed and we do not exit the loop. Why? When i> 7, i is also greater than 5 and the continue statement is executed thus ignoring the break statement

Be Careful when using break, pass, and continue statements. If we had incremented the variable i after the continue statement, it would be an infinite loop after i =5.

Summary

  • Use break when you want to exit the loop
  • Use pass when you to skip over a few lines of coder
  • Use continue when you want to skip an iteration of the loop