The break statement is used to immediately exit a loop (for, while, do-while) or a switch statement, even if the loop’s condition has not been fully satisfied.
Syntax:
break;
1. It exits the innermost loop or switch in which it is used.
2. After the break statement is executed, the program control moves to the next statement after the loop or switch block.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 4) {
break; // Exit the loop when i is 4
}
printf("%d\n", i);
}
printf("Loop terminated.\n");
return 0;
}
Output:
1
2
3
Loop terminated.
2
3
Loop terminated.
Explanation:
- The loop prints numbers 1, 2 and 3.
- When i becomes 4, the break statement exits the loop, and the program continues with the statement after the loop.
C Break Statement – Interview Questions
Q 1: What is the break statement in C?
Ans: The break statement is used to terminate a loop or switch statement immediately.
Q 2: Where can break be used in C?
Ans: Inside for, while, do-while loops, and switch statements.
Q 3: What happens when break is executed inside a loop?
Ans: Control exits the loop and moves to the next statement after the loop.
Q 4: Does break terminate all nested loops?
Ans: No, it only terminates the innermost loop.
Q 5: Why is break used?
Ans: To stop execution when a specific condition is met.
C Break Statement – Objective Questions (MCQs)
Q1. What is the main purpose of the break statement in C?
Q2. In which of the following can a break statement be used?
Q3. What happens when a break statement is executed inside a loop?
Q4. What is the output of this code?
for (int i = 1; i <= 5; i++) {
if (i == 3)
break;
printf("%d ", i);
}
Q5. What effect does break have when used outside of a loop or switch?