Introduction
Learning to code can be a difficult endeavor, especially when it comes down to the techniques used to control program flow. Knowing how and when to use the break, continue statement in C is essential for writing efficient code that remains organized with minimum clutter. In this article, you will discover various scenarios concerning each of these three statement types that should help make programming in C an easier task!
What is the Break statement in C
The break statement in C language is used to exit from a loop or switch statement, prematurely, before the loop or switch block has been fully executed. When a break statement is encountered inside a loop, it immediately terminates the loop, and control is transferred to the next statement outside the loop. Similarly, when a break statement is encountered inside a switch block, it terminates the block, and control is transferred to the next statement outside the block.
Example
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // terminates the loop when i is equal to 5
}
printf("%d\n", i);
}
Types of break statements in C
There are two types of break statements in C, which are
- Break statement in switch case
- Break statement in for loop
Break statement in switch case in C
In C programming language, the switch statement is used to select one of several blocks of code to be executed, depending on the value of an expression. The break statement is used inside a switch statement to terminate the execution of the enclosing switch statement and exit the block of code. When a break statement is encountered inside a switch block, the control flow is transferred to the end of the switch block, and the remaining case statements are skipped. This is useful when the developer wants to terminate a switch block early or when the developer wants to avoid executing unnecessary code.
Example
switch (expression) {
case 1:
// code to be executed if expression is equal to 1
break;
case 2:
// code to be executed if expression is equal to 2
break;
case 3:
// code to be executed if expression is equal to 3
break;
default:
// code to be executed if expression doesn't match any case
break;
}
Break statement in for loop in C
In C programming language, the break statement is used to exit a loop immediately. It is commonly used within a for loop to terminate the loop before its normal end condition is reached. The break statement can be placed anywhere inside the for loop's body, and when executed, it immediately terminates the loop, and control is transferred to the next statement after the loop.
Example
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d\n", i);
}
Output
0
1
2
3
4
What is continue statement in C
In C programming, the continue statement is used to skip the current iteration of a loop (for loop, while loop, or do-while loop) and move to the next iteration. When the continue statement is encountered inside a loop, the control of the program immediately goes to the loop's increment/decrement part, skipping the remaining statements in the current iteration. The loop then continues with the next iteration.
Syntax
//loop statements
continue;
//some lines of the code which is to be skipped
Example
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
printf("%d\n", i);
}
Summary
To summarize, breaks and continues allow for more advanced flexibility in C programming. These statements allow the programmer to control the content of the loop which can save a lot of time down the line. Therefore, understanding how these statements work and when to use them is important when developing in C. It's also important to look up other resources online to understand what scenarios fit best with each statement and help you become a better programmer. If you follow these tips and practice coding in C, you'll start seeing quite quickly a different level of programming complexity that would have seemed impossible before gaining familiarity with Breaks and Continues.
Take our free skill tests to evaluate your skill!

In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.