Join Our 4-Week Free Gen AI Course with select Programs.

Request a callback

or Chat with us on

Difference Between Break and Continue in Python

Basics of Python
Basics of Python
icon
5 Hrs. duration
icon
9 Modules
icon
1800+ Learners
logo
Start Learning

Both Break and Continue statements are known as jump statements, as they transfer the control of the program to other parts of the program. They are always used within a loop. The break statement is used to exit the current loop, whereas the continue statement moves the current loop iteration to the next loop. Let’s explore the key differences between break and continue statements in detail.

Break Statement

The break statement terminates or ends the enclosing loop, such as for, do-while, etc. When the compiler encounters a break statement, all iterations in the loop terminate, and control comes outside the loop. It is used when you want to stop the loop early.

 

Syntax: break

Working of Break Statement

The break statement ends the natural flow of the loop. In a loop, when a break is encountered, the loop terminates, and the flow of the program is shifted to the next line of the code. To understand it better, see the flow chart given below.

Python Implementation of Break Statement

Let’s dive deep into the implementation of the break statement in the Python programming language.

 

Example 1: Break statement with For loop

# break statement with for loop # define the list lis = [1, 0, -3, 99, 10, 42, 2] # target number result = 42 # iterating over the list for item in lis: if item == result: print("Found the target number") # used break to terminate the loop when target number is found break

Output:

Found the target number

In the above example, the break statement is used to terminate the loop when the target number is found.

 

Example 2: Break statement with While loop

# break statement with while loop # declaring variable i = 0 # while loop while True: print(i, " ", end = "") # increasing the value by 1 i = i + 1 if i == 9: # when value become 9 # used break to terminate the loop break print("loop ends")

Output:

0  1  2  3  4  5  6  7  8  loop ends

In the above example, the break statement is used to terminate the loop when the value of variable “i” becomes 9.

 

Example 3: Break statement with Multiple loops

# break statement with multiple loops # define the 2-D array arr = [[21, 22, 23], [14, 15, 16], [47, 48, 49]] # target element result = 5 # iterating over the list present in an array for i in arr: # iterating through all elements of each list for j in i: # check if any element in the nested array is the result if (j == result): print("Element found") # break terminates the innerloop and control goes to the outer loop break else: print(j)

Output:

1 2 3 4 Element found 7 8 9

In the above example, the single break will only terminate the inner loop, and the outer loop will continue to run.

Features of Break Statement

The break statement provides the following features:

 

  • Loop Termination: It terminates the loop. Whenever a break statement is encountered within a loop, it terminates the iteration, and control goes to the next line of the program.
  • Error Handling: It can be used in error handling. It will terminate the execution whenever an error is encountered.
  • Used in Nested Loops: It can be used inside nested loops. When a break statement is encountered, the innermost loop will be terminated, and control goes to the outermost loop.
  • Used in if Statement: It is widely used in if statement. When the “if” condition becomes true, the break statement is executed, terminating the current execution of the program.

Advantages of Break Statement

  • It improves the coding efficiency.
  • It is used to reduce the time complexity of programs in certain instances.
  • It is widely used in Error handling.
  • It is also used inside nested loops and terminates the loop when certain conditions are met.

Disadvantages of Break Statement

  • Break can’t be used in conditional statements.
  • There is a lack of clarity and maintainability around its use.
  • It is not effective when the number of loops is more than two.
  • It is difficult to find an error if it comes after a break statement. It will decrease code efficiency.

Continue Statement

The continue statement terminates the current iteration, not the entire loop. When the compiler encounters a continue statement, the current iteration will be skipped, and the control moves to the next iteration.

 

Syntax: continue

DevOps & Cloud Engineering
Internship Assurance
DevOps & Cloud Engineering

Working of Continue Statement

The continue statement skips the current iteration of the loop. It is specially used when you want to skip a certain iteration and perform operations on the remaining iterations. To understand it better, see the flow chart given below.

Python Implementation of Continue Statement

Let’s understand the implementation of the continue statement in the Python programming language.

 

Example 1: Continue statement with For loop

# continue statement with for loop # iterating from 20 to 30 for itr in range(20, 31): # when an element is equal to 25, continue is executed, # The current iteration will terminate, and the next iteration will start if itr == 25: continue # print the number otherwise print(itr, end=" ")

Output:

20 21 22 23 24 26 27 28 29 30

In the above example, the continue statement is used to skip the iteration when the desired result is achieved; otherwise, print the elements.

 

Example 2: Continue statement with While loop

