This blog will give you a comprehensive understanding of how break and continue statements work. We have also explained the flow of break and continue statements with examples and their implementation.
Table of Contents
Explore the world of C programming with this captivating YouTube video—your gateway to a comprehensive learning experience!
Break and Continue Statements: Introduction
Break and continue are control flow statements used in looping statements like ‘for’, ‘while’, or ‘do-while’ to alter the flow of execution of a loop inside a program. These statements are also known as jump statements, as they are used to jump in and out of the loop.
- Break: The ‘break’ statement terminates the loop for a particular condition that is defined inside the program. Once the ‘break’ is encountered in the program, the iteration of the loop stops immediately and exits the loop, then the program continues with the next line of code after the loop.
- Continue: When the ‘continue’ statement is encountered in a loop, it skips the current iteration of the loop and moves on to the next iteration. It makes the loop jump directly to its condition check or increment/decrement expression by skipping the remaining code of that iteration.
If you want to know more about C programming, you can go through this C Programming Certification Course!
How Does the Break Statement Work?
The ‘break’ statement interrupts the flow of the loop, which results in the termination of the loop’s execution. Let’s now understand, step by step, how the flow of the loop goes when we add a break statement to it:
- Loop Initialization and Condition Check: The loop is initialized by checking the condition. If the condition is true, the loop body executes; otherwise, the loop terminates.
- Break Encounter: Inside the loop body, if a condition for the ‘break’ statement is met, the ‘break’ statement is executed.
- Immediate Exit: On encountering the ‘break’ statement, the loop exits the ongoing iteration immediately, regardless of whether the loop condition remains true or false.
- The program then continues the execution of the code after the loop.
Take the first step to become a programming master with our C Programming Tutorial today!
Example of Break Statement in C
Let’s understand the working of the ‘break’ statement with the help of the following example in C:
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d ", i);
if (i == 5) {
break; // When i reaches 5, the loop is terminated
}
}
printf("\nLoop terminated due to break statement.\n");
return 0;
}
Here are the steps of how the program works until the break statement is encountered:
- First, the ‘for’ loop is set to iterate from ‘i = 1’ to ‘i <= 10’.
- During each iteration, the value of ‘i’ is printed.
- Inside the loop, there is an ‘if’ statement that checks if ‘i’ is equal to ‘5’ or not.
- When ‘i’ reaches ‘5’, the condition for the ‘break’ statement is satisfied. As a result, the ‘break’ statement is executed, which causes an immediate exit from the loop.
- After that, the program proceeds with the code after the loop and prints “Loop terminated due to break statement“.
Get ready for high-paying programming jobs with these Top C & Data Structure Interview Questions and Answers!
How Does the Continue Statement Work?
When the ‘continue’ statement in the program is encountered inside a loop, the program continues iteration without executing the remaining code within the current iteration of the loop. Instead of exiting the loop entirely like ‘break’, ‘continue’ takes the loop to the next iteration.
Here are the steps for the working flow of the loop with a continue statement:
- When a loop begins, statements within the loop are executed sequentially.
- If the ‘continue’ statement is encountered during an iteration and its condition is satisfied, the program will skip the iteration that is currently running.
- After encountering ‘continue’, the loop immediately proceeds to the next iteration by skipping the code that follows the ‘continue’ statement in the current iteration.
- The loop condition is re-evaluated, and the loop continues its execution with the next iteration, which means the loop logic is again executed from the beginning (checking the loop condition and executing the loop body).
Example of Continue Statement in C
The ‘continue’ statement is used to skip the execution of the remaining code of the current iteration of a loop and move to the next iteration. Here’s an example that describes the use of the ‘continue’ statement:
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
// Skip printing even numbers
if (i % 2 == 0) {
continue; // Skips the remaining code for even numbers
}
printf("%d ", i);
}
printf("\nLoop finished.\n");
return 0;
}
This program demonstrates the ‘continue’ statement inside a ‘for’ loop that iterates from 1 to 5. Let’s step by step understand the flow of this program after it encounters the ‘continue’ statement:
- Inside the loop, there is a condition ‘if (i % 2 == 0)’ to check if the current value of ‘i’ is an even number or not.
- When ‘i’ is even (‘i % 2 == 0’ is true), the ‘continue’ statement is executed, which orders the program to skip the remaining code inside the loop for that particular iteration.
- As a result, even numbers are not printed by the loop.
- The loop then continues the next iteration after encountering ‘continue,’ which means only odd numbers from 1 to 5 will be printed.
- Finally, the program prints “Loop finished” after the loop completes its execution.
In this example, the ‘continue’ statement skips the ‘printf’ statement for even numbers (‘i % 2 == 0’) and proceeds directly to the next iteration, displaying how ‘continue’ affects the loop’s execution flow by skipping specific parts of the loop for certain conditions.
To become an expert in C programming, practice these C Pattern Programs.
How to Use Break and Continue Statements in C?
Here’s an example that demonstrates the combined use of ‘continue’ and ‘break’ statements. In this example, we calculate the sum of numbers from 1 to 10, but we skip the addition of numbers when they are even by using the ‘continue’ statement, and by using the ‘break’ statement, we break the loop when the sum reaches or exceeds 20.
#include <stdio.h>
int main() {
int sum = 0;
int i;
printf("Calculating sum of numbers from 1 to 10 with break and continue:\n");
for (i = 1; i <= 10; i++) {
// Skip even numbers
if (i % 2 == 0) {
printf("%d is even. Skipping.\n", i);
continue; // Skips adding even numbers
}
printf("%d is odd. Adding to sum.\n", i);
sum += i; // Add odd numbers to the sum
// Break when sum reaches or exceeds 20
if (sum >= 20) {
printf("Sum reached or exceeded 20. Breaking loop.\n");
break; // Terminates the loop when sum reaches or exceeds 20
}
}
printf("Final sum: %d\n", sum);
return 0;
}
Let’s understand how this program is executed:
- This program calculates the sum of numbers from 1 to 10, skipping even numbers using ‘continue’. Inside the loop, when encountering an even number (‘i % 2 == 0’), it skips adding that number to the sum and prints a message.
- For odd numbers, it adds them to the ‘sum’ and prints a corresponding message.
- When the ‘sum’ reaches or exceeds 20, the program prints a message and terminates the loop using ‘break’.
- Finally, the program prints the final sum.
Difference Between Break and Continue Statements in C
To effectively use these loop flow controllers, one needs to understand the differences very carefully. Here, we have covered the major differences between break and continue statements:
Feature | Break | Continue |
Effect on Loop Iteration | Terminates the current iteration and exits the loop | Skips the remaining code in the current iteration and proceeds to the next |
Control Flow | Transfers control to the statement following the loop | Starts the next iteration of the loop |
Applications | Exiting a loop when a specific condition is met | Skipping specific elements within a loop and only processing certain ones |
Get your programming basics cleared and learn functions in C with the help of our detailed guide.
Conclusion
In summary, ‘break’ and ‘continue’ statements are essential control flow tools in C. ‘Break’ allows immediate termination of loops based on certain conditions. On the other hand, ‘continue’ skips specific iterations in the loops. It helps in enhancing control over the loop execution flow. Both statements help programmers manage loop iterations for particular conditions, which improves code efficiency and logic execution.
FAQs
Can 'break' be used in nested loops?
Yes, ‘break’ can be used in nested loops. When encountered, it breaks out of the innermost loop where the ‘break’ statement is placed.
In which loop control structures can 'continue' be used?
‘continue’ can be used with all types of loops in C, including ‘for’, ‘while’, and ‘do-while’ loops.
Can 'break' or 'continue' be used outside a loop?
No, both ‘break’ and ‘continue’ are loop control statements and can only be used within loops. Using them outside of loops will result in a syntax error.
How does 'break' affect nested loops?
When ‘break’ is used in nested loops, it terminates the innermost loop where it is encountered, allowing the program to resume execution after that loop.
Can 'break' be used to exit a switch statement?
Yes, ‘break’ can be used to exit a ‘switch’ statement. When encountered, ‘break’ terminates the ‘switch’ block, preventing fall-through to subsequent cases.
Is it mandatory to include 'break' after every 'case' in a 'switch' statement?
No, it’s not mandatory to include ‘break’ after every ‘case’. However, omitting ‘break’ allows fall-through behavior, executing subsequent ‘case’ statements until encountering a ‘break’ or the end of the ‘switch’ block.
Can 'continue' be used multiple times within a loop?
Yes, ‘continue’ can be used multiple times within a loop. Each occurrence of ‘continue’ within the loop causes the current iteration’s remaining code to be skipped, jumping to the next iteration.