C If…Else

if...else statements let a C program make decisions. Instead of running every statement from top to bottom in exactly the same way every time, the program evaluates a condition and chooses which block of code should run.

This is the foundation of control flow: validating input, checking limits, choosing messages, handling errors, and building almost every useful program. To use it well, you need to understand both the syntax and C’s rule for truth values.

Overview: How It Works

An if statement starts with a condition inside parentheses. C evaluates that condition as an expression. If the result compares as true, C executes the statement or block that follows the if. If the result is false, that block is skipped.

In C, conditions are not limited to a special boolean type. The rule is simple and important: 0 is false, and any nonzero value is true. A comparison such as score >= 70 produces 1 when true and 0 when false. Logical expressions with &&, ||, and ! also produce truth values.

An else block gives the program an alternate path when the if condition is false. An else if branch lets you test another condition only if the earlier condition failed. In an if, else if, else chain, at most one branch runs. C checks the chain from top to bottom, runs the first branch whose condition is true, then skips the rest.

Internally, the compiled program contains branch instructions. The CPU evaluates the condition, then jumps to the matching section of code. Your variables are still stored normally in memory; an if statement does not create a new kind of value. It only controls which statements are executed.

Syntax

The full pattern has an if, zero or more else if branches, and an optional final else. This complete program demonstrates the shape:

#include <stdio.h>

int main(void)
{
    int value = 12;

    if (value > 20) {
        printf("large\n");
    } else if (value >= 10) {
        printf("medium\n");
    } else {
        printf("small\n");
    }

    return 0;
}

Output:

medium
Part Meaning
if (condition) Starts a decision. The following block runs only when the condition is true.
else if (condition) Tests another condition only when all earlier conditions in the chain were false.
else Provides a default block. It has no condition because it means none of the earlier tests matched.
{ ... } Groups multiple statements into one block belonging to that branch.
==, <, >= Comparison operators commonly used to form conditions.

Examples

Example 1: A simple if statement

#include <stdio.h>

int main(void)
{
    int temperature = 31;

    if (temperature > 30) {
        printf("It is hot today.\n");
    }

    printf("Check complete.\n");

    return 0;
}

Output:

It is hot today.
Check complete.

The condition temperature > 30 is true, so the first message is printed. The second printf is outside the if block, so it runs no matter what the temperature is.

Example 2: Choosing between two paths

#include <stdio.h>

int main(void)
{
    int items_in_stock = 0;

    if (items_in_stock > 0) {
        printf("Order accepted.\n");
    } else {
        printf("Out of stock.\n");
    }

    return 0;
}

Output:

Out of stock.

Only one branch runs. Because items_in_stock > 0 is false, C skips the if block and runs the else block. This pattern is useful whenever there are exactly two outcomes.

Example 3: Multiple conditions with else if

#include <stdio.h>

int main(void)
{
    int score = 86;

    if (score >= 90) {
        printf("Grade: A\n");
    } else if (score >= 80) {
        printf("Grade: B\n");
    } else if (score >= 70) {
        printf("Grade: C\n");
    } else if (score >= 60) {
        printf("Grade: D\n");
    } else {
        printf("Grade: F\n");
    }

    return 0;
}

Output:

Grade: B

The order matters. The first condition is false because 86 is not at least 90. The second condition is true, so C prints Grade: B and does not test the later branches.

Example 4: Combining conditions

#include <stdio.h>

int main(void)
{
    int age = 20;
    int has_ticket = 1;
    int is_banned = 0;

    if (age >= 18 && has_ticket && !is_banned) {
        printf("You may enter.\n");
    } else {
        printf("Entry denied.\n");
    }

    return 0;
}

Output:

You may enter.

The && operator means all parts must be true. The variable has_ticket is 1, which is true in C. The expression !is_banned is also true because is_banned is 0.

How It Works Step By Step

Consider the grade example. First, C stores 86 in score. Then it evaluates score >= 90. That comparison is false, so the program jumps past the first block.

Next, C evaluates score >= 80. This comparison is true, so the program enters that block and calls printf. After the block finishes, C jumps to the end of the whole chain. It does not continue checking whether 86 is also greater than or equal to 70.

Logical operators add one more detail: short-circuit evaluation. With &&, if the left side is false, C does not evaluate the right side because the whole expression must be false. With ||, if the left side is true, C does not evaluate the right side because the whole expression must be true. This is why checks are often ordered from safest to most specific.

Common Mistakes

Using assignment instead of comparison

The expression if (score = 100) assigns 100 to score. Since 100 is nonzero, the condition is true. To compare values, use ==, as in this corrected program:

#include <stdio.h>

int main(void)
{
    int score = 100;

    if (score == 100) {
        printf("Perfect score.\n");
    }

    return 0;
}

Output:

Perfect score.

Putting a semicolon after the condition

Writing if (is_ready); ends the if statement immediately. The block that follows is no longer controlled by the condition. Put semicolons after statements inside the block, not after the if condition.

Leaving out braces

C allows a branch to control one statement without braces, but this often causes bugs when a second statement is added later. Prefer braces even for one-line branches so the controlled block is always obvious.

Ordering conditions incorrectly

If a grade chain checks score >= 60 before score >= 90, a score of 95 matches the first condition and the later A branch is never reached. Put the most specific or highest-priority tests first.

Best Practices

  • Use braces for every if, else if, and else block.
  • Keep each condition readable. If it becomes long, store part of it in a well-named variable.
  • Use == for equality comparisons and = only for assignment.
  • Order else if branches so the first true match is the correct match.
  • Use an else block when there is a real default case or when all possible inputs should be handled.
  • Remember C’s truth rule: 0 is false; any nonzero value is true.
  • Take advantage of short-circuiting for protective checks, such as testing that a value is valid before doing a dependent calculation.

Practice Exercises

  1. Write a program with an int number. Print whether it is positive, negative, or zero using if, else if, and else.
  2. Create a program with int hour. Print morning for hours before 12, afternoon for hours from 12 through 17, and evening otherwise.
  3. Write an admission check using age, has_ticket, and is_banned. Use logical operators to print whether entry is allowed.

Summary

  • if runs a block only when its condition is true.
  • else runs when the matching if condition is false.
  • else if lets you test several conditions in order.
  • In C conditions, 0 is false and any nonzero value is true.
  • Only the first true branch in an if, else if, else chain runs.
  • Braces, clear ordering, and careful use of == prevent many common bugs.