C if , else if ladder (original) (raw)

Last Updated : 18 Oct, 2025

In C, if else if ladder is an extension of if else statement.

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

}

`

**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.

**Syntax of Complete if, else if Ladder

C `

if (condition1) { // Statements 1 } else if (condition2) { // Statements 2 } else { // Else body }

`

where,

**Working of if, else if Ladder

The working of if else if ladder can be understood using the following flowchart:

if-else-if-ladder-flow-chart

**Check if a number is positive, negative or 0

C `

#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.

C `

#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."