C if-else Statement

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is if-else?

if-else is a two-way branch: one block runs when the condition is true, and a different block runs when it is false. Exactly one path executes—never both, never neither.

Remember
if (condition) {
    /* true path */
} else {
    /* false path */
}

true  → if body only
false → else body only

use { } on both sides
need 3+ choices → else-if / switch
UsePrefer
True path onlyif
True path + false pathif-else (this page)
Three or more choiceselse-if / switch

How to Use if-else in C

Write the condition, then both outcomes in braced blocks.

  1. Decide the test (e.g. n % 2 == 0 or score >= 40).
  2. Put the true work in if (condition) { ... }.
  3. Put the false work in else { ... } right after.
  4. Compile with gcc file.c -std=c11 -o out.
PieceRequired?Role
ConditionYesChooses which branch runs
if bodyYesRuns when condition is true
else bodyYes (for if-else)Runs when condition is false

Pseudocode

Pseudocode
set or read the value to test

if condition is true:
    do the true work
else:
    do the false work

continue after the branch

Cheat Sheet

Keep these facts handy while writing two-way branches.

TopicDetail
Syntaxif (cond) { ... } else { ... }
BranchesExactly one of the two runs
vs ifelse covers the false case
BracesUse { } on both sides
Dangling elseelse binds to nearest if
More choiceselse-if chain
Sampleeven/odd, pass/fail, sign of n

Live Preview

Simulate if-else choices in the browser. Same ideas as the C examples below (compile those with gcc).

Browser simulation of if-else (same idea as the C examples below).

Result
n = 8
if (n % 2 == 0) → true
→ print "even"
(else skipped)

Worked Walkthrough — Even or Odd

Trace if (n % 2 == 0) … else … with two values. Same pattern as Example 1.

Trace
Case A: n = 8
  8 % 2 == 0?  true  → if body  → "even"
  else skipped

Case B: n = 7
  7 % 2 == 0?  false → else body → "odd"
  if body skipped

One branch only — always.

C Examples

Compile every example with gcc file.c -std=c11 -o out. Review if; next try else-if for more than two outcomes.

Example 1 — Even or Odd

Classic two-way branch: remainder 0 means even; otherwise odd.

C
#include <stdio.h>

int main(void) {
    int n = 8;

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

    return 0;
}

How It Works

  1. Set a value. n = 8 is what we test.
  2. Remainder. n % 2 is 0 for even numbers.
  3. True path. Condition is true → print even.
  4. False path. For n = 7, the else branch would print odd.

Example 2 — Pass or Fail

Use a threshold: score at or above 40 passes; otherwise fail.

C
#include <stdio.h>

int main(void) {
    int score = 55;

    if (score >= 40) {
        printf("pass\n");
    } else {
        printf("fail\n");
    }

    return 0;
}

How It Works

  1. Threshold. Passing means score >= 40.
  2. True path. 55 meets the bar → print pass.
  3. False path. A score like 30 would take the else and print fail.
  4. Exclusive. Only one message prints—never both.

Example 3 — Positive or Not

else covers every case that is not positive (zero and negative). For three labels (positive / zero / negative), use else-if.

C
#include <stdio.h>

int main(void) {
    int n = -2;

    if (n > 0) {
        printf("positive\n");
    } else {
        printf("not positive\n");
    }

    return 0;
}

How It Works

  1. Test. n > 0 is false for -2.
  2. Skip if. The true branch does not run.
  3. Run else. Prints not positive (covers zero and negatives).
  4. Limit. Two labels only—split further with else if when needed.

Edge Cases & Pitfalls

Check these before relying on if-else in real projects.

dangling

Dangling else

Without braces, else pairs with the nearest if—often not the one you meant. Always brace both bodies.

assign

= instead of ==

if (x = 1) assigns and takes the true branch. Compare with ==; compile with -Wall.

two-way

Only two outcomes

else means “everything else.” For positive / zero / negative, use else-if, not a single if-else.

empty

Stray semicolon

if (ok); else … makes an empty if. The following else may not match what you expect—avoid the semicolon after the condition.

Performance Notes

ApproachCostBest for
if-elseOne branch takenBinary decisions
Long else-if chainsChecks until a matchPut common cases first
switch on intsOften jump tableMany discrete values — see switch

An if-else is cheap. Prefer the form that reads clearest. For many constant cases, switch can be clearer and sometimes faster.

Key Takeaways

  • Role: exactly one of two branches runs.
  • vs if: else covers the false case explicitly.
  • Style: brace both sides; avoid dangling else.
  • More paths: use else-if or switch.

One line: if-else always picks exactly one path—true or false—brace both bodies.

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, never neither.
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. A plain if-else handles only true vs false. See the else-if tutorial for multi-way chains.
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 so intent is 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.

Next: else-if Statement

Learn how to chain more than two related conditions.

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.

9 people found this page helpful