What Are Break and Continue Statements in Python

Read it in 5 Mins

Last updated on
07th Jun, 2022
Published
23rd Mar, 2021
Views
11,436
What Are Break and Continue Statements in Python

In this article, you will learn about the Loop control statement , the necessity of using these statements to control the loop's flow, and their use according to our requirements. Parallelly, to understand sys.argv command line argument, click here. 

The article has been organized in the following manner: 

  • Introduction to Loop Control Statement 
  • Break statement 
  • Syntax of break statement 
  • Flow diagram and steps involved 
  • Example of break statement 
  • Continue Statement 
  • Syntax of continue statement 
  • Flow diagram and steps involved 
  • Example of continue statement 
  • Pass statement 
  • Syntax of pass statement 
  • Example of Pass statement 
  • Difference between break and continue statement 
  • Conclusion 

Introduction to Loop Control Statement

Break and Continue statements are also known as Loop Control Statements, as they automate the working of the inner loop. Loop automates the task and repeats in a certain manner, but there are some special cases where we need to terminate the loop or skip some iteration. There we use Loop Control Statement that is Break and Continue.

Python Supports 3 Basic Loop Control Statements 

  • Break Statement 
  • Continue Statement 
  • Pass Statement 

Break Statement:

It works similar to a break statement in C. It terminates the current working loop and passes the control to the next statement, and if the break statement resides inside the nested loop, it passes control to the outer loop. It can be used with both while and for loops. 

Forms of Break Statement:

  1. Labelled break statement 
  2. Unlabelled break statement 

Labelled break statement: It terminates the loop's outer statement, which has been marked with the given label.

Unlabelled break statement: It terminates the innermost statement of the loop

Uses of break statement: 

  • It is used when we want to skip or terminate a certain statement. 
  • When some external condition is triggered, and it requires the termination of the loop.
Syntax: break

Flow diagram of break statement:

Flow diagram of break statement

Here are the steps involved in the flow chart given above explaining a break statement 

  1. Execution of loop statement begins 
  2. If the looping statement is true, iteration starts. 
  3. If the looping statement contains a break statement, the iteration will terminate, and then the current loop will exit. 
  4. When the condition completes its execution, it will return its output. 
  5. If the looping statement is false, it will exit from the loop. 
  6. End of the loop. 

Example: 

Let's consider the condition where we want to iterate a loop from 1 to 10 When 6 is encountered, we want to break the loop and return to the outer condition. Here we use break statement with for loop and if statement. 

#Demonstration of Python break statement 
#using for loop 
#loop from 0 to 9 
for i in range(10): 
       # if i is equal to 6 
       # break statement terminate the current working loop   
       if i == 6: 
             break 
       else: 
              print (i)

Output:  

 
1  
2  
3  
4  
5 

Continue Statement:  

It is the second form of the Loop Control Statement, very similar to the break statement. But when it comes to the working of the continue statement, it works just opposite to the break statement. Instead of terminating certain conditions, it jumps off to the very next condition. But it will continue the execution of the loop statement as per its name. 

Forms of continue statement: 

  1. Labelled continue statement 
  2. Unlabelled continue statement 

Labelled continue statement: It skips the iteration of the outermost loop, marked with the label. 

Unlabelled continue statement: It skips the iteration of the innermost loop. 

Uses of Continue statement: 

  • When we don’t want certain conditions to run. 
  • When we don’t want to terminate the loop and just want to skip iteration. 
  • When we want to continue the iteration. 
Syntax : continue

Flow diagram of continue statement:

Flow diagram of continue statement

Here are the steps involved in the flow chart given above explaining a continue statement: 

  1. Execution of loop statement begins 
  2. If the looping statement is true, iteration starts. 
  3. If the looping statement contains a continue statement, the iteration will skip the condition statement and then jump to the next iteration statement. 
  4. When the condition completes its execution, it will return its output. 
  5. If the looping statement is false, it will exit from the loop. 
  6. End of the loop. 

Example:  

The continue statement is used in the program to skip any iteration, such as when we don't want to print a certain statement. Consider the example of a break statement altering the condition. There are numbers from 1 to 10, but we don't want 2 to come and we don't want to increase the number of looping statements in our program either. For this we should use a continue statement. 

Refer to the below implementation:

#Demonstration of Python continue statement 
#using for loop 
#loop from 0 to 9 
for i in range(10): 
       # if i is equal to 2 
       # continue statement will skip   
       #and start executing next iteration 
       if i == 2: 
             continue 
        else: 
              print (i)

Output: 

 
 
 
 
 
 
 
 
 

Pass statement:  

Pass statement is not found in traditional C language; it works like a null operation because nothing happens when the pass statement is executed. It is used in Python when any statement is required syntactically, but we don't want any command or code lines to be executed.

  • Pass statement is also known as the placeholder, which can be used inside the function call, classes, and lopping or control statement.  
  • It can be used as a declarative statement, which can be implemented later on when required. 
  • It is just a block of code that runs when called and works the same as the null statement. 
  • The best place to use the pass statement is Stubs. 

Uses of Pass Statement: 

  • It can be used in Stubs. 
  • It can be used while writing empty loops. 
  • It can be used for empty control statements. 
  • It can also be used to implement empty functions and classes. 
Syntax: pass

Example: 

#Demonstration of Python pass statement 
#using for loop 
#loop from 0 to 9 
for i in range(10):      
       # if i is equal to 6 
       # pass statement will execute  
       #but there will be no impact  
       #on the looping statement  
       if i == 6: 
             pass 
             print ‘Just a pass statement’ 
       else: 
              print (i) 

Output:

0 
1 
2 
3 
4 
5 
Just a pass statement 
7 
8 
9

Difference between break and continue statement:  

  • The main difference between both the statements is that when break keyword comes, it terminates the execution of the current loop and passes the control over the next loop or main body, whereas when continue keyword is encountered, it skips the current iteration and executes the very next iteration in the loop. 
  • In simple words break statement leaves a loop, whereas the continue statement jumps to the next iteration. 

Conclusion:   

These are the loop control statements, which help us to terminate or skip some required statement under the normal procedure. Every statement has its function, and none of them can be considered to be better than the other, as they each have different aspects and usage. 

Profile

Abhresh Sugandhi

Author

Abhresh is specialized as a corporate trainer, He has a decade of experience in technical training blended with virtual webinars and instructor-led session created courses, tutorials, and articles for organizations. He is also the founder of Nikasio.com, which offers multiple services in technical training, project consulting, content development, etc.