Nearby lessons

47 of 124

C - Break Statement

Learn the break statement in C — it immediately stops a loop (or a switch) and jumps to the code after it.

What is break?

break immediately stops the loop completely — the remaining iterations are skipped and execution continues after the loop.

Trainer's Memory Trick: break is the emergency exit — it leaves the building at once.

Break in a for Loop

Here the loop is supposed to run 5 times, but break stops it when i becomes 3:

Example02
CCode Cell
1#include <stdio.h>
2void main() {
3 int i;
4 for (i = 1; i <= 5; i++) {
5 if (i == 3) break; // stop at 3
6 printf("%d ", i);
7 }
8 printf("\nLoop ended\n");
9}
Output

1 2 
Loop ended
      

break in while and switch

break works in while, do-while, for and switch. In a switch, break prevents fall-through to the next case:

Example03
CCode Cell
1#include <stdio.h>
2void main() {
3 int n = 2;
4 switch (n) {
5 case 1: printf("One\n"); break;
6 case 2: printf("Two\n"); break; // without this break,
7 case 3: printf("Three\n"); break; // 'Three' would print too
8 }
9}
Output
Two
📝 Key Takeaways
  • break immediately stops the loop completely
  • Execution continues after the loop
  • break also prevents switch fall-through

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2