The break statement is used to immediately terminate the
nearest enclosing loop or switch statement.
When C reaches break, control moves to the first statement
after that loop or switch.
break statement stops the nearest
enclosing loop or switch. It does not stop the entire program.
The break statement is used to exit a loop immediately.
break;
After executing break, control moves to the statement after
the nearest enclosing loop.
break;
The break statement consists of the keyword
break followed by a semicolon.
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
printf("%d ", i);
}
Output:
1 2 3 4
When i becomes 5, the break statement terminates
the loop.
for (int i = 1; i <= 10; i++)
{
printf("%d ", i);
if (i == 5)
{
break;
}
}
The loop stops immediately after printing 5.
int i = 1;
while (i <= 10)
{
if (i == 5)
{
break;
}
printf("%d ", i);
i++;
}
Output:
1 2 3 4
int i = 1;
do
{
if (i == 5)
{
break;
}
printf("%d ", i);
i++;
}
while (i <= 10);
The loop stops when i reaches 5.
The break statement is often placed inside an
if statement.
for (int i = 1; i <= 10; i++)
{
if (i > 5)
{
break;
}
printf("%d ", i);
}
The if condition decides when the loop should stop.
The break statement terminates only the nearest enclosing
loop.
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
break;
}
printf("%d ", i);
}
printf("\nProgram continues.");
Output:
1 2
Program continues.
A break statement can be used to terminate an intentionally
infinite loop.
int i = 1;
while (1)
{
printf("%d ", i);
if (i == 5)
{
break;
}
i++;
}
The loop stops when i becomes 5.
int numbers[] = {10, 20, 30, 40, 50};
int target = 30;
for (int i = 0; i < 5; i++)
{
if (numbers[i] == target)
{
printf("Number found!");
break;
}
}
Once the target is found, there is no need to continue searching.
int numbers[] = {4, 8, 12, 16, 20};
int target = 12;
int found = 0;
for (int i = 0; i < 5; i++)
{
if (numbers[i] == target)
{
found = 1;
break;
}
}
if (found)
{
printf("Found");
}
else
{
printf("Not Found");
}
The break stops the search as soon as the target is found.
int number;
while (1)
{
printf("Enter number: ");
scanf("%d", &number);
if (number == 0)
{
break;
}
printf("You entered %d\n", number);
}
The loop continues until the user enters 0.
int choice = 2;
switch (choice)
{
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
default:
printf("Invalid choice");
}
In a switch, break is commonly used to prevent
execution from continuing into the following cases.
int choice = 1;
switch (choice)
{
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
default:
printf("Other");
}
When choice is 1, the program prints only
One and then leaves the switch.
int number;
for (int i = 1; i <= 5; i++)
{
printf("Enter number: ");
scanf("%d", &number);
if (number == 0)
{
break;
}
printf("Number = %d\n", number);
}
Entering 0 immediately terminates the loop.
for (int i = 1; i <= 20; i++)
{
if (i % 7 == 0)
{
break;
}
printf("%d ", i);
}
Output:
1 2 3 4 5 6
The loop stops when it reaches the first multiple of 7.
char ch;
while (1)
{
printf("Enter a character: ");
scanf(" %c", &ch);
if (ch == 'q')
{
break;
}
printf("You entered: %c\n", ch);
}
The loop terminates when the user enters q.
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 5; j++)
{
if (j == 3)
{
break;
}
printf("%d ", j);
}
printf("\n");
}
The break exits the inner loop, not the outer loop.
When loops are nested, an ordinary break exits only the
nearest enclosing loop.
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
if (j == 2)
{
break;
}
printf("i=%d j=%d\n", i, j);
}
}
The outer loop continues after the inner loop is terminated.
The break statement must be inside a loop or switch statement.
It is not a general-purpose statement for exiting an if
statement.
if (number == 5)
{
printf("Number is 5");
}
An if statement itself does not require break.
Sometimes a loop can stop naturally through its condition, while
break allows an early exit.
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
printf("%d ", i);
}
The normal condition allows values through 10, but break
stops the loop earlier at 5.
int i = 1;
do
{
printf("%d ", i);
if (i == 5)
{
break;
}
i++;
}
while (i <= 10);
The loop terminates immediately when i reaches 5.
The position of break affects when the loop terminates.
for (int i = 1; i <= 5; i++)
{
printf("%d ", i);
if (i == 3)
{
break;
}
}
Output:
1 2 3
If the break appears before the printf(), 3 would
not be printed.
If a case does not end with break, execution can
continue into the next case when applicable.
switch (choice)
{
case 1:
printf("One\n");
case 2:
printf("Two\n");
break;
}
When choice is 1, both messages can be printed because there
is no break after case 1.
for (int i = 1; i <= 100; i++)
{
if (i % 9 == 0)
{
printf("First divisible number = %d", i);
break;
}
}
The loop stops at the first number divisible by 9.
int numbers[] = {5, 8, 12, -4, 10};
int size = 5;
for (int i = 0; i < size; i++)
{
if (numbers[i] < 0)
{
printf("First negative number = %d", numbers[i]);
break;
}
}
The loop stops as soon as the first negative number is found.
#include <stdio.h>
int main()
{
int numbers[] = {10, 20, 30, 40, 50};
int target;
int found = 0;
printf("Enter number to search: ");
scanf("%d", &target);
for (int i = 0; i < 5; i++)
{
if (numbers[i] == target)
{
found = 1;
break;
}
}
if (found)
{
printf("Number found.");
}
else
{
printf("Number not found.");
}
return 0;
}
#include <stdio.h>
int main()
{
int number;
while (1)
{
printf("Enter a number (0 to exit): ");
scanf("%d", &number);
if (number == 0)
{
break;
}
printf("Square = %d\n", number * number);
}
printf("Program ended.");
return 0;
}
#include <stdio.h>
int main()
{
int choice;
while (1)
{
printf("\n1. Say Hello\n");
printf("2. Show Message\n");
printf("3. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
if (choice == 1)
{
printf("Hello!\n");
}
else if (choice == 2)
{
printf("Welcome to C programming!\n");
}
else if (choice == 3)
{
break;
}
else
{
printf("Invalid choice.\n");
}
}
printf("Program ended.");
return 0;
}
Here, break provides the exit from the otherwise continuous
menu loop.
Practice the following programs:
break.q.break.break inside a nested loop and observe which loop stops.
Practice these examples carefully to understand that break
provides an immediate exit from the nearest enclosing loop or switch.
break immediately terminates the nearest enclosing loop or switch.for, while, and do-while loops.if condition.break commonly prevents fall-through to later cases.break determines when the loop stops.break does not terminate the entire program by itself.Question: What does the break statement do?