if-else if statement / if-else ladder in c programming

if-else if statement / if-else ladder in c programming

 

if-else if statement/ if-else ladder in c programming

When more than two conditions are to be then if - else if statement is used. It is also known as multipath conditional statement/ multipath conditional statement/ if-else ladder. In this statement first if statement is evaluated and if it is a true statement along it will be executed otherwise next if statement is evaluated. If it is true along it will execute otherwise next if statement is evaluated. If any of the if statement is not executed then statement along else will be executed.

Syntax

         if(condition 1)

             {

                   statement 1;

               }

           else if(condition 2)

                {

                       statement 2;

                }

              else if(condition 3)

                {

                       statement 3;

                . 

                .

                .

                }

              else

                {

                    statement n;

                  }

In this statement, conditions are checked from the ladder to downwards. As soon as the true condition is found, the statements associated with it are exected and the control is transferred to outside of the ladder skipping the remaining part of the ladder. When most of the conditions (condition 1-condition n-1) are evaluated false then statements associated with else part will be executed.

Flow chart of if-else if statement

Flow chart of if else ladder
Example
C program to find the smallest number among three numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
printf("\n Enter three digit");
scanf("%d%d%d", &a, &b, &c);
if((a<b)&&(b<c))
{
printf("%d is smallest number", a);
}
else if(b<c)
{
printf("%d is smallest number", b);
}
else
{
printf("%d is smallest number", c);
}
getch();
}
Output
Output of if else ladder


In the above program
first if((a<b)&&(b<c)) condition is check if it is true then printf("%d is smallest number", a); executed and display value of a is smallest number like output 1 . When the condition doesn't match then else if(b<c) is checked if it is true then printf("%d is smallest number", b); display the value of b is the smallest number like output 1. The condition else if(b<c) is evaluated because of when the first condition becomes false and we use else if condition it alters the first condition and becomes b is smallest than a and b is greater than a. To show b is the smallest number must be b is smallest than c (b<c). If non of the above condition satisfied then else part printf("%d is smallest number", c); print c is smallest number like output 3.

For the C program to print largest number among three click here

Let see another example
Write a C program to print middle numbers among three number using else if ladder
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
printf("\n Enter three digit");
scanf("%d%d%d", &a, &b, &c);
if((a>b)&&(a<c)||(a<b)&&(a>c))
{
printf("%d is middle number", a);
}
else if((b<a)&&(b>c)||(b>a)&&(b<c))
{
printf("%d is middle number", b);
}
else
{
printf("%d is middle number", c);
}
getch();
}
Output

Post a Comment

0 Comments