Bird
Raised Fist0
C++programming~20 mins

Nested conditional statements in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Nested Condition 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 this C++ code when num = 15?
C++
#include <iostream>
using namespace std;

int main() {
    int num = 15;
    if (num > 10) {
        if (num < 20) {
            cout << "Between 11 and 19" << endl;
        } else {
            cout << "20 or more" << endl;
        }
    } else {
        cout << "10 or less" << endl;
    }
    return 0;
}
ANo output
B20 or more
C10 or less
DBetween 11 and 19
Attempts:
2 left
๐Ÿ’ก Hint
Check the conditions step by step: first if num > 10, then if num < 20.
โ“ Predict Output
intermediate
2:00remaining
Value of variable after nested conditions
What is the value of result after running this code?
C++
int x = 5;
int y = 10;
int result = 0;

if (x > 3) {
    if (y < 5) {
        result = 1;
    } else {
        result = 2;
    }
} else {
    result = 3;
}
A1
B3
C2
D0
Attempts:
2 left
๐Ÿ’ก Hint
Check the conditions for x and y carefully.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error in nested if-else code
What error will this code produce when compiled?
C++
#include <iostream>
using namespace std;

int main() {
    int a = 7;
    if (a > 5)
        if (a < 10)
            cout << "a is between 6 and 9" << endl;
        else
            cout << "a is 10 or more" << endl;
    else
        cout << "a is 5 or less" << endl;
    return 0;
}
ANo error, outputs: a is between 6 and 9
BCompilation error: else without matching if
CRuntime error: segmentation fault
DNo output
Attempts:
2 left
๐Ÿ’ก Hint
The second 'else' has no matching 'if' because the first 'else' already pairs with the inner 'if'.
๐Ÿ“ Syntax
advanced
2:00remaining
Find the syntax error in nested if-else
Which option contains the correct nested if-else syntax?
A
if (x &gt; 0)
    if (x &lt; 10)
        cout &lt;&lt; "x is between 1 and 9" &lt;&lt; endl;
    else
        cout &lt;&lt; "x is 10 or more" &lt;&lt; endl;
B
if (x &gt; 0) {
    if (x &lt; 10) {
        cout &lt;&lt; "x is between 1 and 9" &lt;&lt; endl;
    } else {
        cout &lt;&lt; "x is 10 or more" &lt;&lt; endl;
    }
}
C
if (x &gt; 0) {
    if (x &lt; 10) {
        cout &lt;&lt; "x is between 1 and 9" &lt;&lt; endl;
    } else
        cout &lt;&lt; "x is 10 or more" &lt;&lt; endl;
}
D
if (x &gt; 0) 
    if (x &lt; 10) 
        cout &lt;&lt; "x is between 1 and 9" &lt;&lt; endl;
    else {
        cout &lt;&lt; "x is 10 or more" &lt;&lt; endl;
}
Attempts:
2 left
๐Ÿ’ก Hint
Check for balanced braces and proper else matching.
๐Ÿš€ Application
expert
3:00remaining
Determine output of complex nested conditions
What will be printed when this code runs?
C++
#include <iostream>
using namespace std;

int main() {
    int score = 85;
    if (score >= 90) {
        cout << "Grade A" << endl;
    } else {
        if (score >= 80) {
            if (score >= 85) {
                cout << "Grade B+" << endl;
            } else {
                cout << "Grade B" << endl;
            }
        } else {
            cout << "Grade C or below" << endl;
        }
    }
    return 0;
}
AGrade B+
BGrade B
CGrade A
DGrade C or below
Attempts:
2 left
๐Ÿ’ก Hint
Follow the nested conditions carefully for score 85.

Practice

(1/5)
1.

What does a nested conditional statement mean in C++?

easy
A. A loop inside a function
B. An if or else inside another if or else
C. A variable declared inside a function
D. A function calling itself

Solution

  1. Step 1: Understand the meaning of nested conditionals

    Nested conditionals mean placing one if or else inside another to check multiple conditions step-by-step.
  2. Step 2: Match the definition to the options

    An if or else inside another if or else correctly describes this as an if or else inside another if or else.
  3. Final Answer:

    An if or else inside another if or else -> Option B
  4. Quick Check:

    Nested conditional = if inside if [OK]
Hint: Look for condition inside another condition [OK]
Common Mistakes:
  • Confusing nested conditionals with loops
  • Thinking nested means multiple variables
  • Mixing nested conditionals with recursion
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) else if (y < 10) { // code }
D. if (x > 0) { else if (y < 10) { // code } }

Solution

  1. Step 1: Check proper use of braces in nested if

    Correct nested if requires braces around inner if block inside outer if.
  2. Step 2: Identify correct syntax option

    if (x > 0) { if (y < 10) { // code } } shows braces properly enclosing the inner if inside the outer if, which is correct syntax.
  3. Final Answer:

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

    Braces around nested if blocks = correct syntax [OK]
Hint: Use braces {} to group nested if blocks [OK]
Common Mistakes:
  • Missing braces causing syntax errors
  • Using else if incorrectly inside nested if
  • Placing else without matching if
3.

What is the output of the following C++ code?

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

Solution

  1. Step 1: Evaluate outer if condition

    Since x = 5 which is greater than 0, the outer if condition is true, so we enter the first block.
  2. Step 2: Evaluate inner if condition

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

    A -> Option C
  4. Quick Check:

    Both conditions true, print "A" [OK]
Hint: Check outer condition first, then inner [OK]
Common Mistakes:
  • Ignoring inner condition and printing wrong letter
  • Confusing else blocks
  • Assuming else runs when outer if is true
4.

Find the error in this nested conditional code:

int a = 3;
if (a > 0)
    if (a < 5)
        std::cout << "Yes";
    else
        std::cout << "No";
else
    std::cout << "Maybe";
medium
A. Missing braces cause else to pair with wrong if
B. Syntax error: missing semicolon after if
C. Variable a is not declared
D. No error, code runs fine

Solution

  1. Step 1: Analyze the indentation and else pairing

    Without braces, the else pairs with the nearest if, which can cause confusion in nested ifs.
  2. Step 2: Identify the problem caused by missing braces

    Here, the else pairs with the inner if, not the outer, which may not be intended and causes logic errors.
  3. Final Answer:

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

    Braces needed to avoid else ambiguity [OK]
Hint: Always use braces to avoid else ambiguity [OK]
Common Mistakes:
  • Assuming else pairs with outer if
  • Ignoring indentation does not affect pairing
  • Thinking semicolon missing causes error
5.

Consider this code snippet:

int score = 85;
if (score >= 90) {
    std::cout << "Grade A";
} else {
    if (score >= 80) {
        std::cout << "Grade B";
    } else {
        std::cout << "Grade C";
    }
}

How can you rewrite this nested conditional using else if to improve clarity?

hard
A. if (score >= 90) { std::cout << "Grade A"; } else if (score < 80) { std::cout << "Grade C"; } else { std::cout << "Grade B"; }
B. if (score >= 90) { std::cout << "Grade A"; } if (score >= 80) { std::cout << "Grade B"; } else { std::cout << "Grade C"; }
C. if (score >= 90) { std::cout << "Grade A"; } else { std::cout << "Grade B"; } else { std::cout << "Grade C"; }
D. if (score >= 90) { std::cout << "Grade A"; } else if (score >= 80) { std::cout << "Grade B"; } else { std::cout << "Grade C"; }

Solution

  1. Step 1: Understand nested if-else structure

    The original code uses an if inside an else to check score ranges.
  2. Step 2: Rewrite using else if for clarity

    Using else if combines the conditions in a clear chain, avoiding deep nesting.
  3. Final Answer:

    if (score >= 90) { std::cout << "Grade A"; } else if (score >= 80) { std::cout << "Grade B"; } else { std::cout << "Grade C"; } -> Option D
  4. Quick Check:

    Use else if to flatten nested conditionals [OK]
Hint: Use else if to avoid nested if inside else [OK]
Common Mistakes:
  • Using multiple ifs without else causing multiple outputs
  • Placing else without matching if
  • Incorrect order of conditions
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