Else If Ladder With Example - C Programming Tutorials

Explain Else If Ladder With Example And Explain
\\ If you have any query or questions on my post, then please comment below //

What Is Else If Ladder: 
If we are having different - different test conditions with different - different statements, then for these kind of programming we need else if leader. Else if the leader is not interdependent to any other statements or any other test conditions. 

Syntax Of Else If Leader:
---------------------------------------------------------------------
 if(test_condition1)
    {
       statement 1;
     }
 else if(test_condition2)
    {
       statement 2;
     }   
else if(test_condition3)
    {
       statement 3;
     }
 else if(test_condition4)
    {
       statement 4;
     }
 --------------------------------
-------------------------------- // Test Condition And Codes As Per Requirement.
--------------------------------
  else                                           // at last, we use only else.
       { 
          statement x;
       }
------------------------------------------------------------------------------

You see in above syntax there is 4 test conditions with 4 different - different statements. Now I will show you one example:
Program: Write a program to enter the temperature and print the following message according to the given temperature by using if else ladder statement.
1. T<=0                        "Its very very cold".
2. 0 < T < 0                  "Its cold".
3. 10 < T < =20            "Its cool out".
4.  20 < T < =30           "Its warm".
5.  T>30                        "Its hot".
Solution:
-------------------------------------------------------------------------------
#inlcude<stdio.h>
#include<conio.h>
void main()
{
  float temp;
  clrscr();
  printf("Enter The Temperature");
  scanf("%f",&T);
  if(T<=0)
     {
      printf("Its very very cold");
      }
  else if(T>0 && T<=10)
           {
            printf("Its cold");
           }
   else if(T>10 && T < =20)
                  {
                   printf("Its cool out");
                  }
   else if(T<=30 && T>20)
              {
                  printf("Its warm");
              }
   else
             { 
                  printf("Its hot");
             }
getch();
     }
--------------------------------------------------------------------------
You see in the above program, we already did in nested if else (Click Here To View), Now we did in else if ladder. Here you see here five test conditions are present with different - different statements and all are not interdependent to any other statement. Here all the test conditions are checked individually. This is the difference between nested if else statement and else if ladder statement.
You you want to know the difference between both the statements, then you must go through with program, then you can understand these things better.  

If you have any query or if you found any error on given program then please comment below.

(Learn C Programming Online With Our Tutorials Site, Please Subscribe Us To Get Our Latest Updates In Your Mail: Kindly Enter Your Email Id Below )
Click Here To Know How To Download Above Video Tutorials Or YouTube Videos Free


 Enter Your Email Id To Get Our Programming, Web Development, Video Tutorial, Job Updates In Mail :

Labels: ,