Break Statement in C
Break Statement in C
Break Statement in C
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.
https://www.tutorialspoint.com/cprogramming/c_break_statement.htm 1/8
6/16/24, 12:24 PM Break Statement in C
In both the scenarios, break causes the control to be taken out of the current scope.
while(condition1){
...
...
if(condition2)
break;
...
...
}
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.
https://www.tutorialspoint.com/cprogramming/c_break_statement.htm 2/8
6/16/24, 12:24 PM Break Statement in C
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 <stdio.h>
int i = 2;
int x = 121;
printf("x: %d\n", x);
return 0;
}
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
https://www.tutorialspoint.com/cprogramming/c_break_statement.htm 3/8
6/16/24, 12:24 PM Break Statement in C
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 following program prints the characters from a given string before a vowel (a, e,
I, or u) is detected.
#include <stdio.h>
#include <string.h>
int main () {
return 0;
}
Output
R
h
https://www.tutorialspoint.com/cprogramming/c_break_statement.htm 4/8
6/16/24, 12:24 PM Break Statement in C
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 <stdio.h>
int main(){
int i, num, n, flag;
printf("The prime numbers in between the range 1 to 30:\n");
Output
2 is prime
3 is prime
https://www.tutorialspoint.com/cprogramming/c_break_statement.htm 5/8
6/16/24, 12:24 PM Break Statement in C
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
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 <stdio.h>
#include <stdlib.h>
#include <time.h>
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;
}
}
Output
https://www.tutorialspoint.com/cprogramming/c_break_statement.htm 6/8
6/16/24, 12:24 PM Break Statement in C
On running this code, you will get an output like the one shown here −
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 <stdio.h>
int main(){
switch(ch) {
case 'm':
printf("Good Morning \n");
break;
case 'a':
printf("Good Afternoon \n");
break;
case 'e':
printf("Good Evening \n");
break;
https://www.tutorialspoint.com/cprogramming/c_break_statement.htm 7/8
6/16/24, 12:24 PM Break Statement in C
default:
printf("Hello");
}
}
Output
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
https://www.tutorialspoint.com/cprogramming/c_break_statement.htm 8/8