C Language : break and continue statement

Break and continue statements

Break

On execution of “break” statement the control jumps out of the loop.

Syntax:

{
—-
—-
if (condition)
break;
—–
—–
}

The break statement can be used with for loop, while loop , do while loop and switch statement.

Continue

On execution of “continue” statement the control jumps to the beginning of the loop.

Syntax:

{
—-
—-
if (condition)
continue;
—–
—–
}

The continue statement can be used with for loop, while loop , do while loop.

Example of break statement with for loop

#include <stdio.h>
int main()
{
    int i;
    for(i=1;i<10;i++)
    {
        if(i>5)
            break;
        printf("%d\n",i);
    }
    return 0;
}

Output:

1
2
3
4
5

Example of break statement with while loop

#include <stdio.h>
int main()
{
    int i=1;
    while(i<=10)
    {
        if(i>5)
            break;
        printf("%d\n",i);
        i++;
    }
    return 0;
}

Output:

1
2
3
4
5

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects