Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C Language Dictionary

  1. Home
  2. C Language Dictionary
  3. if / else / else if

if / else / else if

Since: C89(1989)

A mechanism for branching execution based on a condition. if executes when the condition is true, else handles the false case, and else if evaluates multiple conditions in sequence.

Syntax

// Executes when the condition is true.
if (condition) {
    statement;
}

// Branches execution based on whether the condition is true or false.
if (condition) {
    statement1;
} else {
    statement2;
}

// Evaluates multiple conditions in sequence.
if (condition1) {
    statement1;
} else if (condition2) {
    statement2;
} else {
    statement3;
}

// The conditional (ternary) operator provides a concise alternative.
variable = (condition) ? value_if_true : value_if_false;

Comparison and Logical Operators

OperatorMeaningDescription
==Equal toTrue when the left-hand side equals the right-hand side.
!=Not equal toTrue when the left-hand side differs from the right-hand side.
<Less thanTrue when the left-hand side is less than the right-hand side.
>Greater thanTrue when the left-hand side is greater than the right-hand side.
<=Less than or equal toTrue when the left-hand side is less than or equal to the right-hand side.
>=Greater than or equal toTrue when the left-hand side is greater than or equal to the right-hand side.
&&ANDTrue when both conditions are true.
||ORTrue when at least one condition is true.
!NOTInverts the condition — true becomes false, and false becomes true.

Sample Code

sample_if_else.c
#include <stdio.h>

int main(void) {
    int score = 75;

    // Basic example using if / else if / else.
    if (score >= 90) {
        printf("Grade: A\n");
    } else if (score >= 70) {
        printf("Grade: B\n"); // "Grade: B" is printed.
    } else if (score >= 60) {
        printf("Grade: C\n");
    } else {
        printf("Grade: F\n");
    }

    // Using logical operators to combine conditions.
    int age = 20;
    int has_id = 1; // 1 is true, 0 is false.
    if (age >= 18 && has_id) {
        printf("Entry allowed.\n");
    }

    // Using the ternary operator for a concise expression.
    int n = -5;
    int abs_n = (n >= 0) ? n : -n;
    printf("Absolute value: %d\n", abs_n); // "Absolute value: 5" is printed.

    // In C, 0 is false and any non-zero value is true.
    int flag = 0;
    if (!flag) {
        printf("The flag is not set.\n");
    }

    // Character comparisons are performed using integer values.
    char ch = 'A';
    if (ch >= 'A' && ch <= 'Z') {
        printf("It is an uppercase letter.\n");
    }

    return 0;
}

Run the following command:

gcc if_else.c -o if_else
./if_else
Grade: B
Entry allowed.
Absolute value: 5
The flag is not set.
It is an uppercase letter.

Common Mistakes

Common Mistake: Confusing == and =

Writing = instead of == in a condition performs an assignment rather than a comparison. Because an assignment expression evaluates to the assigned value, an unintended truth evaluation occurs. This does not cause a compile error, so extra care is needed.

if_assign_ng.c
#include <stdio.h>

int main(void) {
    int flag = 0;

    /* NG: = is assignment. Assigning 1 to flag (result = 1 = true) always executes. */
    if (flag = 1) {
        printf("This always executes.\n");
    }

    return 0;
}

Run the following command:

gcc -Wall if_assign_ng.c -o if_assign_ng
./if_assign_ng
This always executes.
if_assign_ok.c
#include <stdio.h>

int main(void) {
    int flag = 0;

    /* OK: use == for comparison */
    if (flag == 1) {
        printf("Executes only when flag is 1.\n");
    } else {
        printf("flag is not 1.\n");
    }

    return 0;
}

Run the following command:

gcc if_assign_ok.c -o if_assign_ok
./if_assign_ok
flag is not 1.

Notes

C does not have a dedicated bool type (though C99 provides one via stdbool.h). The integer value 0 is treated as false; any other value is treated as true. Writing = instead of == inside a condition is an assignment, not a comparison, but it will not cause a compile error. To guard against this mistake, some programmers write the constant on the left-hand side (e.g., 1 == flag).

When a block contains only one statement, the braces can be omitted, but it is recommended to always include them to prevent bugs when adding more statements later.

For multi-way branching, switch / case can lead to cleaner code. For condition-based loops, see for / while.

If you find any errors or copyright issues, please .