In this article, we’ll take a look at how we can use the break statement in C/C++.
Similar to the continue statement, we can use the break statement to manipulate loop control.
Let’s take a look at this, using some examples!
Table of Contents
Using the break statement in C/C++
While we can use the continue statement to jump to the next iteration of the loop, the break statement breaks out of the loop.
This jumps to the next instruction proceeding the loop body while maintaining the state of all non-local variables of the loop.
To see how this works out, let’s look at an example, using while
loops.
Using break statement inside a while loop
We can use break
to directly exit a while
loop.
while(cond) {
if (something)
break;
// Main body
}
Consider the below while loop, which iterates until i > 10.
#include <stdio.h>
int main() {
int i = 0;
while (i <= 10) {
if (i == 5)
break;
printf("i = %d\n", i);
}
printf("Outside the Loop\n");
return 0;
}
If you see what I have done, this program will keep printing the value of i
until 10. But, if i == 5
, it will break out from the loop directly.
The output describes what is happening:
i = 0
i = 1
i = 2
i = 3
i = 4
Outside the Loop
Hopefully, that clears things up regarding break
inside a while loop.
Similar to the while
loop, we can also break from the loop control of a do-while
loop in an almost-identical fashion.
Next, we’ll look at a similar example, using a for
loop.
Using the break statement inside a for loop
We can replicate the above program using a for
loop as well!
#include <stdio.h>
int main() {
for (int i=0; i<10; i++) {
if (i == 5)
break;
printf("i = %d\n", i);
}
printf("Outside the Loop\n");
return 0;
}
This will have the same output as before since we immediately break away when i == 5.
Using the break statement inside a switch statement
Now, this is slightly different from other loop statements like for
and while
.
A switch
statement is a statement which tries to map a target value to a case
value.
If they match, the relevant block is executed. Otherwise, it keeps moving down, until the block is over, or a default
block is encountered.
If we write a switch
statement like the below snippet, we may unknowingly introduce bugs to our program.
#include <stdio.h>
int main() {
for (int i=0; i<=2; i++) {
switch(i) {
case 2:
printf("i is equal to 2\n");
default:
printf("i is not equal to 2\n");
}
}
return 0;
}
Output
i is not equal to 2
i is not equal to 2
i is equal to 2
i is not equal to 2
Why are we getting the last two lines, even though i lies between 0 to 2? The answer is because the default
case is getting executed even if i == 2
. The switch
statement doesn’t stop if one case gets matched.
To avoid this, we must explicitly tell the program to break
away from the switch statement if any case is matched.
So, the common practice is to add the break
statement at the end of every case statement!
#include <stdio.h>
int main() {
for (int i=0; i<=2; i++) {
switch(i) {
case 2:
printf("i is equal to 2\n");
break; // Add break condition
default:
printf("i is not equal to 2\n");
break; // Add break condition
}
}
return 0;
}
Here, I don’t need to use break
after default
, but it is a good practice to do so.
Output
i is not equal to 2
i is not equal to 2
i is equal to 2
Now we get the correct output, and our problem is solved!
Hopefully, you now understand the usefulness of the break
statement in C/C++!
Conclusion
In this article, we learned about using the break statement in C/C++ to break out of loops and case statement blocks.
References
- Microsoft Documentation on C/C++ page on break statement