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.

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

if / else / else if

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

#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;
}

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 .