The else-if ladder is used when a program needs to check multiple conditions. The conditions are evaluated from top to bottom, and the first true condition is executed.
else block,
if present, is executed.
An else-if ladder allows you to test several conditions one after another. It is useful when there are more than two possible outcomes.
if (condition1)
{
// statement
}
else if (condition2)
{
// statement
}
else if (condition3)
{
// statement
}
else
{
// default statement
}
if (condition1)
{
statement1;
}
else if (condition2)
{
statement2;
}
else
{
statement3;
}
The program first checks condition1. If it is false, it checks
condition2. If all conditions are false, the else
block executes.
int number = 0;
if (number > 0)
{
printf("Positive");
}
else if (number < 0)
{
printf("Negative");
}
else
{
printf("Zero");
}
The program checks three possible cases: positive, negative, and zero.
The execution takes place from top to bottom:
if condition is checked.else if condition is checked.else executes.int marks = 75;
if (marks >= 80)
{
printf("Grade A");
}
else if (marks >= 60)
{
printf("Grade B");
}
else if (marks >= 40)
{
printf("Grade C");
}
else
{
printf("Fail");
}
Since the marks are 75, the second condition is true and
Grade B is displayed.
int marks = 92;
if (marks >= 90)
{
printf("A+");
}
else if (marks >= 80)
{
printf("A");
}
else if (marks >= 70)
{
printf("B");
}
else if (marks >= 60)
{
printf("C");
}
else if (marks >= 40)
{
printf("D");
}
else
{
printf("F");
}
The conditions are checked from the highest range to the lowest range.
int age = 25;
if (age < 13)
{
printf("Child");
}
else if (age < 20)
{
printf("Teenager");
}
else if (age < 60)
{
printf("Adult");
}
else
{
printf("Senior Citizen");
}
Each condition represents a different age group.
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0)
{
printf("Positive");
}
else if (number < 0)
{
printf("Negative");
}
else
{
printf("Zero");
}
This is a common beginner example of an else-if ladder.
int a = 30;
int b = 20;
int c = 10;
if (a > b && a > c)
{
printf("A is largest");
}
else if (b > a && b > c)
{
printf("B is largest");
}
else
{
printf("C is largest or there is a tie");
}
Logical operators can be used inside else-if conditions.
int day = 3;
if (day == 1)
{
printf("Monday");
}
else if (day == 2)
{
printf("Tuesday");
}
else if (day == 3)
{
printf("Wednesday");
}
else
{
printf("Invalid day");
}
Each condition checks a different value.
int marks = 68;
if (marks >= 90)
{
printf("Excellent");
}
else if (marks >= 75)
{
printf("Very Good");
}
else if (marks >= 50)
{
printf("Good");
}
else
{
printf("Needs Improvement");
}
Only the first matching condition is executed.
#include <stdio.h>
int main()
{
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks >= 90)
{
printf("Grade A+");
}
else if (marks >= 80)
{
printf("Grade A");
}
else if (marks >= 60)
{
printf("Grade B");
}
else if (marks >= 40)
{
printf("Grade C");
}
else
{
printf("Fail");
}
return 0;
}
int temperature = 35;
if (temperature >= 40)
{
printf("Very Hot");
}
else if (temperature >= 30)
{
printf("Hot");
}
else if (temperature >= 20)
{
printf("Normal");
}
else
{
printf("Cold");
}
The program classifies the temperature into different ranges.
int salary = 45000;
if (salary >= 100000)
{
printf("High Salary");
}
else if (salary >= 50000)
{
printf("Good Salary");
}
else if (salary >= 25000)
{
printf("Average Salary");
}
else
{
printf("Low Salary");
}
int number = 15;
if (number % 2 == 0)
{
printf("Divisible by 2");
}
else if (number % 3 == 0)
{
printf("Divisible by 3");
}
else if (number % 5 == 0)
{
printf("Divisible by 5");
}
else
{
printf("Not divisible by 2, 3 or 5");
}
Remember that the first true condition stops the ladder.
char grade = 'B';
if (grade == 'A')
{
printf("Excellent");
}
else if (grade == 'B')
{
printf("Very Good");
}
else if (grade == 'C')
{
printf("Good");
}
else
{
printf("Needs Improvement");
}
Character values can be compared using the equality operator.
char operator = '+';
int a = 10;
int b = 5;
if (operator == '+')
{
printf("%d", a + b);
}
else if (operator == '-')
{
printf("%d", a - b);
}
else if (operator == '*')
{
printf("%d", a * b);
}
else if (operator == '/')
{
printf("%d", a / b);
}
else
{
printf("Invalid operator");
}
An else-if ladder can select an operation based on the operator entered.
int age = 17;
if (age < 0)
{
printf("Invalid age");
}
else if (age >= 18)
{
printf("Eligible to vote");
}
else
{
printf("Not eligible to vote");
}
The first condition handles invalid input before checking eligibility.
int cost = 1000;
int selling = 1200;
if (selling > cost)
{
printf("Profit");
}
else if (selling < cost)
{
printf("Loss");
}
else
{
printf("No profit and no loss");
}
Three possible outcomes are handled using the ladder.
float percentage = 82.5f;
if (percentage >= 90.0f)
{
printf("Excellent");
}
else if (percentage >= 75.0f)
{
printf("Very Good");
}
else if (percentage >= 60.0f)
{
printf("Good");
}
else
{
printf("Needs Improvement");
}
Floating-point variables can also be used in an else-if ladder.
int marks = 85;
if (marks >= 40)
{
printf("Pass");
}
else if (marks >= 80)
{
printf("Excellent");
}
Here, marks >= 40 is true first, so the second condition is
never reached.
For range-based grading, conditions should generally be arranged from the highest threshold to the lowest when that matches the intended logic.
int marks = 72;
if (marks >= 90)
{
printf("A+");
}
else if (marks >= 80)
{
printf("A");
}
else if (marks >= 70)
{
printf("B");
}
else if (marks >= 60)
{
printf("C");
}
else
{
printf("Fail");
}
Because the conditions are checked from top to bottom, 72 matches the
marks >= 70 condition.
The final else is optional.
int number = 20;
if (number > 100)
{
printf("Greater than 100");
}
else if (number > 10)
{
printf("Greater than 10");
}
If no condition is true, nothing inside the ladder executes.
Multiple related alternatives are often easier to read with an else-if ladder.
if (marks >= 80)
{
printf("A");
}
else if (marks >= 60)
{
printf("B");
}
else
{
printf("C");
}
Nested if statements are still useful when a decision depends
on another decision.
One common mistake is placing a broad condition before a more specific condition.
if (marks >= 40)
{
printf("Pass");
}
else if (marks >= 90)
{
printf("Excellent");
}
The second condition can never be reached for values 90 or higher because those values already satisfy the first condition.
Arrange conditions carefully according to the required logic.
Incorrect:
if (number = 10)
{
printf("Ten");
}
else if (number = 20)
{
printf("Twenty");
}
The = operator performs assignment. Use == when
you want to compare values.
Correct:
if (number == 10)
{
printf("Ten");
}
else if (number == 20)
{
printf("Twenty");
}
#include <stdio.h>
int main()
{
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks >= 90)
{
printf("Grade A+");
}
else if (marks >= 80)
{
printf("Grade A");
}
else if (marks >= 70)
{
printf("Grade B");
}
else if (marks >= 60)
{
printf("Grade C");
}
else if (marks >= 40)
{
printf("Grade D");
}
else
{
printf("Fail");
}
return 0;
}
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0)
{
printf("Positive");
}
else if (number < 0)
{
printf("Negative");
}
else
{
printf("Zero");
}
return 0;
}
#include <stdio.h>
int main()
{
int a, b;
char op;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &op);
if (op == '+')
{
printf("Result = %d", a + b);
}
else if (op == '-')
{
printf("Result = %d", a - b);
}
else if (op == '*')
{
printf("Result = %d", a * b);
}
else if (op == '/')
{
if (b != 0)
{
printf("Result = %d", a / b);
}
else
{
printf("Cannot divide by zero");
}
}
else
{
printf("Invalid operator");
}
return 0;
}
Practice the following programs:
Always check the conditions in the correct order and remember that the first true condition is executed.
else is optional.else executes when none of the conditions is true.== for comparison and = for assignment.Question: In an else-if ladder, what happens after the first true condition is found?