Bird
Raised Fist0
Cprogramming~20 mins

Nested conditional statements - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Conditionals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested if-else with integer input
What is the output of the following C code when num = 7?
C
#include <stdio.h>

int main() {
    int num = 7;
    if (num > 5) {
        if (num < 10) {
            printf("A\n");
        } else {
            printf("B\n");
        }
    } else {
        printf("C\n");
    }
    return 0;
}
AB
BC
CA
DNo output
Attempts:
2 left
💡 Hint
Check the conditions step by step starting from the outer if.
❓ Predict Output
intermediate
2:00remaining
Output of nested if-else with character input
What will be printed when the variable ch = 'b' is used in this code?
C
#include <stdio.h>

int main() {
    char ch = 'b';
    if (ch == 'a') {
        printf("Apple\n");
    } else {
        if (ch == 'b') {
            printf("Banana\n");
        } else {
            printf("Cherry\n");
        }
    }
    return 0;
}
ABanana
BCherry
CApple
DNo output
Attempts:
2 left
💡 Hint
Check the first if condition and then the nested else-if.
🔧 Debug
advanced
2:00remaining
Identify the error in nested if-else code
What error will this code produce when compiled?
C
#include <stdio.h>

int main() {
    int x = 10;
    if (x > 5)
        if (x < 15)
            printf("Inside\n");
        else
            printf("Outside\n");
    return 0;
}
ASyntax error due to missing braces
BRuntime error
CNo error, prints "Outside"
DNo error, prints "Inside"
Attempts:
2 left
💡 Hint
Remember how else matches the nearest if without braces.
🧠 Conceptual
advanced
2:00remaining
Number of times inner block executes
Consider the code below. How many times will the inner printf("Hello\n") execute?
C
#include <stdio.h>

int main() {
    int i;
    for (i = 0; i < 5; i++) {
        if (i % 2 == 0) {
            if (i > 1) {
                printf("Hello\n");
            }
        }
    }
    return 0;
}
A4
B2
C3
D1
Attempts:
2 left
💡 Hint
Check values of i that are even and greater than 1.
❓ Predict Output
expert
3:00remaining
Output of nested if-else with multiple conditions
What is the output of this code snippet?
C
#include <stdio.h>

int main() {
    int a = 3, b = 7, c = 5;
    if (a > b) {
        if (b > c) {
            printf("X\n");
        } else {
            printf("Y\n");
        }
    } else {
        if (a > c) {
            printf("Z\n");
        } else {
            printf("W\n");
        }
    }
    return 0;
}
AW
BZ
CX
DY
Attempts:
2 left
💡 Hint
Evaluate conditions stepwise: a > b, then b > c, then a > c.

Practice

(1/5)
1.

What does a nested conditional statement in C allow you to do?

Choose the best description.

easy
A. Define a function inside another function
B. Check a condition inside another condition
C. Repeat a block of code multiple times
D. Create a variable that changes type

Solution

  1. Step 1: Understand the meaning of nested conditionals

    Nested conditionals mean putting one if or else if statement inside another if or else if block.
  2. Step 2: Compare with other options

    Repeating code is done by loops, not conditionals. Functions inside functions or variable type changes are unrelated.
  3. Final Answer:

    Check a condition inside another condition -> Option B
  4. Quick Check:

    Nested conditionals = condition inside condition [OK]
Hint: Nested means inside another condition [OK]
Common Mistakes:
  • Confusing nested conditionals with loops
  • Thinking nested means changing variable types
  • Mixing nested conditionals with function definitions
2.

Which of the following is the correct syntax for a nested if statement in C?

if (x > 0) {
    if (y < 10) {
        // code
    }
}
easy
A. if x > 0 { if (y < 10) { /* code */ } }
B. if (x > 0) if (y < 10) { /* code */ }
C. if (x > 0) { if y < 10 { /* code */ } }
D. if (x > 0) { if (y < 10) { /* code */ } }

Solution

  1. Step 1: Check correct use of braces and parentheses

    In C, conditions must be inside parentheses and blocks inside braces. if (x > 0) { if (y < 10) { /* code */ } } uses both correctly.
  2. Step 2: Identify syntax errors in other options

    if (x > 0) if (y < 10) { /* code */ } misses braces for outer if block. if (x > 0) { if y < 10 { /* code */ } } misses parentheses around y < 10. if x > 0 { if (y < 10) { /* code */ } } misses parentheses around x > 0.
  3. Final Answer:

    if (x > 0) { if (y < 10) { /* code */ } } -> Option D
  4. Quick Check:

    Correct syntax = if (x > 0) { if (y < 10) { /* code */ } } [OK]
Hint: Use parentheses for conditions and braces for blocks [OK]
Common Mistakes:
  • Omitting parentheses around conditions
  • Forgetting braces for code blocks
  • Mixing syntax from other languages
3.

What is the output of this C code?

int x = 5, y = 8;
if (x > 0) {
    if (y < 10) {
        printf("A\n");
    } else {
        printf("B\n");
    }
} else {
    printf("C\n");
}
medium
A. A
B. B
C. C
D. No output

Solution

  1. Step 1: Evaluate outer if condition

    x = 5, which is greater than 0, so the outer if block runs.
  2. Step 2: Evaluate inner if condition

    y = 8, which is less than 10, so the inner if block runs and prints "A".
  3. Final Answer:

    A -> Option A
  4. Quick Check:

    x > 0 and y < 10 prints A [OK]
Hint: Check outer condition first, then inner [OK]
Common Mistakes:
  • Ignoring inner condition
  • Confusing else blocks
  • Assuming no output if outer condition true
4.

Find the error in this nested conditional code snippet:

if (a > 10)
    if (b < 5)
        printf("X\n");
    else
        printf("Y\n");
medium
A. Missing braces causing else to pair with wrong if
B. Incorrect use of semicolons after if statements
C. Using printf without including stdio.h
D. Variables a and b are not declared

Solution

  1. Step 1: Analyze the if-else pairing

    Without braces, the else pairs with the nearest if, which is the inner if. This may cause confusion or unexpected behavior.
  2. Step 2: Identify the fix

    Adding braces around the outer if block ensures the else pairs correctly and code is clear.
  3. Final Answer:

    Missing braces causing else to pair with wrong if -> Option A
  4. Quick Check:

    Braces fix else pairing [OK]
Hint: Always use braces to avoid else ambiguity [OK]
Common Mistakes:
  • Assuming else pairs with outer if
  • Ignoring braces importance
  • Thinking semicolons cause this error
5.

Given the code below, what will be printed if score = 75?

if (score >= 90) {
    printf("Grade A\n");
} else {
    if (score >= 70) {
        if (score >= 80) {
            printf("Grade B+\n");
        } else {
            printf("Grade B\n");
        }
    } else {
        printf("Grade C\n");
    }
}
hard
A. Grade B+
B. Grade C
C. Grade B
D. Grade A

Solution

  1. Step 1: Check first if condition

    score = 75, which is not >= 90, so skip first if block.
  2. Step 2: Check second if condition

    score >= 70 is true, so enter second if block.
  3. Step 3: Check nested if inside second block

    score >= 80 is false (75 < 80), so else block runs printing "Grade B".
  4. Final Answer:

    Grade B -> Option C
  5. Quick Check:

    75 >= 70 but < 80 prints Grade B [OK]
Hint: Check conditions top-down carefully [OK]
Common Mistakes:
  • Confusing >= 70 and >= 80 checks
  • Skipping nested else block
  • Assuming 75 is Grade B+
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard