0
0
C++programming~20 mins

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

Choose your learning style9 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.