BREAK STATEMENT in C
“Break is a control statement used in C programming to end a loop or switch statement based on specific criteria.”
When a condition has been met, or the loop has run several times, the break statement is frequently used to end the loop early. The program immediately quits the loop or switch statement and resumes execution at the next statement after the loop or switch when a break statement is found in one of those statements. This results in skipping any remaining loop iterations and any switch or loop code after the break statement.
A loop or switch statement's break statement causes the program's control flow to immediately leave the loop or switch statement and go on to the statement that follows the loop or switch statement.
FLOW OF CONTROL IN WHEN IT ENCOUNTERS BREAK STATEMENT :
- The program enters the loop or switch statement and performs the statements contained within it when the loop or switch statement is executed.
- The program's control flow immediately leaves the loop or switch statement and moves on to the statement that follows the loop or switch statement when a break statement is detected in the loop or switch statement.
- If the break statement is contained within a nested loop, it will only exit the innermost loop and proceed to the statement that follows it.
- When a break statement is contained within a switch statement, the switch statement is broken out, and the next sentence after the switch statement is continued.
- The break statement will result in a syntax error and prevent the program from compiling if it is not contained within a loop or switch statement.
A GOOD EXAMPLE OF THE BREAK STATEMENT :
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
printf("\nLoop terminated at i=%d", i);
return 0;
}
The for loop in this illustration iterates from 1 to 10. We utilize the break statement to end the loop early if i equals 5 while it is still in progress.
As can be seen, the loop was stopped early when i equaled 5, and the subsequent loop iterations (6 through 10) were skipped.
BREAK STATEMENT IS USED WITH THE FOLLOWING:
- Simple Loops
- Nested Loops
- Infinite Loops
- Switch case
Exiting a loop is accomplished with the break statement. It can be used to remove program control from any loop.
Example 1: Break Statement with Simple Loop
#include <stdio.h>
int main() {
int i;
// Example 1: using a break in a for loop
printf("Example 1:\n");
for (i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
printf("\n");
// Example 2: using a break in a while loop
printf("Example 2:\n");
i = 0;
while (i < 10) {
if (i == 5) {
break;
}
printf("%d ", i);
i++;
}
printf("\n");
// Example 3: using a break in a do-while loop
printf("Example 3:\n");
i = 0;
do {
if (i == 5) {
break;
}
printf("%d ", i);
i++;
} while (i < 10);
printf("\n");
return 0;
}
Three examples from this program show how to utilize breaks in short loops.
When i equals 5, Example, 1's break command causes the for loop to end early.
To get the same behavior as Example 1, we use a break inside a while loop in Example 2.
To get the same behavior as in Examples 1 and 2, we utilize a break in a do-while loop in Example 3.
The break statement is used to end the loop early in each example, as can be seen so that the loop only runs until i = 5.
Example 2: Break Statement with Nested Loops
#include <stdio.h>
int main() {
int i, j;
// Example 1: using a break in nested for loops
printf("Example 1:\n");
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
printf("%d %d\n", i, j);
if (i == 2 && j == 2) {
break;
}
}
}
printf("\n");
// Example 2: using a break in nested while loops
printf("Example 2:\n");
i = 1;
while (i <= 3) {
j = 1;
while (j <= 3) {
printf("%d %d\n", i, j);
if (i == 2 && j == 2) {
break;
}
j++;
}
i++;
}
printf("\n");
return 0;
}
We have two examples in this program showing how to utilize nested loops breaks.
In Example 1, when i and j are both equal to 2, we utilize to break in nested for loops to end the inner loop early.
To get the same behavior as Example 1, we use to break in nested while loops in Example 2.
As we can see, each example uses the break statement to end the inner loop early. As a result, the inner loop only runs until i and j are equal to 2.
A break statement can only exit one loop at a time. Therefore, instead of breaking out all the loops at once, the control will go to the outer loop if we use the break command in the inner loop of a nested loop. If we wish to exit each loop, we must utilize several break statements.
Example 3: Break Statement with Infinite Loops
#include <stdio.h>
int main() {
int i = 0;
// Example: using a break in an infinite loop
printf("Example:\n");
while (1) {
if (i == 5) {
break;
}
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}
To illustrate the use of breaks in this program, we employ an infinite while loop. Until the break statement, which ends the loop, is encountered, the loop runs indefinitely.
In this instance, when i equals 5, we use the break to end the loop. We use printf to output the value of i to the terminal before the break statement is reached.
As we can see, when i equals 5, the break statement stops the infinite loop, causing the loop to only run until i = 5.
BREAK IN C SWITCH CASE
To stop the execution of a case block in C, the break statement is frequently used in switch statements.
Syntax of break-in switch case
switch(expression)
{
case value1:
statement_1;
break;
case value2:
statement_2;
break;
.....
.....
case value_n:
statement_n;
break;
default:
default statement;
}
When a switch statement is executed, an expression is evaluated and checked to see whether its value matches any of the case labels in the switch block. The program runs the statements in a case block when a matching case label is discovered until it reaches the block's conclusion or comes across a break statement.
Using break in a switch statement is shown below:
#include <stdio.h>
int main() {
int choice = 2;
switch(choice) {
case 1:
printf("You selected option 1.\n");
break;
case 2:
printf("You selected option 2.\n");
break;
case 3:
printf("You selected option 3.\n");
break;
default:
printf("Invalid option.\n");
break;
}
return 0;
}
The switch is used in this program to check the chosen variable's value. A printf command is run if the value meets one of the case labels. The default case is applied if none of the case labels match the value of the choice.
We use the break to stop the switch statement from running in each case block. The program would otherwise carry out the instructions in the following case block without a break statement.
When the program is run with the option set to 2, the following result will be produced:
As can be seen, the switch statement analyses the option's value and executes the case block statements that correspond to the value of the choice. The break statement is used to stop the case block from running and stop the program from running the statements in the next case block.
The break statement in C offers several benefits, including:
- Early loop termination: Depending on a specific condition, the break statement can end a loop early. This can shorten computation times and avoid loop iterations that aren't essential.
- Better program flow control: You can manage program flow by using the break statement. The program can reduce wasteful code execution and increase efficiency by ending a loop or switch statement when a specific condition is fulfilled.
- Better error management: The break statement can be used to manage mistakes more efficiently. The program can stop additional problems from happening and give the user more thorough error messages by ending a loop when an error condition is satisfied.
- Performance gain: When used in lengthy loops, the break statement can dramatically boost a program's performance. Early loop exits allow the program to save time and resources, allowing quicker execution times and more effective use of system resources.
CONCLUSION :
We covered the break statement and how it can be used to change how a C program runs normally. The break statement is especially helpful when we want to end the loop earlier than the regular logic.