if…else if Ladder in C

if…else if ladder is another extension of if statement supported by C programming language. if statement checks a single condition and if True executes one single statement (simple or compound). if else statement also checks a single condition, however it executes one of the two statements (both of which can be simple or compound). Sometimes, it is required to check multiple conditions and accordingly decide the execution of a specific statement (simple or compound) among multiple possible statements (simple or compound).

One way to achieve this is to have multiple if statements. Consider a simple problem in which depending upon the value of some integer we have to display the day of week. The value can be entered by a user using a keyboard and we have to display the corresponding weekday. The logic of the program is very simple. We have to check whether the value of variable is 1 then display “Monday”, or if 2 then display “Tuesday”, and so on.

#include <stdio.h>
void main()
{
int weekday;
printf(“please enter the weekday in number “);
scanf(“%d”, &weekday);
if (weekday == 1)
puts(“Monday”);
if (weekday == 2)
puts(“Tuesday”);
if (weekday == 3)
puts(“Wednesday”);
if (weekday == 4)
puts(“Thursday”);
if (weekday == 5)
puts(“Friday”);
if (weekday == 6)
puts(“Saturday”);
if (weekday == 7)
puts(“Sunday”);
}

Program 1: Program that uses multiple if statements

Screenshot from 2020-07-16 16-03-22

This program will run fine. However, this program has an efficiency problem. If the user enters value 1 then as soon as “Monday” will be displayed, the next if statement will also checked; in this case weekday == 2. Indeed this condition will fail but it will consume CPU time. The worst part of the scenario is that all following conditions will be tested. To stop as soon checking other condition as soon as some condition is met, we can use if…else if statement. The general syntax of if..else if statement is as follows:

if ( condition1 )
statement1;
else if ( condition2 )
statement2;
.
.
.
else
statementn;

The condition can be any expression that evaluates to True or False. Also, all statements can be a simple statement or compound statement. Furthermore, there is no limit on number of else if constructs that can be used. Also, else construct is not mandatory.

#include <stdio.h>
void main()
{
int weekday;
printf(“please enter the weekday in number “);
scanf(“%d”, &weekday);
if (weekday == 1)
puts(“Monday”);
else if (weekday == 2)
puts(“Tuesday”);
else if (weekday == 3)
puts(“Wednesday”);
else if (weekday == 4)
puts(“Thursday”);
else if (weekday == 5)
puts(“Friday”);
else if (weekday == 6)
puts(“Saturday”);
else if (weekday == 7)
puts(“Sunday”);
}

Program 2: Program that uses else…if statements

Now, if the user enters value 1 then as soon as “Monday” will be displayed, the next and all subsequent else if statements will not be checked and control will come out of the construct. However, if the user enters value 2, then first two conditions will be checked and rest will be skipped. Though there will no difference in the output but this program will run faster as compared to other one. It should be noted that if else is to be used it should be the last statement.