Else – If Ladder

In C programming language the else if ladder is a way of putting multiple ifs together when multipath decisions are involved. It is a one of the types of decision making and branching statements. A multipath decision is a chain of if’s in which the statement associated with each else is an if. The general form of else if ladder is as follows –

if ( condition 1)
{
        statement - 1;
}
        else if (condtion 2)
        {
                statement - 2;
        }
                else if ( condition n)
                {
                        statement - n;
                }
                       else
                        {
                                default statment;
                        }
statement-x;

This construct is known as the else if ladder.The conditions are evaluated from the top of the ladder to downwards. As soon as a true condition is found, the statement associated with it is executed and the control is transferred to the statement-x (skipping the rest of the ladder). When all the n conditions become false, then the final esle containing the default statement will be executed.

Below is the sample C program of the if – else ladder statement in which the color is to be selected by using the if – else ladder –

#include<stdio.h>
#include<string.h>
void main()
{
     int n;
     
     printf(" Enter 1 to 4 to select random color");
     scanf("%d",&n);
     
     if(n==1)
     {
             printf("You selected Red color");
     }
          else if(n==2)
          {
               printf("You selected Green color");
          }
               else if(n==3)
               {
                    printf("You selected yellow color");
               }
                    else if(n==4)
                    {
                         printf("You selected Blue color");
                    }
                         else
                         {
                             printf("No color selected");
                         }
          getch();
}

Note – C programming language is a structured language; so it will be better to form blocks in ‘if else ladder’ as this makes the programmer to understand the language better and reduces the confusion and chaos. Also it will make the others easy to understand your program. It is also necessary to align the opening and the closing braces of the block.

yashan has written 69 articles

17 thoughts on “Else – If Ladder

  1. shalu says:

    write a program to enter total sales you will calculate discount as per given condition.
    sales discount
    2000 && 40000 && 60000 && 80000 10%
    you will print total sales ,discount amount, pay bill amount.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>