0
0
C++programming~20 mins

Why conditional logic is needed in C++ - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Logic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of conditional logic in C++
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int x = 10;
    if (x > 5) {
        std::cout << "Greater" << std::endl;
    } else {
        std::cout << "Smaller or equal" << std::endl;
    }
    return 0;
}
ACompilation error
BGreater
CSmaller or equal
D10
Attempts:
2 left
💡 Hint
Check the condition if (x > 5).
🧠 Conceptual
intermediate
1:30remaining
Why use conditional logic?
Why do programmers use conditional logic in their code?
ATo make the program run faster
BTo repeat the same code multiple times
CTo store data permanently
DTo make decisions and run different code based on conditions
Attempts:
2 left
💡 Hint
Think about how programs choose what to do next.
Predict Output
advanced
2:00remaining
Output with nested if-else
What will this C++ program print?
C++
#include <iostream>
int main() {
    int score = 75;
    if (score >= 90) {
        std::cout << "A" << std::endl;
    } else if (score >= 70) {
        std::cout << "B" << std::endl;
    } else {
        std::cout << "C" << std::endl;
    }
    return 0;
}
AA
BC
CB
DCompilation error
Attempts:
2 left
💡 Hint
Check which condition score = 75 satisfies.
🔧 Debug
advanced
2:30remaining
Find the error in conditional logic
This code is supposed to print "Positive" if number is greater than zero, but it does not compile. What is the error?
C++
#include <iostream>
int main() {
    int number = 5;
    if number > 0 {
        std::cout << "Positive" << std::endl;
    }
    return 0;
}
AMissing parentheses around condition in if statement
BMissing semicolon after if statement
CWrong variable name
DMissing return statement
Attempts:
2 left
💡 Hint
Check the syntax of the if statement.
🚀 Application
expert
3:00remaining
Predict the output of complex conditional logic
What is the output of this C++ program?
C++
#include <iostream>
int main() {
    int a = 3, b = 4, c = 5;
    if (a > b) {
        if (b > c) {
            std::cout << "X" << std::endl;
        } else {
            std::cout << "Y" << std::endl;
        }
    } else if (a + b > c) {
        std::cout << "Z" << std::endl;
    } else {
        std::cout << "W" << std::endl;
    }
    return 0;
}
AZ
BW
CY
DX
Attempts:
2 left
💡 Hint
Check each condition step by step with a=3, b=4, c=5.