Break and Continue
Break and Continue Statement :
- Break Statement is used to interrupt the loop execution.
- On the occurence of break statement execution control of a program directly comes out of loop by skipping next loop execution and continues with a statement following a loop.
- Syntax : break;
- Example : Let's consider following block of code
int main()
{
int i;
for(i=1;i<=100;i++)
{
pritf(" %d ",i);
if(i==50)
break;
}
return 0;
}
Output :
1 2 3 4 5 ..... 50
- Here though for loop written for printing 100 natural numbers it will print only 50 numbers because of break statement.
- Continue Statement : Continue statement continues with the next iteration of loop by skipping statements following it inside the loop.
- Syntax : continue;
- Example : Let's see following block of code
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=100;i++)
{
if(i%2==0)
continue;
printf("%d",i);
}
return 0;
}
Output :
1 3 5 7 9 ...... 99
- Here program will print only odd numbers between 1 to 100 because of continue statement.
Comments
Post a Comment
Please do not enter any spam link in message.