break and continue keyword

break and continue keyword :

   break keyword is used in two statements,
     1. switch
     2. loop

In switch statement, every case ends with break keyword. Upon execution of a particular case in switch statement the control of execution will be transferred to the statement written after the switch statement.

If we use break keyword in loop then the loop iterations will be stopped and control of execution will transfer to the statement written after looping statement. If the break is written nested loop then the loop where break keyword is written is will be stopped and control of execution will transfer to parent loop.

Example: for break keyword

#include< stdio.h >
#include< conio.h >
main()
{
int i,j;
for(i=0;i<=5;i++)
{
printf("\ni=%3d",i);
for(j=0;j<=5;j++)
   {
 printf("\nj=%3d",j);
   if(j==3) break;
   }
}
}


continue keyword :

If we use continue keyword in loop then the current loop iteration will be skipped and control of execution will transfer to the statement next iteration and loop execution will not stop. When the continue keyword is executed then the statements written after it will be skipped. 

Example: for continue keyword

#include< stdio.h >
#include< conio.h >
main()
{
int i,j;
for(i=0;i<=5;i++)
    {
        printf("\ni=%3d",i);


        for(j=0;j<=5;j++)
           {
             if(j==3) continue;

                printf("\nj=%3d",j);   
           }
      }
}

Comments