Table of Contents
An else-if ladder evaluates multiple conditions in sequence. The first condition that evaluates to a non-zero value determines which block is executed.
#include <stdio.h>
int main()
{
int marks = 72;
if (marks >= 90)
{
printf("Grade A+\n");
}
else if (marks >= 75)
{
printf("Grade A\n");
}
else if (marks >= 60)
{
printf("Grade B\n");
}
else if (marks >= 40)
{
printf("Grade C\n");
}
else
{
printf("Grade F\n");
}
return 0;
}
Example Output #
Grade B
Explanation #
The first condition checks whether marks is at least 90. Since 72 is less than 90, that block is skipped.
The next condition is evaluated:
else if (marks >= 75)
This is also false, so execution continues to the next else if:
else if (marks >= 60)
This condition is true because 72 is greater than or equal to 60. Its block executes and prints Grade B.
Once a condition in the ladder evaluates to non-zero, the remaining conditions are not evaluated.
The final else executes only when none of the preceding conditions is true.