C Break Statement: Usage and Examples
In C language, the keyword “break” is used to interrupt a loop or switch statement, causing the program to jump out of the current loop or switch statement’s execution. When the break statement is executed, the program will immediately exit the current loop or switch statement and continue executing the code following the loop or switch statement. This is very useful in situations where it is necessary to prematurely terminate a loop or switch statement. For example:
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d\n", i);
}
return 0;
}
In this example, the break statement will be executed when i equals 5, causing the program to exit the loop and output the result:
0
1
2
3
4