Bird
Raised Fist0
C++programming~20 mins

Switch vs if comparison in C++ - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Switch vs If Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of switch with fall-through
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int x = 2;
    switch (x) {
        case 1:
            std::cout << "One ";
        case 2:
            std::cout << "Two ";
        case 3:
            std::cout << "Three ";
            break;
        default:
            std::cout << "Default ";
    }
    return 0;
}
ATwo Three
BOne Two Three
CThree
DDefault
Attempts:
2 left
💡 Hint
Remember that switch cases fall through unless you use break.
❓ Predict Output
intermediate
2:00remaining
If-else chain output
What is the output of this C++ code using if-else?
C++
#include <iostream>
int main() {
    int x = 2;
    if (x == 1) {
        std::cout << "One ";
    } else if (x == 2) {
        std::cout << "Two ";
    } else if (x == 3) {
        std::cout << "Three ";
    } else {
        std::cout << "Default ";
    }
    return 0;
}
ATwo
BOne Two
CTwo Three
DDefault
Attempts:
2 left
💡 Hint
If-else stops checking after the first true condition.
🧠 Conceptual
advanced
2:00remaining
Switch statement limitations
Which of the following is NOT a limitation of the C++ switch statement compared to if-else?
ASwitch only works with integral or enum types, not with strings or floats.
BSwitch cannot evaluate complex conditions like ranges or multiple variables.
CSwitch cases must be constant expressions known at compile time.
DSwitch statements always execute all cases regardless of matching.
Attempts:
2 left
💡 Hint
Think about how switch chooses which case to run.
❓ Predict Output
advanced
2:00remaining
Output difference between switch and if
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int x = 5;
    switch (x) {
        case 1:
            std::cout << "One ";
            break;
        case 2:
            std::cout << "Two ";
            break;
        default:
            std::cout << "Default switch ";
    }
    if (x == 1) {
        std::cout << "One if ";
    } else if (x == 2) {
        std::cout << "Two if ";
    } else {
        std::cout << "Default if ";
    }
    return 0;
}
ADefault if
BDefault switch
CDefault switch Default if
DOne if
Attempts:
2 left
💡 Hint
Both switch and if-else handle default cases here.
🧠 Conceptual
expert
2:00remaining
When to prefer switch over if-else
Which scenario best justifies using a switch statement instead of if-else in C++?
AWhen you need to check multiple unrelated conditions involving different variables and complex expressions.
BWhen you have a single variable with many possible constant integral values to handle distinctly.
CWhen you want to evaluate ranges or conditions involving inequalities.
DWhen you want to execute code based on string comparisons.
Attempts:
2 left
💡 Hint
Switch works best with one variable and fixed values.

Practice

(1/5)
1. Which statement best describes when to use switch instead of if in C++?
easy
A. switch can handle complex conditions like ranges.
B. if cannot be used with multiple conditions.
C. if is only for checking equality.
D. switch is best for many fixed, discrete values.

Solution

  1. Step 1: Understand switch usage

    switch works well when you have many fixed values to compare, like numbers or characters.
  2. Step 2: Compare with if

    if can handle any condition, including ranges or complex logic, but switch is simpler for fixed choices.
  3. Final Answer:

    switch is best for many fixed, discrete values. -> Option D
  4. Quick Check:

    Use switch for fixed values = B [OK]
Hint: Use switch for fixed values, if for complex conditions [OK]
Common Mistakes:
  • Thinking switch can handle ranges
  • Believing if is only for equality
  • Assuming if cannot handle multiple conditions
2. Which of the following is the correct syntax for a switch statement in C++?
easy
A. switch variable { case 1: doSomething(); break; default: doDefault(); }
B. switch (variable) { case 1: doSomething(); break; default: doDefault(); }
C. switch (variable) do { case 1: doSomething(); break; }
D. switch (variable) { if case 1: doSomething(); }

Solution

  1. Step 1: Recall correct switch syntax

    The switch statement requires parentheses around the variable and curly braces enclosing cases.
  2. Step 2: Check each option

    switch (variable) { case 1: doSomething(); break; default: doDefault(); } uses correct syntax with parentheses, cases, and breaks. Others miss parentheses, use wrong keywords, or incorrect structure.
  3. Final Answer:

    switch (variable) { case 1: doSomething(); break; default: doDefault(); } -> Option B
  4. Quick Check:

    Parentheses and braces needed in switch = A [OK]
Hint: Remember switch(variable) with braces and cases [OK]
Common Mistakes:
  • Omitting parentheses around variable
  • Using 'if' inside switch incorrectly
  • Missing curly braces for cases
3. What will be the output of this C++ code?
int x = 2;
switch (x) {
  case 1:
    std::cout << "One";
    break;
  case 2:
    std::cout << "Two";
  case 3:
    std::cout << "Three";
    break;
  default:
    std::cout << "Other";
}
medium
A. TwoThree
B. Two
C. Three
D. Other

Solution

  1. Step 1: Identify matching case

    The variable x is 2, so the switch jumps to case 2.
  2. Step 2: Trace output and breaks

    It prints "Two" but no break after case 2, so it continues to case 3 and prints "Three" before breaking.
  3. Final Answer:

    TwoThree -> Option A
  4. Quick Check:

    Missing break causes fall-through = TwoThree [OK]
Hint: Remember missing break causes fall-through in switch [OK]
Common Mistakes:
  • Assuming only case 2 prints
  • Forgetting fall-through behavior
  • Confusing default case execution
4. Identify the error in this code snippet:
int n = 3;
switch (n) {
  case 1:
    std::cout << "One";
  case 2:
    std::cout << "Two";
    break;
  default:
    std::cout << "Default";
}
medium
A. Default case must be last.
B. Switch variable must be a string.
C. Missing break after case 1 causes fall-through.
D. Case labels must be in ascending order.

Solution

  1. Step 1: Check switch cases and breaks

    Case 1 lacks a break, so if n == 1, it will fall through to case 2 and print both outputs.
  2. Step 2: Verify other rules

    Switch variable can be int, default can be anywhere, and case order does not matter.
  3. Final Answer:

    Missing break after case 1 causes fall-through. -> Option C
  4. Quick Check:

    Break needed to prevent fall-through = A [OK]
Hint: Always add break after each case unless fall-through wanted [OK]
Common Mistakes:
  • Thinking switch variable must be string
  • Believing default must be last
  • Assuming case order matters
5. You want to check if an integer variable score is between 90 and 100 (inclusive) to print "Excellent", between 75 and 89 to print "Good", and otherwise print "Try again". Which approach is better and why?
hard
A. Use if because switch cannot handle ranges.
B. Use if only for equality checks.
C. Use switch with multiple cases for each number in the range.
D. Use switch because it is simpler for ranges.

Solution

  1. Step 1: Understand the problem with ranges

    The conditions check if score falls within ranges, not fixed values.
  2. Step 2: Compare switch and if suitability

    switch cannot directly handle ranges; if can use relational operators to check ranges easily.
  3. Final Answer:

    Use if because switch cannot handle ranges. -> Option A
  4. Quick Check:

    Ranges need if, not switch = D [OK]
Hint: Use if for ranges, switch only for fixed values [OK]
Common Mistakes:
  • Trying to use switch for ranges
  • Using switch with many case labels for ranges
  • Thinking if only checks equality
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