Break Statement In C [ Full Information With Examples ]
Break Statement In C -: break is a keyword that is used in C language with an if statement. The break statement is used to bring the control of the program out of the loop and switch.
Suppose we are creating a program in which we are going to search a particular number out of 1000 numbers. If we get that number in the 10th search, then why should we do the remaining 990 searches? Since we found our number only in the 10th search, in such a situation, we would like to end the loop by not doing the rest of the searches.
In C language, if we want to end a loop before its entire condition is running, we use a break statement for this.
Let’s understand these things through a program.
Example Program -:
#include <stdio.h>
int main ()
{
int i;
for (i = 1; i<10; i++)
{
if (i == 5)
{
break; // breaks out of the for loop
}
printf ("Value of i = %d \n", i);
}
// signal to operating system everything works fine
return 0;
}
Output -:
Value of i = 1
Value of i = 2
Value of i = 3
Value of i = 4
How it works
Look at the program above, in that program we want the loop to be over as soon as the value of i is equal to 5 and the control of the program comes out of the loop.
The value of i becomes 5 on the 5th iteration in the program. Whereby (i == 5) condition is checked then the condition comes true from which the break statement is executed and the control comes out from the loop. If there was no break statement, then this loop would execute 9 times.
Syntax of break statement
// loop or switch case
break;
Uses of Break Statement in C
We use the break statement in two things:
- Inside a loop Statement
- Inside switch case statement
Inside a loop Statement
When we use the break statement inside the loop with the if statement, then as soon as the condition of the if statement is true, the break statement goes away and the break statement terminates the loop immediately, causing the control to come out of the loop and The next statement after the loop starts executing.
Example – Use of break in a for loop
#include <stdio.h>
int main ()
{
int var;
for (var = 100; var>= 0; var--)
{
printf ("var: %d \n", var);
if (var == 99)
{
break;
}
}
printf ("Out of for loop");
return 0;
}
Output -:
var: 100
var: 99
Out of for loop
Inside switch case statement
When a break is used at the end of every case within a switch case statement, the break statement brings the control out of the switch statement as soon as the case is executed.
Example – Use of break statement in switch-case
#include <stdio.h>
int main ()
{
int num;
printf ("Enter value of num:");
scanf ("%d", &num);
switch (num)
{
case 1:
printf ("You have entered value 1 \n");
break;
case 2:
printf ("You have entered value 2 \n");
break;
case 3:
printf ("You have entered value 3 \n");
break;
default:
printf ("Input value is other than 1,2 & 3");
}
return 0;
}
Output -:
Enter value of num: 2
You have entered value 2
If we do not use the break statement after every case inside the switch case in the above program, the output would be something like this.
Enter value of num: 2
You have entered value 2
You have entered value 3
Input value is other than 1,2 & 3
Read More -:
- Control Statements in C
- If else Statement in C
- Loop in C
- For Loop in C
- While and Do while Loop in C
- Continue statement in C
- Switch Statement in C
- Conditional Operator in C
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
Friends, hope that after reading this post today, you have learned what is the break statement in C language. And how is the break statement used?
Friends, I hope you have found the answer of your question and you will not have to search about the break statement in C language.
However, if you want any information related to this post or related to programming language, and computer science, then comment below I will clear your all doubts.
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the break statement in C Language.
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.