if...else if Ladder in C



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
} else if (condition2) {
   // Code to be executed if condition1 is false and condition2 is true
} else if (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>

int main() {
   int items = 72;  
  
   if (items >= 90) {
      printf("Store is full");
   } else if (items >= 80) {
      printf("Need few more items");
   } else if (items >= 70) {
      printf("Store is partially full");
   } else if (items >= 50) {
      printf("Sore is half full");
   } else {
      printf("Store is empty");
   }

   return 0;
}

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 −

Working of if…else if Ladder

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>

int main() {
   int day = 5;
   printf("Day Number: %d\n", day);
   if (day == 1) {
      printf("Day Name: Monday\n");
   } else if (day == 2) {
      printf("Day Name: Tuesday\n");
   } else if (day == 3) {
      printf("Day Name: Wednesday\n");
   } else if (day == 4) {
      printf("Day Name: Thursday\n");
   } else if (day == 5) {
      printf("Day Name: Friday\n");
   } else if (day == 6) {
      printf("Day Name: Saturday\n");
   } else if (day == 7) {
      printf("Day Name: Sunday\n");
   } else {
      printf("Invalid day Number\n");
   }

   return 0;
}

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;
}

int main() {
   int num = 5;

   // if...else if ladder
   if (num % 2 == 0 && isPrime(num)) {
      printf("%d is Even and Prime.\n", num);
   } else if (num % 2 != 0 && isPrime(num)) {
      printf("%d is Odd and Prime.\n", num);
   } else if (num % 2 == 0) {
      printf("%d is Even.\n", num);
   } else if (num % 2 != 0) {
      printf("%d is Odd.\n", num);
   } else {
      printf("Invalid input.\n");
   }

   return 0;
}

When you run this code, it will produce the following output −

5 is Odd and Prime.
Advertisements