# continue statement with while loop # defining a string str = "Continue" # initialize an iterator itr = 0 # while loop starts while itr < len(str): # when found character 'n' used continue to skip the remaining execution # and move to the next iteration if(str[itr] == 'n'): itr = itr + 1 continue # otherwise print characters print(str[itr]) itr = itr+1

Output:

C o t i u e

In the above example, the continue statement is used to skip the iteration of the loop when the value of the character becomes “n” while the remaining characters are printed.

 

Example 3: Continue statement with Multiple loops

# continue statement with multiple loops # define the 2-D array arr = [[51, 20, 33], [14, 5, 16], [7, 48, 49]] # iterating over the list present in the array for i in arr: # iterating through all elements of each list for j in i: # check if any element is even if (j %2 == 0): # continue skips the current iteration, and control goes to the outer loop continue else: # printing the odd elements print(j)

Output:

51 33 5 7 49

In the above example, the continue statement will skip all even elements and print all the odd elements in the array.

Features of Continue Statement

The continue statement provides the following features:

 

  • Loop Termination: It terminates the current iteration in the loop. Whenever a continue statement is encountered within a loop, it terminates the current iteration, and control goes to the next iteration.
  • Used in Nested Loops: It can be used inside nested loops. When a continue statement is encountered, the current iteration of the innermost loop will be terminated.
  • Error Handling: It can be used in error handling. It will skip the current iteration whenever an error is encountered.
  • Used in if Statement: It is commonly used in if statement. When the “if” condition becomes true, the continue statement is executed, stopping the current iteration of the loop.

Advantages of Continue Statement

  • When there is a need to skip any iteration of the loop, a continue statement is the best option.
  • Unlike the break statement, it doesn’t terminate the loop; rather, the desired iteration is skipped, and further iteration goes on.
  • It is used to reduce the time complexity of programs in certain instances.

Disadvantages of Continue Statement

  • Improper use of the continue statement leads to bugs in the code.
  • If the continue statement is used in an inaccurate position, it can cause an infinite loop condition.
  • It has limited usage and is commonly used with the for loop and if statement to obtain the desired results.

Comparison Table

Let’s understand the difference between break and continue statements using a comparison table.

 

Break Statement Continue Statement
It terminates or ends all the iterations of the loop. It only ends or skips the current iteration of the loop.
The control of the program will go to the next line after the loop. The flow of control will go to the next iteration of the loop.
It can be used in both loops and switch statements. It is only used in loops.
It improves performance by terminating unnecessary iterations. It may not improve performance as it terminates the current iteration only.

Conclusion

This concludes our discussion on the difference between break and continue statements in Python. We learned about how break and continue statements work with their implementation, features, advantages, and disadvantages. In the end, we even used a comparison table to differentiate between them.

FAQs
In a loop, the break statement is used when we need to terminate the loop completely. If the loop has achieved the required output, then the break is executed, and the control comes out of the loop.
In a loop, the continue statement is used when we need to skip the current iteration. If the loop has achieved the desired result, then continue is executed, the current iteration is skipped, and control goes to the next iteration.
The break statement is used to terminate the whole iteration of the loop, whereas the continue statement is used to terminate the current iteration of the loop.
Yes, the break and continue statements can be used with all types of loops, like for, while, and do while. Unlike continue statements, the break is used with switch statements.

Deploying Applications Over the Cloud Using Jenkins

Prashant Kumar Dey

Prashant Kumar Dey

Associate Program Director - Hero Vired

Ex BMW | Google

19 October, 12:00 PM (IST)

Limited Seats Left

Book a Free Live Class

left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.

Data Science

Accelerator Program in Business Analytics & Data Science

Integrated Program in Data Science, AI and ML

Accelerator Program in AI and Machine Learning

Advanced Certification Program in Data Science & Analytics

Technology

Certificate Program in Full Stack Development with Specialization for Web and Mobile

Certificate Program in DevOps and Cloud Engineering

Certificate Program in Application Development

Certificate Program in Cybersecurity Essentials & Risk Assessment

Finance

Integrated Program in Finance and Financial Technologies

Certificate Program in Financial Analysis, Valuation and Risk Management

Management

Certificate Program in Strategic Management and Business Essentials

Executive Program in Product Management

Certificate Program in Product Management

Certificate Program in Technology-enabled Sales

Future Tech

Certificate Program in Gaming & Esports

Certificate Program in Extended Reality (VR+AR)

Professional Diploma in UX Design

Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

© 2024 Hero Vired. All rights reserved