1- break is a keyword.
2-The break statement is used within a loop or switch statement on execution,it transfer the control outside the loop or switch statement within which it is enclosed.
3-The break statement should always be dependent upon a condition.
for( _ ; _ ;_ )
{ _
_
if(condition)
break;
else
_
_
}
Example of break statement in for loop -:
#include<stdio.h>
#include<conio.h>
main()
{ int i=1;
for(i=1;i<=5;i++)
{ if(i==3)
break;
else
printf(" %d \n ",i);
}
getch();
}
Output-: 1
2
Syntax of break statement in while loop-:
while(condition)
{ _
_
if(condition)
break;
else
_
_
}
Example of break statement in while loop -:
#include<stdio.h>
#include<conio.h>
main()
{ int i=1;
while(i<=5)
{ if(i==3)
break;
else
printf(" %d \n ",i);
i++;
}
getch();
}
Output-: 1
2
Syntax of break statement in do-while loop-:
main()
{initialize;
do
{ _
_
if(condition)
break;
else
_
_
}while(condition);
}
Example of break statement in do-while loop -:
#include<stdio.h>
#include<conio.h>
main()
{ int i=1;
do
{ if(i==3)
break;
else
printf(" %d \n ",i);
i++;
} while(i<=5);
getch();
}
Output-: 1
2
ads
share blog
ReplyDelete