Continue and break in C

 Continue statement in C

Sometimes in programming during the execution of loop we need to take control beginning of the loop , for this we need to bypass the statements which present inside the loop and not yet been executed . By using keyword continue we can do this . when continue statement usually declared inside the any body of loop like for loop, while loop, do while loop etc , the compiler automatically transfer control to the beginning of the loop. And remaining statements after the continue keywords are skipped. 

Syntax: continue;

Example of Continue statement

#include <stdio.h>

#include<conio.h>

int main()

{

int j;

clrscr();

   for ( j=0; j<=10; j++)

   {

      if (j==5)

      {

        continue;

       }

       printf("%d ", j);

   }

getch();

return 0;

}

 

C – break statement

Sometimes in programming we during the execution we want jump out of the loop instantly , we use break statement is inside a loop we can do this, so the control automatically transfer directly out of loop and passed to first statement after the loop. 

Syntax: break;

Example of Break statement

#include <stdio.h>

int main()

{

int i =0;

while(i<=50)

  {

     printf("value of variable i is: %d\n",i);

if (i==2)

    {

      break;

    }

  i++;

  }

printf("Out of while-loop after break when value of i is : %d\n",i);

return 0;

}

 

C – goto statement

The goto statement is rarely used by programmers because it makes program confusing, less readable and complex code . Also the control of the program won’t be easy to trace, this makes testing and debugging very difficult.

When a goto statement is used in a C program, the control jumps directly to the label mentioned in the goto statement.

Syntax:    

goto label_name;

..

..

label_name: C-statements


Lable in c

A label used as variable name, and it is followed by a colon. We can attach lable to any function as in goto function and its scope is for entire function.

Syntax:


label_name: C-statements


 Example of goto and label statement

#include <stdio.h>

#include<conio.h>

int main()

{

int i, sum=0;

clrscr();

for(i = 0; i<=20; i++)

 {

  sum = sum+i;

  if(i==3

   {

   goto addition;

   }

 }

addition:

printf("%d", sum);

getch();

return 0;

}





Previous
Next Post »