Break and continue statements
Break and continue statements in C programming language are program code control flow statements. These are used by C programmer to change the normal flow of program execution within a program loop. In normal language break statement breaks the execution of C program code. Whereas continue statement continues the break loop control flow or prints the remaining statements on the screen.
Break Statement in C Language.
In C programming break statement is used to exit the loop control immediately, regardless of the currently applied program loop condition. Normally wherever break statement is added in the loop, the loop will run till that point in the program and the statement will print break and the loop will be terminated.
Break Statement Syntax in C Language.
for (int p = 0; p <=10; p++)
{
if (p == 7)
{
break; // Exit the program loop when p is equals 7
}
printf(“\n %d”, p);
}
Usage of break statement.
The break statement is usually used to prematurely terminate or break the apply loop in the program based on some program conditions.
The break statement is often used to exit the current apply loop when a specific programming condition is met. So that unnecessary program choices or iterations can be prevented in a program.
Continue Statement in C Programming.
The continue statement in C programming language is used to skip the remainder of the current program iteration and proceed to the next iteration statement of the loop. It is used to continue the condition after the break statement.
Continue Statement Syntax in C Programming.
for (int q = 0; q < =10; q++)
{
if (q == 7)
{
continue; // here continue statement Skip the current iteration when q reach to 7
}
printf(“\n %d”, q);
Use of continue statement.
The continue statement in C language is used when C programmers want to skip some iterations of a program loop based on some specific programming conditions.
The continue statement allows the C programmer to bypass some remaining program code execution within a loop without completely exiting the current program loop source code.
Use of both break and continue statements in C programming language.
#include <stdio.h>
int main()
{
// Example using break statement
printf(“\n with break statement -“);
for (int p = 0; p < 10; p++)
{
if (p == 7)
{
break; // Exit the loop when p reach to 7
}
printf(“\n %d “, p);
}
printf(“\n”);
// Example using continue in c
printf(“\n with continue statement -“);
for (int q = 0; q < 10; q++)
{
if (q % 2 == 0) {
continue; // it ignore even number
}
printf(“%d “, q);
}
printf(“\n”);
return 0;
}
About break and continue statements.
In C language break and continue statements are used to break and continue loop body statements in any program loop (for, while, do-while).
Both break and continue statements provide mechanism to programmer to control the flow of program execution within the loop. This increases program control by making program loop behavior flexible.
You should always be careful when using break and continue statements in a C program to ensure that the intended logic of the program loop is maintained and unintended program code side effects are avoided.