Ad Code

Responsive Advertisement

break and continue statement in c language.

Break in C language :

Sometimes, it is required to jump out of 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.

 Syntex : break;

when nested loops are used, then break jumps the control from the loop where it has been used. break statement can be used inside any loop
 ex, while, do-while, for and also in switch statement.

  Examples :
//Write a program to calculate first smallest divisor of a number.
#include<stdio.h>
void main()
{
    Int a, i;
   
    Printf("Enter a number ") ;
    scanf (" %d", & a) :
   
    for(i=2; i<=num; i++)
    {
        if((num%i) ==0)
        {
            printf("smallest divisor for number                                     %d is %d", a, i) ;
            break;
        }
    }
}


The continue statement : 

unlike break statement, which is used to jump the control out of the loop, it is something required to skip part of the loop and to continue the execution with next loop iteration.
 continue statement used inside the loop helps to bypass the section of a loop.

  Syntan:
        continue;
Example :

// Write a program to print first 20 natural numbers skip the divisible by 5.
#include<stdio.h>
void main()
{
    int i;
   
    for(i=1; i<=20; i++)
    {
        if((i%5)==0)
        continue;
        printf("%d", i) ;
    }
}
out put :
1 2 3 4  6 7 8 9 11 12 13 14 16 17 18 19


❮ Previous                                                      Next ❯

Post a Comment

0 Comments