C-if-else-if-Ladder .

In this session i have discussed if-else-if ladder in C .


IF-Else-IF-Ladder :

It is also decision making and branching statement . If you have multiple choice then you can use the if-else-if ladder or switch . In if-else-if case first control check the if condition if it is true then the contorl come inside the if body other wise control transfer to else-if statement check the condition if it is true then contorl come inside the else if body and go on . If no any condition match then last else statement become execute .

IF-Elese-IF-statement-syntax :

 image


Example : Write a program check the days of week according to given value .

    #includ<stdio.h> 
    int main()
    {
          int i=3;
          if(i==1)
          {
               printf("Sunday");
          }
          else if(i==2)
          {
               printf("Monday");
          }
          else if(i==3)
          {
               printf("Tuesday");
          }
          else if(i==4)
          {
               printf("Wednesday");
          }
          else if(i==5)
          {
               printf("Thursday");
          }
          else if(i==6)
          {
               printf("Friday");
          }
          else if(i==7)
          {
               printf("Sutarday");
          }
          else 
          {
               printf("Invalid Number");
          }
               printf("\nThanks");
          return 0;
    }
    
    OUTPUT : Tuesday
             Thanks
    


Example : Write a program that perform 1-Addition , 2-Subtraction , 3-multiplication , 4 Division .

    #includ<stdio.h> 
    int main()
    { 
           int op=2;
           int x=20;
           int y=10;
           if(op==1)
           {
               int sum=x+y;
               printf("Sum= %d",sum);
           }
           else if(op==2)
           {
               int sub=x-y;
               printf("Sub= %d",sub);
           }
           else if(op==3)
           {
               int multi=x*y;
               printf("Multi= %d",multi);
           }
           else if(op==4)
           {
               int div=x/y;
               printf("Div= %d",div);
           }
           printf("\nThanks");
           return 0; 
    }
    
    OUTPUT : Sub= 10
             Thanks
    


Welcome to AndroidATPoint.

back image                     next image