Skip to content
StudyFame

Nested if-else in C

Some decisions happen in steps. First check the username; only if it is correct, check the password. First check if a number is positive; only then check if it is even.

Putting an if inside another if is called a nested if. In this lesson you will learn how it works and write the classic exam program: largest of three numbers.

What You Will Learn

  • What a nested if-else is.
  • Its syntax and flowchart.
  • Largest of three numbers using nested if-else.
  • A two-step login check.
  • When to use nesting and when to use &&.

Nested if-else in C Explained

Exam definition: A nested if-else is an if or if-else statement placed inside another if or else block. The inner condition is checked only when the outer condition allows it.

Memory trick: Nested if is like airport security: you reach the second check (boarding pass) only if you pass the first check (ID). Fail the first, and the second is never asked.

Syntax of Nested if-else

if (condition1)
{
    if (condition2)
    {
        /* condition1 true AND condition2 true */
    }
    else
    {
        /* condition1 true, condition2 false */
    }
}
else
{
    /* condition1 false */
}

Flowchart of nested if-else

            < condition1? >
             /           \
           Yes            No
            |              |
     < condition2? >   [ outer else ]
       /        \          |
     Yes         No        |
      |           |        |
 [ block A ] [ block B ]   |
      \           |       /
          [ next statement ]

Example 1: Largest of Three Numbers Using Nested if-else

#include <stdio.h>

int main(void)
{
    int a, b, c;

    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);

    if (a >= b)
    {
        if (a >= c)
            printf("Largest = %d\n", a);
        else
            printf("Largest = %d\n", c);
    }
    else
    {
        if (b >= c)
            printf("Largest = %d\n", b);
        else
            printf("Largest = %d\n", c);
    }

    return 0;
}

Output

Enter three numbers: 14 9 22
Largest = 22

Code Explanation

  1. Outer if: is a ≥ b? 14 ≥ 9 is true, so we enter the first block. Now we know b is not the largest.
  2. Inner if: is a ≥ c? 14 ≥ 22 is false, so the inner else runs and prints c = 22.
  3. The outer else block is skipped completely.
  4. Using >= instead of > makes the program correct even when two numbers are equal.

How Nested if-else Works

  • C checks the outer condition first.
  • It enters the matching outer block (if or else) and then checks the inner condition there.
  • Inner conditions in the block that was not entered are never checked.
  • Each else pairs with the nearest unmatched if, so use braces to keep the pairing clear and avoid the dangling else bug.

Example 2: Two-Step Login Check

#include <stdio.h>

int main(void)
{
    int user_id, pin;

    printf("User ID: ");
    scanf("%d", &user_id);

    if (user_id == 101)
    {
        printf("PIN: ");
        scanf("%d", &pin);

        if (pin == 4321)
            printf("Login successful. Welcome!\n");
        else
            printf("Wrong PIN.\n");
    }
    else
    {
        printf("User not found.\n");
    }

    return 0;
}

Output

User ID: 101
PIN: 4321
Login successful. Welcome!

The program asks for the PIN only if the user ID is correct. This step-by-step behaviour is something && alone cannot do.

Important Points to Remember

  • Nested if = an if inside another if or else block.
  • The inner condition is checked only when the outer path reaches it.
  • Each else pairs with the nearest unmatched if.
  • Use braces to keep nesting clear.
  • Don't nest too deep (more than 3 levels is hard to read).

Common Mistakes Beginners Make

MistakeWhat happensFix
No braces in nested if-elseelse attaches to the wrong ifAlways use { }
Using > for largest with equal numbersWrong answer when a == bUse >=
Nesting 4–5 levels deepCode is hard to read and debugCombine with && or use else-if
Bad indentationLooks like the wrong structureIndent each level consistently

Nested if vs Logical Operators

Nested ifLogical AND (&&)
Can do different things at each stepOne combined condition, one result
Good when the second check depends on the firstGood when both checks are simple
More linesShorter

Nested if-else Practice Questions

  1. What is a nested if-else statement?
  2. When is the inner if of a nested statement checked?
  3. Write the logic for the largest of three numbers.
  4. Which if does an else belong to in nested code?
  5. When would you use nested if instead of &&?
Show answers
  1. An if or if-else statement inside another if or else block.
  2. Only when the outer condition leads into the block that contains it.
  3. If a ≥ b: the largest is a if a ≥ c, else c. Otherwise: the largest is b if b ≥ c, else c.
  4. The nearest unmatched if above it.
  5. When the second check depends on the first, or different messages are needed at each step.

Coding Exercise

Read a number. If it is positive, print whether it is even or odd. If it is not positive, print "Not a positive number".

Show solution
#include <stdio.h>

int main(void)
{
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    if (n > 0)
    {
        if (n % 2 == 0)
            printf("Positive and even\n");
        else
            printf("Positive and odd\n");
    }
    else
    {
        printf("Not a positive number\n");
    }
    return 0;
}
Enter a number: 12
Positive and even

Quick Quiz: Nested if-else in C (MCQ)

  1. A nested if is:

    A) An if after another if   B) An if inside another if   C) An if without else   D) A loop

  2. If the outer condition is false, the inner if in the outer if-block is:

    A) Checked   B) Skipped   C) An error   D) Checked twice

  3. With a = 5, b = 5, c = 3, the program above prints:

    A) Largest = 5   B) Largest = 3   C) Nothing   D) Error

  4. An else belongs to:

    A) The outermost if   B) The nearest unmatched if   C) The first if   D) No if

  5. Best way to keep nested ifs clear:

    A) Write everything on one line   B) Use braces and indentation   C) Avoid else   D) Use goto

Show answers
  1. B: an if inside another if.
  2. B: skipped.
  3. A: Largest = 5.
  4. B: the nearest unmatched if.
  5. B: braces and indentation.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Nested ifif inside another if/else block
Inner condition checkedOnly if the outer path reaches it
else pairs withNearest unmatched if
Largest of 3Compare a, b first; then winner with c
Equal numbersUse >=

Frequently asked questions

What is a nested if statement?
An if statement inside another if or else, checked only when the outer path allows.
How do you find the largest of three numbers?
Compare a and b, then compare the bigger one with c.
Why use braces in nested if-else?
To avoid the dangling else problem and make the structure clear.
Can nested if be replaced by &&?
Sometimes, when both checks are simple; not when each step needs its own action.

Remember

  • Nested if = airport security: pass check 1 to reach check 2.
  • Braces + indentation always.

Next Topic

Next lesson: switch Statement in C. You will learn a cleaner way to choose from many fixed options: the switch statement.