0
0
C++programming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If–else 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 statements
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int x = 10;
    if (x > 5) {
        if (x < 15) {
            std::cout << "A";
        } else {
            std::cout << "B";
        }
    } else {
        std::cout << "C";
    }
    return 0;
}
ANo output
BB
CA
DC
Attempts:
2 left
💡 Hint
Check the conditions step by step starting from the outer if.
Predict Output
intermediate
2:00remaining
Output with else if ladder
What will this program print?
C++
#include <iostream>
int main() {
    int score = 75;
    if (score >= 90) {
        std::cout << "A";
    } else if (score >= 80) {
        std::cout << "B";
    } else if (score >= 70) {
        std::cout << "C";
    } else {
        std::cout << "F";
    }
    return 0;
}
AA
BB
CF
DC
Attempts:
2 left
💡 Hint
Check which condition the score satisfies first.
Predict Output
advanced
2:00remaining
Output of if–else with logical operators
What is the output of this code?
C++
#include <iostream>
int main() {
    int a = 5, b = 10;
    if (a > 0 && b < 5) {
        std::cout << "X";
    } else if (a > 0 || b < 5) {
        std::cout << "Y";
    } else {
        std::cout << "Z";
    }
    return 0;
}
AY
BX
CZ
DNo output
Attempts:
2 left
💡 Hint
Evaluate the logical conditions carefully.
Predict Output
advanced
2:00remaining
Value of variable after if–else execution
What is the value of variable result after running this code?
C++
#include <iostream>
int main() {
    int x = 3, result = 0;
    if (x % 2 == 0) {
        result = 10;
    } else {
        result = 20;
    }
    std::cout << result;
    return 0;
}
A20
B0
C10
DCompilation error
Attempts:
2 left
💡 Hint
Check if x is even or odd.
Predict Output
expert
2:00remaining
Behavior of if–else without braces
What will be the output of this code snippet?
C++
#include <iostream>
int main() {
    int x = 0;
    if (x == 0)
        std::cout << "Zero";
    std::cout << "Done";
    else
        std::cout << "Not zero";
    return 0;
}
AZero
BCompilation error
CNot zero
DZeroDone
Attempts:
2 left
💡 Hint
Without braces, 'if' controls only the next single statement, and an extra statement before 'else' causes a syntax error.