Bird
Raised Fist0
C++programming~20 mins

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

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

Practice

(1/5)
1. What does an if-else statement do in C++?
easy
A. It defines a function.
B. It repeats a block of code multiple times.
C. It chooses between two paths based on a condition.
D. It declares a variable.

Solution

  1. Step 1: Understand the purpose of if-else

    An if-else statement lets the program decide which block of code to run based on a condition.
  2. Step 2: Compare with other options

    Repeating code is done by loops, functions define reusable code, and variables store data, so these are not correct.
  3. Final Answer:

    It chooses between two paths based on a condition. -> Option C
  4. Quick Check:

    If-else = choose path [OK]
Hint: If condition true, run if-block; else run else-block [OK]
Common Mistakes:
  • Confusing if-else with loops
  • Thinking if-else defines functions
  • Mixing variable declaration with control flow
2. Which of the following is the correct syntax for an if-else statement in C++?
easy
A. if (x > 0) { cout << "Positive"; } else { cout << "Non-positive"; }
B. if x > 0 then cout << "Positive" else cout << "Non-positive"
C. if (x > 0) cout << "Positive" else cout << "Non-positive";
D. if x > 0 { cout << "Positive" } else { cout << "Non-positive" }

Solution

  1. Step 1: Check C++ if-else syntax rules

    C++ requires parentheses around the condition and braces around blocks for multiple statements.
  2. Step 2: Analyze each option

    if (x > 0) { cout << "Positive"; } else { cout << "Non-positive"; } uses correct parentheses and braces. Options A and B miss parentheses or use incorrect keywords. if (x > 0) cout << "Positive" else cout << "Non-positive"; misses semicolon after else block.
  3. Final Answer:

    if (x > 0) { cout << "Positive"; } else { cout << "Non-positive"; } -> Option A
  4. Quick Check:

    Correct syntax uses parentheses and braces [OK]
Hint: Use parentheses for condition and braces for blocks [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Missing braces for multiple statements
  • Using 'then' keyword (not in C++)
3. What will be the output of this code?
int x = 5;
if (x < 3) {
  cout << "Less";
} else {
  cout << "More or equal";
}
medium
A. Less
B. More or equal
C. No output
D. Compilation error

Solution

  1. Step 1: Evaluate the condition x < 3

    Variable x is 5, so 5 < 3 is false.
  2. Step 2: Determine which block runs

    Since condition is false, the else block runs, printing "More or equal".
  3. Final Answer:

    More or equal -> Option B
  4. Quick Check:

    Condition false -> else block output [OK]
Hint: Check condition truth to pick if or else output [OK]
Common Mistakes:
  • Assuming if-block runs when condition is false
  • Confusing output strings
  • Ignoring else block
4. Find the error in this code snippet:
int n = 10;
if n > 5 {
  cout << "Big";
} else {
  cout << "Small";
}
medium
A. Missing parentheses around condition
B. Missing semicolon after cout statements
C. Incorrect else keyword
D. Variable n not declared

Solution

  1. Step 1: Check if statement syntax

    C++ requires parentheses around the condition in if statements.
  2. Step 2: Identify the error

    The code uses if n > 5 without parentheses, which is invalid syntax.
  3. Final Answer:

    Missing parentheses around condition -> Option A
  4. Quick Check:

    if condition needs parentheses [OK]
Hint: Always put condition inside parentheses in if [OK]
Common Mistakes:
  • Omitting parentheses in if condition
  • Forgetting semicolons (though not the main error here)
  • Misreading else keyword
5. You want to write a program that prints "Even" if a number is divisible by 2, otherwise prints "Odd". Which code correctly uses if-else to do this?
hard
A. if (num % 2 = 0) { cout << "Even"; } else { cout << "Odd"; }
B. if (num / 2 == 0) { cout << "Even"; } else { cout << "Odd"; }
C. if (num % 2 != 0) { cout << "Even"; } else { cout << "Odd"; }
D. if (num % 2 == 0) { cout << "Even"; } else { cout << "Odd"; }

Solution

  1. Step 1: Understand divisibility by 2

    A number is divisible by 2 if remainder when divided by 2 is zero, i.e., num % 2 == 0.
  2. Step 2: Check each option's condition

    if (num % 2 == 0) { cout << "Even"; } else { cout << "Odd"; } correctly uses num % 2 == 0. if (num / 2 == 0) { cout << "Even"; } else { cout << "Odd"; } uses division instead of modulo. if (num % 2 = 0) { cout << "Even"; } else { cout << "Odd"; } uses assignment (=) instead of comparison (==). if (num % 2 != 0) { cout << "Even"; } else { cout << "Odd"; } reverses the logic.
  3. Final Answer:

    if (num % 2 == 0) { cout << "Even"; } else { cout << "Odd"; } -> Option D
  4. Quick Check:

    Use modulo == 0 for even check [OK]
Hint: Use modulo (%) and == 0 to check even numbers [OK]
Common Mistakes:
  • Using assignment (=) instead of comparison (==)
  • Using division (/) instead of modulo (%)
  • Reversing if and else blocks
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