The Break Statement :-
The keyword break allows us to jump out of a loop instantly without waiting to get back to the conditional test. When the keyword break is encountered inside any C loop, automatically passes to the first statement after the loop. For e.g. the following program is to determine whether a number is prime or not
Logic : To test a number is prime or not, is to divide it successively by all numbers from 2 to one less than itself. If the remainder of any of the divisions is zero, the number is not a prime.
Following program implements this logic
main( )
{
int num i;
printf("Enter a number");
scanf("%d",&num);
i = 2;
while (i <= num-1)
{
if(num% i = = 0)
{
printf("Not a prime number");
break;
}
i++;
}
if(i == num)
printf("Prime number");
}
The Continue Statement:-
The keyword continue allows us to take the control to the beginning of the loop bypassing the statements inside the loop which have not yet been executed. When the keyword continue is encountered inside any C loop control automatically passes to the beginning of the loop.
For example
main( )
{
int i,j;
for(i=1; i<=2; i++)
{
for(j=1; j<=2; j++)
{
if(i = = j)
continue;
printf("%d%d", i,j);
}
}
}
The output of the above program would be.........
12
21
When the value of i equal to that of j, the continue statement takes the control to the for loop (inner) bypassing rest of the statement pending execution in the for loop (inner).
The keyword break allows us to jump out of a loop instantly without waiting to get back to the conditional test. When the keyword break is encountered inside any C loop, automatically passes to the first statement after the loop. For e.g. the following program is to determine whether a number is prime or not
Logic : To test a number is prime or not, is to divide it successively by all numbers from 2 to one less than itself. If the remainder of any of the divisions is zero, the number is not a prime.
Following program implements this logic
main( )
{
int num i;
printf("Enter a number");
scanf("%d",&num);
i = 2;
while (i <= num-1)
{
if(num% i = = 0)
{
printf("Not a prime number");
break;
}
i++;
}
if(i == num)
printf("Prime number");
}
The Continue Statement:-
The keyword continue allows us to take the control to the beginning of the loop bypassing the statements inside the loop which have not yet been executed. When the keyword continue is encountered inside any C loop control automatically passes to the beginning of the loop.
For example
main( )
{
int i,j;
for(i=1; i<=2; i++)
{
for(j=1; j<=2; j++)
{
if(i = = j)
continue;
printf("%d%d", i,j);
}
}
}
The output of the above program would be.........
12
21
When the value of i equal to that of j, the continue statement takes the control to the for loop (inner) bypassing rest of the statement pending execution in the for loop (inner).
0 Comments:
Post a Comment