0
0
C++programming~20 mins

If statement in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested if statements
What is the output of this C++ code snippet?
C++
int x = 5;
int y = 10;
if (x > 3) {
    if (y < 15) {
        std::cout << "A";
    } else {
        std::cout << "B";
    }
} else {
    std::cout << "C";
}
AA
BB
CC
DNo output
Attempts:
2 left
💡 Hint
Check both conditions in the nested if statements carefully.
Predict Output
intermediate
2:00remaining
If-else with logical operators
What will this C++ code print?
C++
int a = 7;
int b = 3;
if (a > 5 && b < 5) {
    std::cout << "X";
} else {
    std::cout << "Y";
}
ANo output
BY
CX
DCompilation error
Attempts:
2 left
💡 Hint
Both conditions in the if must be true for the if block to run.
Predict Output
advanced
2:00remaining
If statement with assignment inside condition
What is the output of this C++ code?
C++
int x = 0;
if (x = 5) {
    std::cout << x;
} else {
    std::cout << 0;
}
A5
B0
CCompilation error
DUndefined behavior
Attempts:
2 left
💡 Hint
Remember that assignment inside if condition returns the assigned value.
Predict Output
advanced
2:00remaining
If statement with multiple else if branches
What will this C++ code print?
C++
int n = 15;
if (n < 10) {
    std::cout << "Low";
} else if (n < 20) {
    std::cout << "Medium";
} else {
    std::cout << "High";
}
ANo output
BLow
CHigh
DMedium
Attempts:
2 left
💡 Hint
Check which condition matches the value of n first.
Predict Output
expert
2:00remaining
If statement with complex boolean expression
What is the output of this C++ code?
C++
bool a = true;
bool b = false;
bool c = true;
if ((a && b) || (b || c)) {
    std::cout << "Yes";
} else {
    std::cout << "No";
}
ANo
BYes
CCompilation error
DNo output
Attempts:
2 left
💡 Hint
Evaluate the boolean expression step by step.