c
C Programming Tutorial : Introduction

C Conditional Statements - if-else Ladder

The if-else ladder allows you to check between multiple test expressions and execute different statements. This is also a type of nesting in which, there is an ‘if-else’ statement in every else part except the last else part.

This if-else statement executes two different codes depending upon, whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.

Syntax

if (condition1)
    statement A;
    else if (condition2)
        statement B;
    else if (condition 3)
        statement C;
    else
        statement D;
 

This nested structure is generally written in compact form as in second figure. The flow chart for this structure is: –

if (condition1) statement A; else if (condition2) statement B; else if (condition 3) statement C; else statement D flow chart

Here each condition is checked, and when a condition is found to be true, the corresponding statements are executed, and the control comes out of the nested structure without checking remaining conditions. If none of the conditions is true then the last else part will be executed.

Example-1

/ * P5. 6 Program to find out the grade of a student when the marks of 4 subjects are given. */
#include<stdio.h>
#include<conio.h>

void main ( )
{
    float ml, m2, ni3, m4, total, per;
    char grade;
    printf("Enter marks of 4 subjects ");
    scanf(“%f%f%f%f”, &m1,&m2,&m3,&m4) ;
    total=ml+m2+m3+m4;
    per=total/4;
    if (per>=85)
        grade= 'A’;
    else if(per>=70)
        grade='B';
    else if (per>=55)
        grade= 'C’;
    else if (per>=40)
        grade= 'D’;
    else
        grade='E';
        printf ("Percentage is %f\n Grade is %c\n", per, grade);
    getch();
}
 

If we don’t use the else if ladder, the equivalent code for this problem would be

Example-2

if(per>=85)
    grade=' A’;
    if(per<85&&per>=70)
        grade='B’;
    if(per<70&&per>=S5)
        grade=' C’;
    if(per<55&&per>=40).
        grade= 'D’;
    if(per<40)
        grade='E’;
 

In if-else ladder whenever a condition is found true other conditions are not checked, while in this case all the conditions will always be checked wasting a lot of time, and moreover the conditions here are lengthier.

Example-1

/*Write a C Program which demonstrate use of if-else-if ladder statement.*/
 #include<stdio.h>
 #include<conio.h> 
void main() 
{    
 int a;   
  printf("Enter a Number: ");  
   scanf("%d",&a);   
  if(a > 0)  
   {     
    printf("Given Number is Positive");  
   }     else if(a == 0)  
   {       
  printf("Given Number is Zero");   
  }     
else if(a < 0)   
  {    
     printf("Given Number is Negative"); 
    }  
   getch();
 }
 

Example-2

/* C program to illustrate nested-if statement */
  
#include <stdio.h> 
#include <conio.h> 

int main() 
{ 
    int i = 25; 
   
    if (i >= 0 && i <= 10) 
        printf("i is between 0 and 10"); 
      else if (i >= 11 && i <= 15) 
        printf("i is between 11 and 15"); 
      else if (i >= 16 && i <= 20) 
        printf("i is between 16 and 20"); 
       else
        printf("i is greater than 20"); 
}