The break
statement in C is used to immediately exit from a loop or a switch
statement, effectively terminating the loop or switch
execution and transferring control to the next statement after the loop or switch
.
Syntax
break;
break
exits the innermost loop orswitch
block that it is inside.- It is commonly used in loops (such as
for
,while
, ordo...while
) to exit early when a specific condition is met. - It is also used in
switch
statements to terminate the case block after executing a matching case.
Example 1: Using break
in a for
loop
In a for
loop, the break
statement can be used to exit the loop prematurely based on a certain condition.
#include <stdio.h> int main() { for (int i = 1; i <= 10; i++) { if (i == 6) { break; // Exit the loop when i equals 6 } printf("i = %d\n", i); } return 0; }
Output:
i = 1 i = 2 i = 3 i = 4 i = 5
In this example:
- The loop runs from
i = 1
toi = 10
. - When
i
equals 6, thebreak
statement is executed, and the loop is terminated, so the remaining iterations (from 6 to 10) are skipped.
Example 2: Using break
in a while
loop
The break
statement can also be used in a while
loop to exit when a certain condition is met.
#include <stdio.h> int main() { int i = 1; while (i <= 10) { if (i == 6) { break; // Exit the loop when i equals 6 } printf("i = %d\n", i); i++; } return 0; }
Output:
i = 1 i = 2 i = 3 i = 4 i = 5
In this example:
- The
while
loop runs as long asi
is less than or equal to 10. - When
i
equals 6, thebreak
statement is executed, causing the loop to terminate.
Example 3: Using break
in a do...while
loop
The break
statement can be used in a do...while
loop to exit the loop early.
#include <stdio.h> int main() { int i = 1; do { if (i == 6) { break; // Exit the loop when i equals 6 } printf("i = %d\n", i); i++; } while (i <= 10); return 0; }
Output:
i = 1 i = 2 i = 3 i = 4 i = 5
Here:
- The
do...while
loop executes the block of code first, then checks the condition. The loop would normally run untili
reaches 10, but thebreak
statement exits the loop wheni
equals 6.
Example 4: Using break
in a switch
statement
The break
statement is also commonly used in switch
statements to terminate the execution of a particular case
.
#include <stdio.h> int main() { int num = 2; switch (num) { case 1: printf("Number is 1\n"); break; // Exit the switch statement case 2: printf("Number is 2\n"); break; // Exit the switch statement case 3: printf("Number is 3\n"); break; // Exit the switch statement default: printf("Number is not 1, 2, or 3\n"); } return 0; }
Output:
Number is 2
In this example:
- The
switch
statement checks the value ofnum
. - The program enters
case 2
, prints"Number is 2"
, and thebreak
statement is executed, terminating theswitch
block.
Without the break
statement, the program would continue executing subsequent case
blocks (called fall-through behavior), which is usually not desired.
Example 5: Using break
in Nested Loops
In nested loops, break
only exits the innermost loop in which it is placed. If you want to exit from multiple levels of loops, you would need additional control logic or labels.
#include <stdio.h> int main() { for (int i = 1; i <= 3; i++) { // Outer loop for (int j = 1; j <= 3; j++) { // Inner loop if (i == 2 && j == 2) { break; // Breaks the inner loop when i = 2 and j = 2 } printf("i = %d, j = %d\n", i, j); } } return 0; }
Output:
i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 2, j = 1 i = 2, j = 2
In this example:
- The outer loop runs from
i = 1
toi = 3
. - The inner loop runs from
j = 1
toj = 3
. - When
i
equals 2 andj
equals 2, thebreak
statement is executed, causing the inner loop to terminate, but the outer loop continues.
Summary
- The
break
statement is used to exit from loops (for
,while
,do...while
) orswitch
statements early. - It helps control the flow of the program by terminating the loop or
switch
when a specific condition is met. - In nested loops, the
break
statement exits only the innermost loop, so you need additional logic if you want to break out of multiple loops.