C if-else Statement

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
Two-way branch

What You’ll Learn

The if-else statement handles two outcomes: when a condition is true, one block runs; when false, the else block runs instead. Use it whenever your program must always choose between two paths—pass or fail, even or odd, yes or no.

01

Syntax

if ... else.

02

Two paths

True/false.

03

One runs

Not both.

04

Nested

else in if.

05

Braces

Both sides.

06

else if

Next page.

Definition and Usage

An if-else statement extends plain if with an alternative. C evaluates the condition once. If it is true (non-zero), the if body runs and the else body is skipped. If false (zero), the if body is skipped and the else body runs.

Use if-else when both outcomes need explicit code. If you only care about the true case, a plain if is enough. For three or more mutually exclusive cases, use an else if chain in the next tutorial.

💡
Beginner Tip

Put braces around both the if and else bodies. This prevents the “dangling else” problem where else accidentally attaches to the wrong if when you nest conditions.

📝 Syntax

Standard two-way branch in C:

C
if (condition) {
    // runs when condition is true
} else {
    // runs when condition is false
}

Flow

  • Evaluate condition once.
  • True → execute if block, skip else.
  • False → skip if block, execute else.
  • Continue with the next statement after the whole construct.

Preview: else if (next tutorial)

C
if (score >= 90) {
    printf("A\n");
} else if (score >= 80) {
    printf("B\n");
} else {
    printf("Below B\n");
}
/* See /c/statements/else-if for full coverage */

Compile

C
gcc program.c -std=c11 -o program

⚡ Quick Reference

PatternExample
Sign checkif (n > 0) ... else ...
Even / oddif (n % 2 == 0) ... else ...
Pass / failif (score >= 50) ... else ...
Null checkif (ptr != NULL) ... else ...
Min of twoif (a < b) min = a; else min = b;
True
if { }

Runs

False
else { }

Runs

Pairing
else needs if

Required

3+ cases
else if

Next tut

Examples Gallery

Compile with gcc example.c -std=c11 -o example. Each program shows a classic if-else pattern.

📚 Getting Started

Simple true vs false branches.

Example 1 — Positive or Non-Positive

From the reference: print different messages for each outcome.

C
#include <stdio.h>

int main(void) {
    int number = 10;

    if (number > 0) {
        printf("The number is positive.\n");
    } else {
        printf("The number is non-positive.\n");
    }

    return 0;
}

With number = -5, the else branch would print The number is non-positive.

How It Works

Exactly one branch runs. 10 > 0 is true, so the else block is never executed.

Example 2 — Even or Odd

Two-way split using the modulo operator.

C
#include <stdio.h>

int main(void) {
    int n = 7;

    if (n % 2 == 0) {
        printf("%d is even.\n", n);
    } else {
        printf("%d is odd.\n", n);
    }

    return 0;
}

How It Works

Every integer is either even or odd—a perfect fit for if-else with no third case needed.

📈 Practical Patterns

Grades, nesting, and choosing between two values.

Example 3 — Pass or Fail

Compare a score against a threshold.

C
#include <stdio.h>

int main(void) {
    int score = 42;

    if (score >= 50) {
        printf("Result: PASS (%d)\n", score);
    } else {
        printf("Result: FAIL (%d)\n", score);
    }

    return 0;
}

How It Works

Binary decisions like pass/fail map naturally to if-else. For letter grades A/B/C/D, use an else if ladder instead.

Example 4 — Nested if-else (Adult Check)

Outer if-else for age; inner if-else for gender.

C
#include <stdio.h>

int main(void) {
    int age = 25;
    char gender = 'F';

    if (age > 18) {
        if (gender == 'M') {
            printf("Adult male.\n");
        } else {
            printf("Adult female.\n");
        }
    } else {
        printf("Not an adult.\n");
    }

    return 0;
}

How It Works

The inner else pairs with the inner if (gender == 'M'). Braces make the structure obvious. See also the nested if tutorial.

Example 5 — Find the Smaller of Two Numbers

Use if-else to assign the minimum.

C
#include <stdio.h>

int main(void) {
    int a = 30;
    int b = 12;
    int min;

    if (a < b) {
        min = a;
    } else {
        min = b;
    }

    printf("min(%d, %d) = %d\n", a, b, min);
    return 0;
}

How It Works

Each branch assigns a different value to min. When equal, else runs and picks b—either value is fine for a tie.

🚀 Common Use Cases

  • Pass/fail — threshold comparisons.
  • Sign tests — positive vs zero/negative.
  • Parity — even vs odd.
  • Null safety — use pointer vs report error.
  • Min/max — pick the smaller or larger of two values.
  • Toggle flags — flip state between two choices.

🧠 How if-else Works

1

Evaluate once

Compute the condition expression.

Test
2

Pick a branch

True → if body. False → else body.

Branch
3

Skip the other

The unchosen block is never executed.

Exclusive
=

Continue

Next statement after the whole if-else.

📝 Notes

  • else must follow an if or else if—never alone.
  • Only one of if or else runs per evaluation.
  • Without braces, else binds to the nearest preceding if.
  • For three or more cases, use else if (see next tutorial).
  • else needs no condition—it is the default when all prior tests fail.
  • You can nest if-else inside either branch.

⚡ Optimization

Modern CPUs predict branches; keep hot paths in the if branch when one outcome is far more common. For simple min/max, the ternary operator min = (a < b) ? a : b; compiles to similar code but use if-else when beginners need clarity or when branches contain multiple statements.

Conclusion

The C if-else statement handles exactly two outcomes. Master it for pass/fail, even/odd, and binary choices, then learn else if when you need three or more exclusive paths.

Always brace both branches and keep nesting shallow for readable code.

💡 Best Practices

✅ Do

  • Use braces on both if and else bodies
  • Keep each branch focused and readable
  • Use if-else for true binary decisions
  • Indent nested blocks consistently
  • Move to else if when you have 3+ cases

❌ Don’t

  • Write else without a matching if
  • Nest deeply without braces
  • Duplicate large blocks in both branches
  • Use if-else when plain if suffices
  • Chain many if-else when switch fits better

Key Takeaways

Knowledge Unlocked

Five things to remember about if-else

Two-way branching in C programs.

5
Core concepts
02

One runs

Exclusive.

Flow
{ } 03

Braces

Both sides.

Safety
🕸 04

Nested

if in else.

Structure
📈 05

else if

3+ cases.

Next

❓ Frequently Asked Questions

if-else runs one block when the condition is true and a different block when it is false. Syntax: if (condition) { ... } else { ... }. Exactly one of the two branches executes (never both).
if alone does nothing when the condition is false. if-else always handles both outcomes—use it when you need an explicit alternative path, such as printing pass or fail.
No. else must immediately follow an if or else-if block. A standalone else is a compile error.
else if chains more than two conditions: if, else if, else if, ..., else. It is covered in the else-if tutorial. A plain if-else handles only true vs false.
Without braces, else binds to the nearest if. In if (a) if (b) x++; else y++; the else pairs with the inner if, not the outer one. Always use braces to make intent clear.
C allows single statements without braces, but use { } on both if and else bodies in real code for readability and to avoid dangling-else bugs.
Did you know?

C’s ternary operator condition ? expr1 : expr2 is an expression form of if-else that returns a value. It is handy for short assignments like max = (a > b) ? a : b; but harder to read when branches contain side effects.

Continue to else-if

Learn how to chain multiple conditions when two branches are not enough.

else-if tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful