C Nested if Statement

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

What Is a Nested if?

A nested if is an if placed inside another if (or else) block. The inner test runs only after the outer condition is already true—useful for layered checks like “old enough, then has ID.”

Remember
if (outer) {
    if (inner) {
        /* both true */
    }
}

outer false → inner never checked
both true   → innermost body runs

brace every level
flat choices → else-if; both required → &&
UsePrefer
Second test only after firstNested if (this page)
Both must be true, no middle else&& in one if
Mutually exclusive bandselse-if / switch

How to Use Nested if in C

Check the outer gate first, then the inner detail—brace every level.

  1. Write the outer if for the first requirement.
  2. Inside its braces, write another if for the next check.
  3. Add else at the level that needs a false path (outer or inner).
  4. Compile with gcc file.c -std=c11 -o out.
PieceRequired?Role
Outer conditionYesGate—must pass before inner runs
Inner conditionYesSecond check, only if outer is true
Braces { }Strongly recommendedShows nesting; avoids dangling else

Pseudocode

Pseudocode
if outer condition is true:
    if inner condition is true:
        do the work that needs both
    else:
        outer true, inner false
else:
    outer false (inner never tested)

Cheat Sheet

Keep these facts handy while nesting decisions.

TopicDetail
Ideaif inside if / else
OrderOuter checked first; then inner
vs &&Use && when no middle else
vs else-ifSame-level exclusive choices
BracesAlways — every level
Dangling elseelse → nearest unmatched if
DepthKeep shallow; refactor if deep

Live Preview

Simulate nested checks in the browser. Same ideas as the C examples below (compile those with gcc).

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

Result
age = 20, has_id = 1
outer: age >= 18 → true
  inner: has_id → true
→ "allowed"

Worked Walkthrough — Age, Then ID

Outer gate: adult. Inner gate: has ID. Same pattern as Example 1.

Trace
Case A: age = 20, has_id = 1
  outer true → enter
    inner true → "allowed"

Case B: age = 20, has_id = 0
  outer true → enter
    inner false → "need ID"

Case C: age = 15
  outer false → skip inner entirely → "too young"

C Examples

Compile every example with gcc file.c -std=c11 -o out. Also see if, if-else, and else-if.

Example 1 — Age, Then ID

The ID check runs only after the age check passes—true nesting with different messages at each level.

C
#include <stdio.h>

int main(void) {
    int age = 20;
    int has_id = 1;  /* 1 = yes, 0 = no */

    if (age >= 18) {
        if (has_id) {
            printf("allowed\n");
        } else {
            printf("need ID\n");
        }
    } else {
        printf("too young\n");
    }

    return 0;
}

How It Works

  1. Outer gate. age >= 18 is true for 20 → enter the outer block.
  2. Inner gate. has_id is 1 (true) → print allowed.
  3. Inner else. If has_id were 0, print need ID.
  4. Outer else. If age were under 18, skip the inner if and print too young.

Example 2 — Positive, Then Even or Odd

First confirm the number is positive; only then classify even/odd.

C
#include <stdio.h>

int main(void) {
    int n = 8;

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

    return 0;
}

How It Works

  1. Outer. n > 0 is true for 8.
  2. Inner. n % 2 == 0 is true → print positive even.
  3. Odd case. For n = 7, the inner else prints positive odd.
  4. Not positive. For n = -1 or 0, the outer else runs.

Example 3 — Prefer && When No Middle Else

If you only care that both are true and have no message between them, one if with && is clearer than nesting.

C
#include <stdio.h>

int main(void) {
    int n = 8;

    /* Clearer than: if (n > 0) { if (n % 2 == 0) { ... } } */
    if (n > 0 && n % 2 == 0) {
        printf("positive even\n");
    }

    return 0;
}

How It Works

  1. Combine. && requires both n > 0 and even.
  2. Short-circuit. If n > 0 is false, the even check is skipped.
  3. When to nest. Nest when you need different else messages at each level (Examples 1–2).
  4. When to flatten. Use && when the only goal is “both true.”

Edge Cases & Pitfalls

Check these before nesting decisions in real projects.

dangling

Dangling else

Without braces, else binds to the nearest if. Nested code without { } is a common source of wrong branches.

depth

Too many levels

Four+ levels of nesting are hard to read. Flatten with &&, else-if, or a helper function.

vs and

Nesting without a middle else

If both must be true and you have no per-level message, prefer if (a && b) over two empty-nested ifs.

indent

Indentation lies

C ignores indentation. Only braces define structure—pretty formatting does not change which else matches which if.

Performance Notes

ApproachCostBest for
Nested ifCheap; may skip inner testsLayered gates with different elses
&& / ||Short-circuit, often clearerCombined conditions, no middle else
else-if / switchSimilar; often clearerSame-level exclusive choices

Nesting is not slow—unreadability is the real cost. Put the cheapest or most likely-to-fail check outermost when it helps you skip work.

Key Takeaways

  • Role: inner if runs only after outer is true.
  • When: layered checks with different else messages.
  • Flatten: use && or else-if when clearer.
  • Safety: brace every level; avoid dangling else.

One line: nest when the next check only matters after the first—brace every level.

Frequently Asked Questions

A nested if is an if statement placed inside another if (or else) block. The inner if runs only when the outer condition is already true. This lets you test layered requirements step by step.
C does not set a hard limit, but deep nesting (four or more levels) is hard to read. Prefer else-if ladders, logical operators (&&, ||), or small helper functions when logic grows.
Use nested if when a second condition only matters after the first is true—e.g. check age, then check ID. Use else-if when outcomes are mutually exclusive at the same level—e.g. grade bands A, B, C.
When both conditions must be true together and there is no else branch between them, combine with &&: if (a > 0 && a % 2 == 0). That is clearer than two nested ifs with no else in between.
Always use { } on every if and else in real code. Braces make nesting visible, prevent the dangling-else ambiguity, and stop bugs when you add another line later.
In C, else binds to the nearest unmatched if. Without braces, else may attach to the wrong if in nested code. Always brace nested blocks so else clearly belongs to the if you intend.

Next: switch Statement

Learn multi-way branching on discrete values with case labels.

switch 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