Table of contents
1.
Introduction
2.
Break Statement
2.1.
Syntax
3.
Working of Break Statement
4.
Implementation of Break Statement
4.1.
C++ Implementation of Break Statement
4.2.
Output
4.3.
Python Implementation of Break Statement
4.4.
Output
5.
Features of Break Statement
6.
Advantages
7.
Disadvantages
8.
Continue Statement
9.
Syntax
10.
Working of Continue Statement
11.
Implementation of Continue Statement
11.1.
C++ Implementation of Continue Statement
11.2.
Output
11.3.
Python Implementation of Continue Statement
11.4.
Output
12.
Features of Continue Statement
13.
Advantages
14.
Disadvantages
15.
Comparison Table between Break and Continue
16.
Frequently Asked Questions
16.1.
What is the major difference between the break and continue statements?
16.2.
When should you use break in a loop?
16.3.
When should you use continue in a loop?
16.4.
Can you use break and continue in all types of loops?
17.
Conclusion
Last Updated: Apr 28, 2024
Easy

Difference between Break and Continue Statement

Author Muskan Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Ninjas!!

While working on your code, many times, a situation comes where you have to use the jump statement to skip the statements or terminate the loop. For this, we use jump statements like Break and Continue. 

Difference between Break and Continue Statement

In this article, you will learn about how break and continue statements work and the difference between break and continue statements. So without further ado, let’s start with the difference between the break and continue statements!!

Break Statement

We have to discuss the difference between break and continue. Firstly, let’s start with the break statement.  The break statement terminates or ends the loop. When the break statement is encountered,  all the iterations in the loop stop and the control flow is shifted outside that loop.

A break statement is useful when you want to stop the flow of the loop early. 

Syntax

break


Now let's have a look at how a break statement works with the help of an example:

Working of Break Statement

The break statement is used to terminate or end the normal flow of the loop. Whenever in the loop, the break statement appears, the loop terminates. After that, the flow of the program shifts to the next line of the code. 

In the body of the loop, you'll set the break statement where you want to break the flow of the loop, and then the flow will be terminated. 

Working of Break Statement

Let's understand this with an example in Python.

for i in range(1, 10):
    if i==6:
       break
    print (i)
print("loop terminated ")
You can also try this code with Online Python Compiler
Run Code


Output

1
2
3
4
5
loop terminated 


Explanation:

In the above example, we are printing the numbers until 5, when 6 is encountered. When our condition becomes true, our loop gets terminated and moves to the next line of the code, and printed, loop terminated

So, that is how a break statement works.

Implementation of Break Statement

Now let’s look into the implementation of the break statement in C++:

C++ Implementation of Break Statement

Following is the C++ implementation of the break statement.

#include<stdio.h>
#include<string.h>

