C if , else if ladder
In C, if else if ladder is an extension of if else statement used to test a series of conditions sequentially, executing the code for the first true condition. A condition is checked only if all previous ones are false. Once a condition is true, its code block executes, and the ladder ends.
Example:
#include <stdio.h>
int main() {
int marks = 85;
// Assign grade based on marks
if (marks >= 90) {
printf("A\n");
} else if (marks >= 80) {
printf("B\n");
} else if (marks >= 70) {
printf("C\n");
} else if (marks >= 60) {
printf("D\n");
} else {
printf("F\n");
}
return 0;
}
Output
B
Explanation: The program checks the value of marks and assigns a grade based on predefined ranges. It checks from highest to lowest, printing the grade based on the conditions met, or "F" if none are met.
Component of if, else if ladder
- if statement: This is the first statement in the if else if ladder. The if statement contains a condition and a code block which is executed if the condition is evaluates to true else the control is passed to the nest statement.
Syntax
if(condition)
{
code to be executed
}
- else if statement: The 'else if' statement always comes after the if statement. An if else if ladder can have multiple else if statements, where each have a condition that is to be tested along with a code block which will be executed if the condition evaluates to true else the control is passed to the nest statement. A particular else if statements is executed only when the conditions mentioned in the 'if' and 'else if' statements before the current 'else if' evaluates to false.
Syntax
if (condition){.....}
else if( condition)
{
code to be executed
}
- else statement: This is the last statement in the if else if ladder and it is optional. The else statement does not contains any condition and only contain a code block which is executed if all the conditions mentioned in the above 'if' and 'else if' statements evaluates to false.
Syntax
if (condition) {....}
else if (condition) {....}
else {
code to be executed.
}
Syntax of Complete if, else if Ladder
if (condition1) {
// Statements 1
}
else if (condition2) {
// Statements 2
}
else {
// Else body
}
where,
- condition1, condition2: Contitions that are to be tested.
- statements 1, statements 2: Code block corresponding to each condition.
- else body: code block to be executed if none of the condition evaluate to true.
Working of if, else if Ladder
The working of if else if ladder can be understood using the following flowchart:

- If Condition 1 evaluates to true, Statement 1 is executed, and the rest of the else if and else conditions are skipped.
- If Condition 1 is false, Condition 2 is evaluated.
- If Condition 2 evaluates to true, Statement 2 is executed, and the else block is skipped.
- If Condition 2 is also false, the Else Body is executed.
- After completing the if-else-if ladder, the statement just below the if are executed.
Examples
The following examples demonstrate the use of if else if ladder in different coding problems:
Check if a number is positive, negative or 0
#include <stdio.h>
int main() {
int n = 10;
printf("Number = %d \n", n);
// Check if the number is positive, negative, or zero
if (n > 0) {
printf("Positive.\n");
} else if (n < 0) {
printf("Negative.\n");
} else {
printf("Zero.\n");
}
return 0;
}
Output
Number = 10 Positive.
Explanation: The if-else if ladder checks whether the number is greater than zero (positive), less than zero (negative), or exactly zero. Based on the result, the corresponding message is printed.
Print the day name using day number
#include <stdio.h>
int main() {
int day = 3;
printf("Day Number: %d\n", day);
// Check the day of the week
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;
}
Output
Day Number: 3 Day Name: Wednesday
Explanation: This program checks the value of day and prints the corresponding day of the week. If the value of day is not between 1 and 7, it prints "Invalid day."