Definition: The break statement is used to immediately terminate the nearest loop or switch-case statement. Program execution continues with the statement that follows the terminated loop or switch.
Definition: The break statement is used
to immediately terminate the nearest loop or switch-case
statement. Program execution continues with the statement that follows
the terminated loop or switch.
break when i == 5,
the loop stops immediately at 5 and does not continue further.
break;
In the following example, the loop is stopped when the value of
i becomes 3.
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 5; i++)
{
if (i == 3)
break; // stop the loop when i becomes 3
printf("%d ", i);
}
return 0;
}
1 2
The break statement is used when the program needs to stop
a loop immediately after a particular condition is satisfied.
When break is used inside a for loop,
the loop terminates immediately. The remaining iterations are skipped.
break executes
at 6, values 6 to 10 are not processed.
The break statement can also terminate a
while loop when a required condition is met.
break to stop the loop.
In a switch-case statement, break is commonly
used to terminate the current case and prevent execution from falling
into the next case.
case 1, break exits the
switch instead of continuing to case 2.
Write a C program to search for the number 7 from
1 to 10. Stop the loop when the number 7 is reached using
the break statement.
break statement.break with a conditional statement inside a loop.
Use a for loop from 1 to 10. When i == 7,
use break to terminate the loop.
The break statement immediately terminates the nearest
loop when it is executed. Control then moves to the statement after
the loop.
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 10; i++)
{
if (i == 7) // stop when 7 is reached
break;
printf("%d ", i);
}
return 0;
}
1 2 3 4 5 6
break terminates only the nearest loop or
switch in which it appears. It does not simply skip the
current iteration; it completely exits the loop.
| break | continue |
|---|---|
| Terminates the loop completely. | Skips the current iteration. |
| Control moves outside the loop. | Control moves to the next iteration. |
| Used when no further repetition is required. | Used when one particular iteration should be skipped. |
break immediately terminates the nearest loop or switch.for, while and do-while loops.switch-case.break, control moves to the statement after the loop or switch.break is different from continue.| Concept | Remember |
|---|---|
| Purpose | Terminate the nearest loop or switch. |
| Syntax | break; |
| Loops | for, while, do-while |
| switch-case | Prevents fall-through between cases. |
Watch a beginner-friendly explanation of the break statement in C programming.
A short handwritten-style revision sheet for the break statement will be provided here.
Use the mind map for quick revision of break, loop termination and switch-case.