Stat Mania
  • C Programming
  • All Courses
  1. Making Decisions
  2. Nested if in C, Explained with Real-Life Examples
  • Course Home
  • Getting Started
    • C's "Hello, World!" Explained Line by Line (for R & Python Users)
    • How to Test a Small C Snippet Quickly
  • Variables and Data Types
    • Declaring Multiple Variables in One Line in C
    • float vs double in C: Precision, Memory, and When to Use Which
  • Operators and Input
    • Pre-Increment vs Post-Increment in C: ++i vs i++
    • Checking User Input in C: Is It Really a Number?
  • Making Decisions
    • Nested if in C, Explained with Real-Life Examples
    • Shortcut Ways to Write if in C: the Ternary Operator and More
    • The switch Statement in C: Syntax, break, Fall-Through and Examples
  • Loops
    • The do-while Loop in C: Run First, Check After
    • Why Use break in a while Loop If the Condition Already Stops It?
    • Printing a Comma-Separated List in C (No Trailing Comma, Clean Line Breaks)
    • 5 while Loop Practice Problems in C (with Hints and Solutions)
    • 5 for Loop Practice Problems in C (with Hints and Solutions)
    • 5 Nested for Loop Practice Problems in C (with Hints and Solutions)
  • Arrays and Structs
    • How to Save Loop Results in an Array in C
    • Structs in C: Grouping Related Data (with Python and R Comparisons)
  • Random Numbers
    • srand(time(NULL)) in C: Why Random Numbers Repeat and How to Seed Them
    • Tossing a Die in C Until We Get a 6

On this page

  • Example 1: E-commerce discount eligibility
  • Example 2: ATM withdrawal
  • Nested if or &&?
  • Common pitfalls
  • Key takeaway
  • References
  1. Making Decisions
  2. Nested if in C, Explained with Real-Life Examples

Nested if in C, Explained with Real-Life Examples

What a nested if statement is in C, when the inner check runs, and two worked examples (a store discount and an ATM withdrawal) with flowcharts, code and common pitfalls.
Reading 5 min

By the end of this lesson you will

  • Predict when an inner if runs
  • Trace a nested decision with a flowchart

Resources

  • if statement

Nested if: the inner check runs only when the outer condition is true

A nested if statement in C is simply an if statement placed inside another if statement. It lets you test conditions that depend on one another: the inner condition is evaluated only if the outer condition is true.

Example 1: E-commerce discount eligibility

Imagine an online store running a promotion:

  1. Outer condition: is the customer a premium member?
  2. Inner condition: only if they are, check the purchase amount. Over $1,000 earns a 20% discount; otherwise 10%.
  3. Non-members get no discount at all.
#include <stdio.h>

int main() {
    int isMember = 1;              // 1 for yes, 0 for no
    double purchaseAmount = 1250.00;

    // Outer if: is the user a premium member?
    if (isMember == 1) {
        printf("Membership verified.\n");

        // Inner (nested) if: the order total decides the discount
        if (purchaseAmount >= 1000.0) {
            printf("Success: You qualify for a 20%% VIP discount!\n");
        } else {
            printf("Success: You qualify for a 10%% Standard Member discount.\n");
        }

    } else {
        // Runs if the outer condition (isMember == 1) is false
        printf("Standard account: No discount available for non-members.\n");
    }

    return 0;
}

Output:

Membership verified.
Success: You qualify for a 20% VIP discount!

Note %% in printf: a single % starts a format specifier, so a literal percent sign must be written twice.

The flowchart at the top of this post shows the same logic. If isMember were 0, the computer would skip the inner purchaseAmount check entirely and jump straight to the outer else.

Example 2: ATM withdrawal

Here is another common scenario:

  1. Outer condition: did the user enter the correct PIN?
  2. Inner condition: only if so, does the account have enough balance for the withdrawal?
#include <stdio.h>

int main() {
    int enteredPin = 1234;
    int correctPin = 1234;
    double accountBalance = 5000.00;
    double withdrawalAmount = 6000.00;

    // Outer if: security check for the PIN
    if (enteredPin == correctPin) {
        printf("PIN verified successfully.\n");

        // Inner (nested) if: financial check for sufficient funds
        if (withdrawalAmount <= accountBalance) {
            printf("Transaction approved. Please collect your cash.\n");
        } else {
            printf("Transaction denied: Insufficient funds in your account.\n");
        }

    } else {
        // Runs if the outer condition (enteredPin == correctPin) is false
        printf("Transaction denied: Invalid PIN entered.\n");
    }

    return 0;
}

Output (the withdrawal exceeds the balance):

PIN verified successfully.
Transaction denied: Insufficient funds in your account.

The logic as a text flowchart:

[Is the PIN correct?]
       │
       ├──► YES ──► [Is there enough cash in the account?]
       │                   │
       │                   ├──► YES ──► Dispense cash
       │                   └──► NO  ──► Show "Insufficient funds"
       │
       └──► NO  ──────────────────────► Show "Invalid PIN"
NoteWhy this must be nested

You must not check the account balance before verifying the user’s identity. Nesting keeps the balance logic locked behind a successful PIN check, so it can never run when the PIN is wrong.

Nested if or &&?

If the outer check has no else of its own and you only want to know whether both conditions hold, a single if with && is shorter:

if (isMember == 1 && purchaseAmount >= 1000.0) {
    printf("20%% discount\n");
}

Use nesting when each level needs its own different outcome (as in both examples above, where “not a member” and “member but small purchase” give different results).

Common pitfalls

  • Dangling else: an else always belongs to the nearest unmatched if. Without braces, the compiler pairs it differently from what the indentation suggests. Always use braces { }.
  • Too deep: more than two or three levels becomes hard to read. Flatten with &&, an early return, or a helper function.
  • = instead of ==: if (isMember = 1) assigns and is always true. Compile with -Wall to catch it.

Key takeaway

The inner if is evaluated only when the outer condition is true. This makes the program efficient and lets you express steps that must happen in order, such as verifying identity before touching money.

References

  1. C nested if (W3Schools)
  2. Nested if statement in C with code examples (Uncodemy)
  3. Nested if-else in C (WsCube Tech)
  4. C if…else statement example (BeginnersBook)
  • The inner if runs only when the outer condition is true.
  • && can replace a nest when there is no else between the checks.
  • Use braces on every level.

Nesting. Placing one statement inside another, such as an if inside an if.

Back to top
Stat Mania

Free, hands-on statistics and programming courses that run right in your browser.

Courses

All courses R Programming Statistics Probability

Resources

Practice quizzes Tools & games Quarto docs Course kit on GitHub

Company

Home About Contact Privacy policy Terms of use
© Stat Mania. All rights reserved.
Making Decisions
Shortcut Ways to Write if in C: the Ternary Operator and More