Loading...
Loading...
00:00:00

Break and Continue Statement

The break and continue statements in C language are used within loops to modify the control flow of the program.

The break statement is used to exit the loop prematurely, regardless of whether the loop condition has been fully satisfied. This is useful in situations where the loop should terminate early under certain conditions.

The continue statement, on the other hand, is used to skip the current iteration of the loop and proceed with the next iteration. This is useful in situations where certain iterations of the loop are not needed, or where the loop should continue even if a certain condition is not met.

Both "break" and "continue" statements can be used with "for", "while", and "do-while" loops, and they provide additional control over the behavior of the loop.

"break" Keyword

break keyword is used to terminate the loop if some condition become true. break keyword break the loop and exit. break and continue statement can be use in any loops.

Example:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int i = 0;
    for (i = 0; i < 10; i++)
    {
        if (i == 5)
            break;  // terminate the loop
        printf(" %d", i);   // 0 1 2 3 4 
    }
    return 0;
}

"continue" Keyword

continue statement is used to skip the iteration when condition is become true, it does not exit from loop.

Example:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int i = 0;
    for (i = 0; i < 10; i++)
    {
        if (i == 5)
            continue;  // skip this iteration
        printf(" %d", i);        //  0 1 2 3 4 6 7 8 9
    }
    return 0;
}