Understanding the break Statement in C: Usage and Examples
Table of Content:
break statement
C supports three jump statements: break
, continue
, and return
.
These statements transfer
control to another part of your program. Each is discusssed here.
Two keywords, break
and continue
, can be used in loop statements to provide addi-
tional controls. Using break and continue can simplify programming in some cases.
The break statement terminates the loop (for, while and do...while loop) immediately when it is encountered. The break statement is used with decision making statement such as if...else.
Syntax of break statement
break;
The simple code above is the syntax for break statement.
Flowchart of break statement
How break statement works?
Example of break
statement
By using break, you can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop. When a break statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop. Here is a simple example:
#include void main() { int i; for(i=1;i<=10;i++){ if(i==7){ break; } printf("%d \n",i); } }
Output
1 2 3 4 5 6 Press any key to continue . . .
As you can see, although the for loop is designed to run from 0 to 10, the break statement causes it to terminate early, when i equals 7
Example of break
statement in while loop
// Using break to exit a while loop.
#include
void main()
{
int i = 0;
while(i < 100) {
if(i == 10) break; // terminate loop if i is 10
printf("i:%d \n",i);
i++;
}
printf("Loop complete. \n");
}
Output
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
Loop complete.
Press any key to continue . . .
C Break Statement with Inner Loop
When used inside a set of nested loops, the break statement will only break out of the innermost loop.
// Using break with nested loops. #include void main() { int i,j; for(i=0; i<2; i++) { printf("Pass %d",i); for(j=0; j<100; j++) { if(j == 10) break; // terminate loop if j is 10 printf(" %d",j); } printf(" \n"); } printf("Loops complete.\n"); }
Output
Pass 0 0 1 2 3 4 5 6 7 8 9 Pass 1 0 1 2 3 4 5 6 7 8 9 Loops complete. Press any key to continue . . .
Another Example of break
Statement with Inner Loop
// Using break with nested loops. #include void main() { int i, j ; for(i=1;i<=2;i++){ for(j=1;j<=3;j++){ if(i==2&&j==2){ break; } printf(" %d",i); printf(" %d \n",j); } } }
Output
1 1 1 2 1 3 2 1 Press any key to continue . . .
the break
statement in the inner loop only causes termination of that loop.
The outer loop is unaffected.
1. More than one break statement may appear in a loop
2. The break that terminates a switch statement affects only that switch statement and not any enclosing loops.
Another Example
The Below program calculates the sum of maximum of 10 numbers. It's because, when the user enters negative number, the break statement is executed and loop is terminated.
// Program to calculate the sum of maximum of 10 numbers // Calculates sum until user enters positive number # include int main() { int i; double number, sum = 0.0; for(i=1; i <= 10; ++i) { printf("Enter a n%d: ",i); scanf("%lf",&number); // If user enters negative number, loop is terminated if(number < 0.0) { break; } sum += number; // sum = sum + number; } printf("Sum = %.2lf",sum); return 0; }
Output
Enter a n1: 2.4 Enter a n2: 4.5 Enter a n3: 3.4 Enter a n4: -3 Sum = 10.30 Press any key to continue . . .