break and continue statements -Programmer's Mind

There are two loop control statements in C,break and continue.These are unconditional control statements to alter the execution of the program.

break statement:

In C,break is used to exit from the loop and transfers program control to the next statement after the loop.Syntax and example are as follows

Syntax of break statement

break;

Example Program using break statement:

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

Output:

break program output

break can also be used in switch statement.
break should not be used in any statements other than loops,if used compiler will generates an error "Misplaced break"
To know more about break Interesting facts about break
How to use break with switch statement Break with switch

Continue statement:

In C,continue is used to skip the current iteration of the loop and transfers program control to the next iteration i.e., to the condition of the loop.Syntax and example are as follows

Syntax of continue statement

continue;

Example Program using continue statement:

#include<stdio.h>
void main()
 {
  int num=1;
  while(num<=10)
   {
     if(num%2==1)
     {
       num++;
       continue;
     }
     else
     {
       printf("%d\n",num);
       num++;
     }
   }
 }

output:

Continue program output

In above program continue statement is used to skip the odd numbers.
If we use continue with if-else statement then compiler will generates an error "Misplaced continue"
To know more about continue Interesting facts about continue

Previous Topic:

Related Topic:


Next Topic:


Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment