Open In App

C if else if ladder

Last Updated : 02 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

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

Syntax of if else if Ladder

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

where,

  • condition1, condition2: Contitions to be tested.
  • statements 1, statements 2: Code block corresponding to each condition.

An if-else ladder can exclude else block.

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

Flowchart of if else if ladder


  • 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 of if else if Ladder

The following examples demonstrate the use of if else if ladder in different coding problems:

Check whether a number is positive, negative or 0

C
#include <stdio.h>

int main() {
    int n = 10;

    // 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
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

C
#include <stdio.h>

int main() {
    int day = 3;

    // Check the day of the week
    if (day == 1) {
        printf("Monday\n");
    } else if (day == 2) {
        printf("Tuesday\n");
    } else if (day == 3) {
        printf("Wednesday\n");
    } else if (day == 4) {
        printf("Thursday\n");
    } else if (day == 5) {
        printf("Friday\n");
    } else if (day == 6) {
        printf("Saturday\n");
    } else if (day == 7) {
        printf("Sunday\n");
    } else {
        printf("Invalid day\n");
    }

    return 0;
}

Output
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.”



Next Article
Article Tags :

Similar Reads