Loading, please wait...

Break Statement

The break statement, when executed in a while, for, do…while or switch statement causes an immediate exit from that statement. Program execution continues with the next statement. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch statement.

 

Example

/*using the break statement in a for statement */

#include <stdio.h>

int main (void)
{
int x;

/*loop 10 times*/
for (x = 1; x<=10; x++) {
/* if x is 5, terminate loop */
if ( x == 5 ) {
break;
}

printf(“%d”, x);
}
printf(“\nbroke out of loop at x== %d\n”, x);
return 0;
}

 

Output:

1 2 3 4

Broke out of loop at x == 5