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.
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.
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.
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.
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
Feature
Break Statement
Continue Statement
Function
Exits the loop or switch statement immediately.
Skips the current iteration of the loop and proceeds to the next.
Usage
Used 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 Impact
Terminates the entire loop or exits the switch case.
Continues with the next iteration of the loop without exiting the loop.
Typical Use
To 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 Point
Stops 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 Flow
Provides 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 Context
Exiting 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.