The break statement in C is used in two different contexts. In switch-case, break is placed as the last statement of each case block. The break statement may also be employed in the body of any of the loop constructs (while, do–while as well as for loops).
When used inside a loop, break causes the loop to be terminated. In the switch-case statement, break takes the control out of the switch scope after executing the corresponding case block.
The flowchart of break in loop is as follows −
The following flowchart shows how to use break in switch-case −
In both the scenarios, break causes the control to be taken out of the current scope.
The break statement is never used unconditionally. It always appears in the True part of an if statement. Otherwise, the loop will terminate in the middle of the first iteration itself.
while(condition1)The following program checks if a given number is prime or not. A prime number is not divisible by any other number except itself and 1.
The while loop increments the divisor by 1 and tries to check if it is divisible. If found divisible, the while loop is terminated.
#include /*break in while loop*/ int main () < int i = 2; int x = 121; printf("x: %d\n", x); while (i < x/2)< if (x % i == 0) break; i++; >if (i >= x/2) printf("%d is prime", x); else printf("%d is not prime", x); return 0; >
On executing this code, you will get the following output −
x: 121 121 is not prime
Now, change the value of "x" to 25 and run the code again. It will produce the following output −
x: 25 25 is not prime
You can use a break statement inside a for loop as well. Usually, a for loop is designed to perform a certain number of iterations. However, sometimes it may be required to abandon the loop if a certain condition is reached.
The usage of break in for loop is as follows −
for (init; condition; increment)The following program prints the characters from a given string before a vowel (a, e, I, or u) is detected.
#include #include int main () < char string[] = "Rhythmic"; int len = strlen(string); int i; for (i = 0; i < len; i++)< if (string[i] == 'a' || string[i] == 'e' || string[i] == 'i' || string[i] == 'o' || string[i] == 'u') break; printf("%c\n", string[i]); >return 0; >
Run the code and check its output −
R h y t h m
If break appears in an inner loop of a nested loop construct, it abandons the inner loop and continues the iteration of the outer loop body. For the next iteration, it enters the inner loop again, which may be broken again if the condition is found to be true.
In the following program, two nested loops are employed to obtain a list of all the prime numbers between 1 to 30. The inner loop breaks out when a number is found to be divisible, setting the flag to 1. After the inner loop, the value of flag is checked. If it is "0", the number is a prime number.
#include int main() < int i, num, n, flag; printf("The prime numbers in between the range 1 to 30:\n"); for(num = 2; num > if(flag == 0) printf("%d is prime\n",num); > return 0; >
2 is prime 3 is prime 5 is prime 7 is prime 11 is prime 13 is prime 17 is prime 19 is prime 23 is prime 29 is prime
An infinite loop is rarely created intentionally. However, in some cases, you may start an infinite loop and break from it when a certain condition is reached.
In the following program, an infinite for loop is used. On each iteration, a random number between 1 to 100 is generated till a number that is divisible by 5 is obtained.
#include #include #include int main() < int i, num; printf ("Program to get the random number from 1 to 100: \n"); srand(time(NULL)); for (; ; )< num = rand() % 100 + 1; // random number between 1 to 100 printf (" %d\n", num); if (num%5 == 0) break; >>
On running this code, you will get an output like the one shown here −
Program to get the random number from 1 to 100: 6 56 42 90
To transfer the control out of the switch scope, every case block ends with a break statement. If not, the program falls through all the case blocks, which is not desired.
In the following code, a series of if-else statements print three different greeting messages based on the value of a "ch" variable ("m", "a" or "e" for morning, afternoon or evening).
#include int main() < /* local variable definition */ char ch = 'm'; printf("Time code: %c\n\n", ch); switch(ch) < case 'm': printf("Good Morning \n"); break; case 'a': printf("Good Afternoon \n"); break; case 'e': printf("Good Evening \n"); break; default: printf("Hello"); >>
Here, the break statement breaks the program execution after checking the first case.
Time code: m Good Morning
Now, comment the break statements and run the code again. You will now get the following output −
Time code: m Good Morning Good Afternoon Good Evening Hello