The if-else-if ladder in C is an extension of the simple if-else statement that is used to test multiple conditions sequentially. It executes a block of code associated with the first condition that evaluates to true.
Syntax of if…else if Ladder in C
The syntax of if…else if ladder in C is as follows −
if(condition1){// Code to be executed if condition1 is true}elseif(condition2){// Code to be executed if condition1 is false and condition2 is true}elseif(condition3){// Code to be executed if all previous are false, and condition3 is true}// ...else{// Code to be executed if all preceding conditions are false}
In an if…else if ladder, each condition is evaluated sequentially, but only if all the previous conditions are false. As soon as a condition evaluates to true, its corresponding code block executes, and the ladder terminates without checking the remaining conditions.
Example
Let’s understand the if…else if ladder in C with an example to see how the code executes and how the flow runs step by step.
#include <stdio.h>intmain(){int items =72;if(items >=90){printf("Store is full");}elseif(items >=80){printf("Need few more items");}elseif(items >=70){printf("Store is partially full");}elseif(items >=50){printf("Sore is half full");}else{printf("Store is empty");}return0;}
When you run this code, it will produce the following output −
Store is partially full
Explanation − In this example, there are 72 items. The condition “items >= 90” evaluates to false, so the control moves to “items >= 80”, which is also false.
Next, it checks the condition “items >= 70”, which evaluates to true. Therefore, the statement “Store is partially full” is printed, and the remaining conditions in the ladder are skipped.
Components of if…else if Ladder
Let’s look at the components of an if…else if ladder in C programming −
If statement
The if statement first statement in the if…else if ladder. The if block contains a condition and a code block, which executes if the condition evaluates to true; otherwise, the control passes to the next statement.
else if statement
The else-if statement always comes after the if statement. An if…else if ladder can have multiple else if statements, each containing a condition to be tested. If a condition evaluates to true, its corresponding code block executes; otherwise, the control moves to the next statement.
else statement
The else statement is the last statement in an ifâ¦else if ladder. The else statement does not contain any condition, however it contains a code block which is executed if all the conditions mentioned in the above if and else if statements evaluate to false.
Working of if…else if Ladder
The working of the if…else if ladder in C can be understood using the following flowchart −

This is how the control flow moves −
- If condition 1 is true, Statement 1 executes, and the remaining else-if and else conditions are skipped.
- If condition 1 is false, condition 2 is evaluated. If condition 2 is true, Statement 2 executes, and the else block is skipped.
- If condition 2 is also false, the else block executes.
- After the if…else if ladder finishes, the statement immediately following the ladder executes.
Example 1
In this C example, we have used an ifâ¦else if ladder to display the day names based on their designated numbers −
#include <stdio.h>intmain(){int day =5;printf("Day Number: %d\n", day);if(day ==1){printf("Day Name: Monday\n");}elseif(day ==2){printf("Day Name: Tuesday\n");}elseif(day ==3){printf("Day Name: Wednesday\n");}elseif(day ==4){printf("Day Name: Thursday\n");}elseif(day ==5){printf("Day Name: Friday\n");}elseif(day ==6){printf("Day Name: Saturday\n");}elseif(day ==7){printf("Day Name: Sunday\n");}else{printf("Invalid day Number\n");}return0;}
Run this code and check its output. When you enter the number “5”, the program will print “Friday” on the console −
Day Number: 5
Day Name: Friday
Example 2
The following C Program uses an if…else if ladder to check whether a given number is even, odd, even & prime, or odd & prime −
#include <stdio.h>#include <stdbool.h>// Function to check prime number
bool isPrime(int num){if(num <=1){return false;}for(int i =2; i * i <= num; i++){if(num % i ==0){return false;}}return true;}intmain(){int num =5;// if...else if ladderif(num %2==0&&isPrime(num)){printf("%d is Even and Prime.\n", num);}elseif(num %2!=0&&isPrime(num)){printf("%d is Odd and Prime.\n", num);}elseif(num %2==0){printf("%d is Even.\n", num);}elseif(num %2!=0){printf("%d is Odd.\n", num);}else{printf("Invalid input.\n");}return0;}
When you run this code, it will produce the following output −
5 is Odd and Prime.
Leave a Reply