The break statement stops a loop or switch early. It helps you exit when you find what you want or need to stop.
Break statement in C
break;The break statement is just the word break followed by a semicolon.
It can only be used inside loops (for, while, do-while) or switch statements.
i becomes 5, the break stops the loop.for (int i = 0; i < 10; i++) { if (i == 5) { break; } printf("%d\n", i); }
i is 3.int i = 0; while (i < 10) { if (i == 3) { break; } printf("%d\n", i); i++; }
switch (day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; default: printf("Other day\n"); break; }
This program prints numbers from 1 to 6. When i reaches 7, the break stops the loop early.
#include <stdio.h> int main() { for (int i = 1; i <= 10; i++) { if (i == 7) { break; } printf("Number: %d\n", i); } printf("Loop stopped at 7.\n"); return 0; }
Using break can make your code faster by stopping loops early.
Be careful not to overuse break; sometimes it can make code harder to read.
Break only exits the closest loop or switch it is inside.
The break statement stops loops or switch cases immediately.
It is useful to exit early when a condition is met.
Remember to use break only inside loops or switch statements.
Practice
What does the break statement do inside a loop in C?
Solution
Step 1: Understand the role of break in loops
Thebreakstatement is designed to stop the loop immediately when executed.Step 2: Identify what happens after break
After breaking, the program continues with the first statement after the loop block.Final Answer:
It immediately exits the loop and continues with the code after the loop. -> Option AQuick Check:
Break stops loop immediately = A [OK]
- Confusing break with continue
- Thinking break pauses the loop
- Assuming break restarts the loop
Which of the following is the correct syntax to use break inside a for loop in C?
for(int i = 0; i < 5; i++) {
if(i == 3) {
?
} printf("%d ", i);
}Solution
Step 1: Recall the keyword to exit loops
In C,break;is the correct statement to exit loops immediately.Step 2: Check syntax correctness
Onlybreak;is a valid C keyword; others likeexit;orstop;are invalid here.Final Answer:
break; -> Option CQuick Check:
Correct break syntax = D [OK]
- Using 'exit;' instead of 'break;'
- Confusing 'continue;' with 'break;'
- Missing semicolon after break
What will be the output of the following C code?
int i;
for(i = 0; i < 5; i++) {
if(i == 2) {
break;
}
printf("%d ", i);
}Solution
Step 1: Trace loop iterations until break
The loop starts i=0, prints 0; i=1, prints 1; when i=2, break triggers and loop stops.Step 2: Determine printed values
Only 0 and 1 are printed before break stops the loop.Final Answer:
0 1 -> Option AQuick Check:
Break at i=2 stops loop, prints 0 1 [OK]
- Including value at break condition in output
- Confusing break with continue
- Assuming loop runs fully
Identify the error in this C code snippet using break:
int i = 0;
if(i == 0) {
break;
}Solution
Step 1: Check where break is used
Thebreakstatement is inside anifbut not inside a loop or switch.Step 2: Recall break usage rules
In C,breakmust be inside loops or switch; using it outside causes a compile error.Final Answer:
break used outside loop or switch statement -> Option DQuick Check:
Break outside loop/switch = error [OK]
- Using break inside if without loop
- Forgetting break requires semicolon (not the main error here)
- Assuming break works anywhere
Consider the nested loops below. What will be the output?
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
if(j == 2) {
break;
}
printf("%d%d ", i, j);
}
}Solution
Step 1: Analyze inner loop behavior with break
Inner loop runs j=1 to 3, but breaks when j==2, so only j=1 prints.Step 2: Trace all iterations
For i=1, prints 11; i=2 prints 21; i=3 prints 31; each inner loop stops after j=1.Final Answer:
11 21 31 -> Option BQuick Check:
Break stops inner loop at j=2, prints only j=1 per i [OK]
- Assuming inner loop prints all j values
- Confusing break effect on outer loop
- Including j=2 prints
Ready to prove it?
Pass the test and this pattern moves unknown
