Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
break statement
2.1.
Example
2.2.
C
3.
continue statement
3.1.
Example
3.2.
C
4.
Difference Between the Break and Continue Statements
5.
Frequently Asked Questions
5.1.
What happens if break is used outside of any loop?
5.2.
Can continue be used in both for and while loops?
5.3.
Is it possible to use multiple break or continue statements in a single loop?
6.
Conclusion 
Last Updated: Jun 11, 2024
Easy

Difference Between Break and Continue in C

Author Rinki Deka
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In programming, controlling the flow of code execution is crucial for creating efficient & effective programs. Two essential tools for controlling code flow in C are the break & continue statements. These statements allow developers to alter the normal flow of loops, providing flexibility & control over how code is executed. 

Difference Between Break and Continue in C

In this article, we will discuss the differences between the break & continue statements in C, with proper examples to understand their usage properly.

break statement

The break statement is used to immediately terminate the execution of a loop (for, while, or do-while) or a switch statement. When encountered within a loop, the break statement causes the program to exit the loop prematurely, regardless of the loop's condition. It then continues with the next statement following the loop.

The break statement is particularly useful when you want to stop the loop iteration based on a specific condition, even if the loop's original condition is still true.

Example

  • C

C

#include <stdio.h>

int main() {

   int i;

   for (i = 1; i <= 10; i++) {

       if (i == 5) {

           break;

       }

       printf("%d ", i);

   }

   printf("\nExited loop at i = %d", i);

   return 0;

}

Output:

1 2 3 4 


Exited loop at i = 5

In this example, the for loop is set to iterate from 1 to 10. However, when i reaches the value of 5, the break statement is encountered. This causes the loop to terminate immediately, and the program continues with the next statement after the loop. As a result, only the numbers 1 to 4 are printed, and the loop exits when i equals 5.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

continue statement

The continue statement is used to skip the remainder of the current iteration in a loop (for, while, or do-while) and proceed to the next iteration. When the continue statement is encountered, the program immediately jumps to the next iteration of the loop, skipping any code that follows the continue statement within the loop body.

The continue statement is helpful when you want to skip specific iterations based on a certain condition but continue with the remaining iterations of the loop.

Example

  • C

C

#include <stdio.h>

int main() {

   int i;

   for (i = 1; i <= 10; i++) {

       if (i % 2 == 0) {

           continue;

       }

       printf("%d ", i);

   }

   printf("\nPrinted only odd numbers");

   return 0;

}

Output

1 3 5 7 9 


Printed only odd numbers

In this example, the for loop iterates from 1 to 10. Inside the loop, there is an if statement that checks if the current value of i is even. If i is even, the continue statement is executed, causing the program to skip the rest of the current iteration and move on to the next iteration. As a result, only the odd numbers (1, 3, 5, 7, 9) are printed.

Difference Between the Break and Continue Statements

FeatureBreak StatementContinue Statement
FunctionExits the loop or switch statement immediately.Skips the current iteration of the loop and proceeds to the next.
UsageUsed to terminate a loop when a specific condition is met.Used to skip the remainder of the current loop iteration when a condition is met.
Flow ImpactTerminates the entire loop or exits the switch case.Continues with the next iteration of the loop without exiting the loop.
Typical UseTo break out of a loop or switch when a termination condition is fulfilled, e.g., finding a target value in an array.To avoid certain conditions within a loop while still completing the remaining iterations, e.g., skipping invalid or irrelevant data.
Execution PointStops loop execution and moves to the statement immediately following the loop or switch.Jumps to the loop's condition check or to the next iteration of a for-loop (i.e., increment section).
Control FlowProvides a way to escape from a loop or switch regardless of its original condition.Allows selective continuation of loop execution, modifying behavior without stopping the loop.
Example ContextExiting a loop upon successful completion of a task or error condition that requires no further iteration.Continuously processing items that meet certain criteria while skipping those that don’t, within a data processing routine.

Frequently Asked Questions

What happens if break is used outside of any loop?

Using break outside of a loop or switch statement results in a compile-time error. It must be used within a loop or switch to function correctly.

Can continue be used in both for and while loops?

Yes, continue can be used in both for and while loops. It is also applicable in do-while loops, effectively skipping the current iteration and continuing with the next loop cycle.

Is it possible to use multiple break or continue statements in a single loop?

Yes, you can use multiple break or continue statements in a single loop, depending on different conditions to control the loop execution more precisely.

Conclusion 

In this article, we have learned about the differences between the break & continue statements in C. The break statement is used to terminate a loop entirely, while the continue statement skips the current iteration and proceeds to the next. We saw their usage with proper codes which should have cleared the confusion between these two statements.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Previous article
Conditional Statement
Next article
Control Structures in C
Live masterclass