#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Control Flow

if else if statements in C Programming

What is if...else if...else?

This ladder is used when you need to test multiple conditions in sequence, executing the block for the first true condition.

Syntax

if (condition1) {
    // block 1
} else if (condition2) {
    // block 2
} else {
    // default block
}

Example

int marks = 72;
if (marks >= 90) {
    printf("Grade A");
} else if (marks >= 75) {
    printf("Grade B");
} else if (marks >= 60) {
    printf("Grade C");
} else {
    printf("Grade D");
}

Conditions are checked top to bottom, and only the first matching block executes.

Why the Order of Conditions Matters

Because the ladder stops at the first true condition, the order you write the checks in changes the result. Look at this version of the grading example with the conditions reversed:

int marks = 95;
if (marks >= 60) {
    printf("Grade C");      /* runs, because 95 >= 60 is true */
} else if (marks >= 75) {
    printf("Grade B");      /* never reached for 95 */
} else if (marks >= 90) {
    printf("Grade A");      /* never reached at all */
}

A student with 95 gets Grade C. When conditions overlap, put the most specific or most restrictive check first. A good test is to ask, for each branch, "is there any value that can actually reach this line?"

else-if Ladder vs Separate if Statements

Separate if statements are each checked independently, so several of them can run for one value. An else-if ladder guarantees at most one branch runs.

/* separate ifs: for 95, all three lines print */
if (marks >= 90) printf("Distinction\n");
if (marks >= 75) printf("First class\n");
if (marks >= 60) printf("Pass\n");

Use separate if statements when the checks are truly independent, such as validating several fields of a form. Use a ladder when the outcomes are mutually exclusive, like grades or tax slabs.

A Complete Program With Input Validation

Real programs should handle invalid input before the main ladder. Putting the error check first keeps the rest of the logic simple.

#include <stdio.h>

int main(void) {
    int marks;
    printf("Enter marks (0-100): ");
    if (scanf("%d", &marks) != 1) {
        printf("Please enter a number.\n");
        return 1;
    }

    if (marks < 0 || marks > 100) {
        printf("Marks must be between 0 and 100.\n");
    } else if (marks >= 90) {
        printf("Grade A\n");
    } else if (marks >= 75) {
        printf("Grade B\n");
    } else if (marks >= 60) {
        printf("Grade C\n");
    } else {
        printf("Grade D\n");
    }
    return 0;
}

When switch Is a Better Fit

If every branch compares the same variable against fixed integer or character values, such as a menu choice of 1, 2, 3 or 4, a switch statement is usually cleaner. For ranges like marks >= 75, stick with if-else-if, because standard C switch cases only match exact constant values.

Bugs That Show Up Often

  • A stray semicolon: if (marks >= 90); ends the if statement right there, so the following block always runs.
  • Missing braces: without { }, only the first statement belongs to the branch. Adding a second line later silently changes behaviour.
  • Assignment in place of comparison: if (marks = 100) sets marks to 100 and is always true.
  • Chained comparisons: if (60 <= marks <= 75) does not mean what it looks like in C. Write marks >= 60 && marks <= 75.
  • Comparing floats with ==: rounding makes exact equality unreliable; compare against a small tolerance instead.

Takeaway: order checks from most to least specific, always use braces, and end the ladder with an else that handles anything you did not expect.

Ready to master real-world C Programming development?

Learn C Programming hands-on with mentor-led, live sessions.

Explore Course