Sometimes,it is required to jump a loop irrespective of the conditional test value.Break statement is used inside any loop to allow the control jump to the immediate statement following the loop.The syntax is as follows:
break;
When nested loops are used,then the break jumps the control from the loop where it has been used.Break statement can be used inside any loop.
Example
Write a program to calculate the first smallest divisor o a number.
#include<stdio.h>
main()
{
int div,num,i;
printf("Enter any number: \n");
scanf("%d",&num);
for(i=0;i<=num;i++)
{
if((num%i)==0)
{
printf("Smallest divisor for number %d"",num,i);
break;
}
}
}
OUTPUT
Enter any number:
9
Smallest divisor of 9 is 3
break;
When nested loops are used,then the break jumps the control from the loop where it has been used.Break statement can be used inside any loop.
Example
Write a program to calculate the first smallest divisor o a number.
#include<stdio.h>
main()
{
int div,num,i;
printf("Enter any number: \n");
scanf("%d",&num);
for(i=0;i<=num;i++)
{
if((num%i)==0)
{
printf("Smallest divisor for number %d"",num,i);
break;
}
}
}
OUTPUT
Enter any number:
9
Smallest divisor of 9 is 3
0 comments:
Post a Comment