If-Else & Decision Making in C

    Atul Kabra3 min readUpdated

    The if statement in C lets your program make decisions: it runs a block of code only when a condition is true. Add else to handle the false case, and chain else if to test several conditions in order. This is how a program chooses one path over another.

    The basic if

    if (condition) {
        /* runs only when condition is non-zero (true) */
    }
    

    In C, any non-zero value is "true" and zero is "false". So if (5) always runs, and if (0) never does.

    #include <stdio.h>
    
    int main(void) {
        int marks = 72;
    
        if (marks >= 40) {
            printf("Pass\n");   /* runs because 72 >= 40 is true */
        }
        return 0;
    }
    

    Adding else

    else gives the alternative when the condition is false:

    int age = 15;
    
    if (age >= 18) {
        printf("Adult\n");
    } else {
        printf("Minor\n");   /* this runs */
    }
    

    The else-if ladder

    To check several mutually exclusive conditions, chain them. C tests each in order and stops at the first match:

    #include <stdio.h>
    
    int main(void) {
        int score = 85;
    
        if (score >= 90) {
            printf("Grade A\n");
        } else if (score >= 75) {
            printf("Grade B\n");   /* first true branch -> runs, rest skipped */
        } else if (score >= 50) {
            printf("Grade C\n");
        } else {
            printf("Fail\n");
        }
        return 0;
    }
    

    Once a branch matches, the remaining else if and else blocks are skipped entirely.

    Want to learn this properly?

    Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.

    Explore C Programming

    Nested if

    You can put an if inside another if when one decision depends on another:

    int loggedIn = 1;
    int isAdmin = 0;
    
    if (loggedIn) {
        if (isAdmin) {
            printf("Admin panel\n");
        } else {
            printf("User dashboard\n");
        }
    }
    

    Keep nesting shallow — deep nesting is hard to read. Often logical operators like && let you flatten it.

    The ternary shortcut

    For a simple either/or assignment, the ternary operator ?: is compact:

    int a = 7, b = 3;
    int max = (a > b) ? a : b;   /* if a>b pick a, else pick b -> 7 */
    printf("Max: %d\n", max);
    

    Read it as: condition ? value-if-true : value-if-false. Use it only for short, simple choices; for anything with multiple statements, a full if-else is clearer.

    How C decides true and false

    There is one idea worth fixing firmly: in C there is no separate boolean type in the classic sense — zero is false, and any non-zero value is true. That is why if (count) runs whenever count is not zero, and if (!ptr) runs when a pointer is NULL (zero). Comparisons like a > b produce exactly 1 for true and 0 for false. Knowing this lets you write concise, idiomatic conditions — but it is also why the = versus == mistake is so dangerous: an assignment evaluates to the value assigned, which C then tests for truthiness.

    Common mistakes

    • = instead of ==: if (x = 5) assigns 5 and is always true. Use == to compare.
    • Stray semicolon: if (x > 0); ends the statement immediately, so the block below always runs. Don't put ; right after the condition.
    • Missing braces with multiple statements: without { }, only the first line belongs to the if. Always use braces.
    • Comparing floats with ==: rounding makes this unreliable; compare a difference against a small tolerance instead.
    • Forgetting else if is ordered: put the most specific or highest condition first, or a broad earlier branch will swallow the cases you wanted later.

    FAQ

    Do I always need else? No. Use if alone when there's nothing to do in the false case.

    Is if-else or switch better? For many comparisons of one variable against constant values, a switch-case is often cleaner. For ranges and complex conditions, use if-else.

    Continue at the hub C Programming, or read Operators in C and Switch-Case in C.

    Want to learn this properly? Join the waitlist for our C Programming course — taught in Jalgaon.

    Want to learn this properly?

    Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.

    Explore C Programming
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides