Code of the Day
BeginnerControl flow

if, else, and else if

Write conditional logic in C using if, else, and else if — including comparison operators, logical operators, and common patterns.

CBeginner10 min read
By the end of this lesson you will be able to:
  • Write an if statement and an if/else statement in C
  • Use comparison and logical operators to form conditions
  • Chain multiple conditions with else if
  • Explain what constitutes a "truthy" value in C

Every useful program makes decisions. A compiler decides whether your code is syntactically valid. A web server decides whether to serve a file or return an error. A game decides whether a bullet hit a target. In C, decisions are expressed with if, else if, and else.

The if statement

The simplest form:

if (condition) {
    /* executed if condition is non-zero (true) */
}

A concrete example:

#include <stdio.h>

int main(void) {
    int score = 85;

    if (score >= 60) {
        printf("Passing grade\n");
    }

    return 0;
}

If score is 85, the condition score >= 60 evaluates to 1 (true), and the body executes. If score were 50, the condition evaluates to 0 (false), and the body is skipped.

In C, truth is numeric. There is no separate bool type in classic C — any non-zero integer is "true" and zero is "false." The standard headers <stdbool.h> (C99 onwards) define bool, true, and false as aliases for _Bool, 1, and 0, respectively. It is good practice to include it.

if / else

int score = 45;

if (score >= 60) {
    printf("Pass\n");
} else {
    printf("Fail\n");
}

The else branch runs when the if condition is false.

Comparison operators

OperatorMeaning
==Equal to
!=Not equal to
<Less than
>Greater than
<=Less than or equal
>=Greater than or equal

Each produces 1 if the comparison holds, 0 if not.

= is assignment; == is comparison. Writing if (x = 5) instead of if (x == 5) assigns 5 to x and then tests whether the result (5) is non-zero — which is always true. This bug compiles without error by default. With -Wall, gcc warns about it. Some programmers write 5 == x (a "Yoda condition") to make this mistake a compile error — the compiler will reject 5 = x. Either style works; consistency matters more than the choice.

else if chains

#include <stdio.h>

int main(void) {
    int score = 78;
    char grade;

    if (score >= 90) {
        grade = 'A';
    } else if (score >= 80) {
        grade = 'B';
    } else if (score >= 70) {
        grade = 'C';
    } else if (score >= 60) {
        grade = 'D';
    } else {
        grade = 'F';
    }

    printf("Score %d: grade %c\n", score, grade);
    return 0;
}

C evaluates conditions top-to-bottom and executes the first branch whose condition is true. Once a branch executes, the rest are skipped.

Logical operators

Combine conditions with:

OperatorMeaningExample
&&AND — both must be truex > 0 && x < 10
||OR — at least one must be truex < 0 || x > 100
!NOT — inverts the condition!is_valid
int age = 25;
int has_license = 1;

if (age >= 16 && has_license) {
    printf("Can drive\n");
}

&& and || use : for &&, if the left side is false, the right side is never evaluated. For ||, if the left side is true, the right side is never evaluated. This matters when the right side has a side effect (like calling a function).

Braces and the single-statement shortcut

C allows omitting braces when an if body is a single statement:

if (x > 0)
    printf("positive\n");

Avoid this. It looks clean but causes bugs when you later add a second line and forget to add braces:

if (x > 0)
    printf("positive\n");
    printf("value: %d\n", x);  /* BUG: always executes */

The second printf is not part of the if — indentation does not matter to C. Always use braces.

Nested conditionals

int x = 5, y = 10;

if (x > 0) {
    if (y > 0) {
        printf("Both positive\n");
    } else {
        printf("x positive, y non-positive\n");
    }
} else {
    printf("x non-positive\n");
}

Nested if statements work correctly but deep nesting is hard to read. When you find yourself three or four levels deep, consider refactoring — either extracting a function or restructuring the logic.

Where to go next

You can write conditional branches. Next: switch statements — a cleaner alternative to long else if chains when you are testing a single variable against a list of constant values.

Finished reading? Mark it complete to track your progress.

On this page