int main() {
  char str[] = "CodingNinjas";
  int i=0;
  for( i=0;i<strlen(str);i++){
      if(str[i]=='j'){
          break;
      }
  }
  printf("Found at index: %d",i);
  return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

Found at index: 9


In this example, we are terminating the loop at the location of j and printing the index of the j. 

Now let's look at the Python code: 

Python Implementation of Break Statement

Following is the Python implementation of the break statement:

for char in "CodingNinjas":  
    if char == "j":  # Checking condition till j comes
        break  
    print(char)
print("Loop Terminated") 
You can also try this code with Online Python Compiler
Run Code

Output

C
o
d
i
n
g
N
i
n
Loop Terminated


Now let’s take a look at some features of the break statement.

Features of Break Statement

  1. Termination of the loop: The break statement terminates the loop. When a break statement is encountered, it terminates the iteration, and the control goes to the next line of the code.
     
  2. Used in the nested loop: If the break statement encounters a nested loop, the innermost loop in the nested loop will be terminated in which the break statement is called.
     
  3. Error Handling: The break statement can be used to exit from the loop when an error can be encountered.
     
  4. Use in “if statement”: Mostly, the break statement is used in if statement. When the condition of if statement becomes true, the break statement is executed and stops the iteration of the loop.


Let’s discuss some advantages and disadvantages of the break statement.

Advantages

  1. It gives a way to stop the execution of the loop at any certain point in time
     
  2. It helps to make the code efficient.
     
  3. It is also used to reduce the time complexity in certain instances.
     
  4. In case of Error handling, Break is used to exit the loop.

Disadvantages

  1. It cannot be used in conditional statements.
     
  2. Also, there is a lack of clarity and maintainability while using break statements.
     
  3. It is not effective for multiple loops. It only exits from the innermost loop.
     
  4. Error finding is sometimes difficult if any error is coming after the break statement and that code is not executed, it may lead to bugs in the final application.

Continue Statement

Now in difference between break and continue. We had discussed the break statement. Now it’s time to discuss the continue statement. The continue statement is also used for terminating the loop. But it is not like a break statement. In the continue statement, the control flow moves to the next iteration and terminates the current iteration, not the whole iteration of the loop.

Syntax

continue

Working of Continue Statement

The continue statement terminates the current iteration of the loop. It is useful when you want to skip a particular iteration of the loop and want to execute the remaining iterations of the loop. 

Let’s understand this, when the loop starts to execute, and the condition is met and becomes true, the continue statement executes and terminates the current iteration. But if the condition is not met and becomes false. Then the remaining part of the loop will be executed.

Working of Continue Statement

Implementation of Continue Statement

Now, let's look into the implementation of the continue statement in C++:

C++ Implementation of Continue Statement

#include<string.h>
#include<stdio.h>

int main() {
  
  for (int i = 0; i <20; i++) {
    if (i%2==0) {
      continue;
    }
    printf("%d\n",i) ;
  }   
  return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

1
3
5
7
9
11
13
15
17
19


Explanation: 

In the above example, if the if condition gets true, it will execute the continue statement for that value of i. Then it will exit that iteration and execute the next iteration. That's all the odd numbers until 20 are printed.

Now, let's look at the Python code: 

Python Implementation of Continue Statement

for i in range(1,20):
   if i%2== 0:
    continue
   print(i)
You can also try this code with Online Python Compiler
Run Code

Output

1
3
5
7
9
11
13
15
17
19


Explanation:

In the above example, if the if condition gets true, like if the number is divisible by 2. It will execute the continue statement for that value of i. Then it will exit that iteration and execute the next iteration. That's all the odd numbers until 20 are printed.

Now let’s have a look at some features of the continue statement.

Features of Continue Statement

  1. Termination of the loop: The continue statement terminates the current iteration in the loop. When a continue statement is encountered, it terminates the current iteration, and the control goes to the next iteration of the loop.
     
  2. Used in the nested loop: If the continue statement encounters a nested loop, the current iteration of the innermost loop will be terminated.
     
  3. Error Handling: The continue statement can be used to skip from the current iteration when an error can be encountered.
     
  4. Use in “if statement”: Mostly, the continue statement is used in if statement. When the condition of if statement becomes true, the continue statement is executed and stops the current iteration of the loop.

Advantages

  1. Continue skips the current iteration and goes to the next iteration. So if we need to skip any iteration, we can use the continue statement.
     
  2. Unless, like the break statement, it does not exit the loop. So only desired iteration is skipped, and further iteration goes on.
     
  3. The execution of the continue statement is at run time, which is helpful for making changes at runtime.

Disadvantages

  1. Improper use of continue may lead to bugs in the code, meaning if not in the correct position, it may skip the wrong iteration.
     
  2. If the continue statement is used at the wrong position, it may lead to an infinite loop.
     
  3. The continue statement has limited uses. We can use common if-else statements or loops to achieve the desired result.


In the next section of the article, we will discuss the difference between break and continue statements.

Comparison Table between Break and Continue

We have already discussed the break and continue statement briefly. Now it’s time to look into the difference between break and continue. 

So let’s begin with the difference between the break and continue statements:

Break Statement 

Continue Statement

In the break statement, the iteration of the loop terminates or ends fully. In the continue statement, the control flow moves to the next iteration and terminates the current iteration.
All of the next iterations will be terminated. Only the current iteration will be terminated.
The flow of control will go to the next line after the loop. The flow of control will go to the next iteration of the loop only.
Break statements can be used in loops and switch statements.  The continue statement can only be used in a loop.
In the Break Statement, the continuation of the loop terminates. In the Continue Statement, the current iteration is terminated.
The left iterations will not be executed after the encounter of the break. The left iterations can be executed after the encounter with continue statement.
The break statement improves performance by terminating unnecessary iterations. The continue statement may not improve the performance because it terminates the current iteration only.

Related Article Loops in Python.

Frequently Asked Questions

What is the major difference between the break and continue statements?

The major difference between the break and continue statements is that the break statement terminates the whole iteration of the loop. While the continue statement terminates the current iteration of the loop.

When should you use break in a loop?

In a loop, we can use a break when we need to exit from the loop completely, i.e., if the loop has performed the required output, then the break is executed, and it comes out of the loop.

When should you use continue in a loop?

In the loop, the continue is used to skip an iteration i.e, the iteration that is running is skipped, and the next iteration is executed. This is done when some code needs not to be executed at some specific points in the loop.

Can you use break and continue in all types of loops?

Yes, both the break statement and continue statements can be used in all the loops like, while, for, and do while. But the continue statement cannot be used in switch statements.

Conclusion

In this article, we learned about what the break statement and continue statement are. Working of the break and continue statement and the implementation of both statements. Lastly, the difference between break and continue statements.

We hope this article helped you in your learning. 

If you want to learn more, refer to these articles: 


For more information, refer to our Guided Path on Coding Ninjas Studio to upskill yourself in PythonData Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!!

Happy Learning Ninja!!

Live masterclass