break statement, continue statement in C-language - C-Tutorial

Latest

Sunday, 9 October 2016

break statement, continue statement in C-language

break statement:         The break statement is used in loop control statements such as while, do-while, for and switch statements to terminate the execution of the loop or switch statement.  The general format of break statement is:


           
               Syntax:           break;

            When the keyword break is encountered inside any C loop, control automatically skip entire loop and passes to the statements available after the loop.

/* Example program for break statement */

main()
{
int i;
clrscr();
for(int i=1;i<10;i++)
{
            if(i==5)
            {
            printf("\n Come out from for loop when i equal to 5");
            break;
            }
printf("%3d",i);
getch();
}
continue statement:            The continue statement is used in while, do-while and for statements to terminate an iteration.  The general format of continue statement is:

            Syntax:           continue;

            When the keyword continue is encountered inside any C loop, compiler skips the remaining statements available after the continue statement and control reaches to next iteration of the loop.

/* Example program for continue statement */

main()
{
int i;
clrscr();
for(int i=1;i<10;i++)
{
            if(i==5 || i==6)
            {
            continue;
            }
printf("%3d",i);
getch();

}

No comments:

Post a Comment