Skip to content

Break – Continue statements in C

Break Statement:

The break statement is used to exit a loop prematurely, regardless of whether the loop’s termination condition has been met. When encountered, the break statement causes immediate termination of the loop and transfers control to the statement following the loop.

Syntax:

break;

Example:

#include <stdio.h>

int main() {

    int i;

    for (i = 1; i <= 10; i++) {

        if (i == 5) {

            break; // exit the loop when i equals 5

        }

        printf(“%d “, i);

    }

    return 0;

}

In this example, the loop will terminate when the value of i becomes equal to 5, as the break statement will cause immediate termination of the loop.

Continue Statement:

The continue statement is used to skip the remaining code within a loop iteration and move to the next iteration of the loop. It allows you to bypass certain statements within the loop’s body based on a specific condition.

Syntax:

continue;

Example:

#include <stdio.h>

int main() {

    int i;

    for (i = 1; i <= 10; i++) {

        if (i % 2 == 0) {

            continue; // skip even numbers

        }

        printf(“%d “, i);

    }

    return 0;

}

In this example, the continue statement is used to skip printing even numbers. When i is even, the continue statement is executed, causing the loop to move to the next iteration without executing the printf statement.

Key Differences:

  • break is used to terminate the loop prematurely and exit the loop entirely, while continue is used to skip the remaining code within a loop iteration and move to the next iteration.
  • break can be used within any loop (for, while, do-while), while continue is typically used within loop constructs.

Both break and continue statements are powerful tools for controlling the flow of execution within loops and are commonly used in various programming scenarios to achieve specific requirements.

Here’s a comparison between the break and continue statements in tabular format:

FeatureBreak StatementContinue Statement
PurposeTerminates the loop prematurely, exitingSkips the remaining code within the
the loop entirelycurrent iteration and moves to the next
Syntaxbreak;continue;
ScopeCan be used within any loop constructCan only be used within loop constructs
EffectExits the loop immediately, transferringSkips the remaining code within the
control to the statement following the loopcurrent iteration and moves to the next
ConditionTypically used to exit the loop based on aTypically used to skip certain
specific conditionstatements within the loop based on a
specific condition
Example“`c“`c
for (i = 1; i <= 10; i++) {for (i = 1; i <= 10; i++) {
if (i == 5) {if (i % 2 == 0) {
break;continue;
}}
printf(“%d “, i);printf(“%d “, i);
}}
“`“`

Conclusion:

The break and continue statements are both control flow statements used within loops in C programming, but they serve different purposes. While break is used to exit the loop prematurely, continue is used to skip the remaining code within the current iteration and move to the next iteration. Understanding the differences between these two statements is essential for effective loop control and achieving specific programming requirements.