Skip to content

Else – If Ladder

by yashan on October 2nd, 2011

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.

From → C Tutorials

11 Comments
  1. Very nice tutorial

  2. i got one error.what can i do…

  3. mahmud permalink

    nice program

  4. it training permalink

    i got one 2 errors….. number 4 and 30

  5. shalu permalink

    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.

  6. Subhasis Sarkar permalink

    Nice tutorial.

  7. rundy permalink

    humko chodo pls.

  8. sripriya permalink

    i want more information about c programing

  9. dheetchitha permalink

    i want more examples on else if ladder with output

  10. hitesh permalink

    write a program to change c’s background

    please try it

  11. Suji permalink

    I want an another else if prgm with output

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS