0
0
C++programming~10 mins

Switch vs if comparison in C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print "One" when the variable num is 1.

C++
int num = 1;
switch (num) {
    case [1]:
        std::cout << "One" << std::endl;
        break;
    default:
        std::cout << "Other" << std::endl;
}
Drag options to blanks, or click blank then click option'
A1
B2
C0
Ddefault
Attempts:
3 left
💡 Hint
Common Mistakes
Using a case label that does not match the variable value.
Using 'default' as a case label.
2fill in blank
medium

Complete the if statement to print "Two" when num equals 2.

C++
int num = 2;
if (num [1] 2) {
    std::cout << "Two" << std::endl;
} else {
    std::cout << "Not Two" << std::endl;
}
Drag options to blanks, or click blank then click option'
A!=
B<
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment operator '=' instead of comparison '=='.
Using inequality operator '!='.
3fill in blank
hard

Fix the error in the switch statement to correctly handle the variable grade.

C++
char grade = 'B';
switch (grade) {
    case [1]:
        std::cout << "Good" << std::endl;
        break;
    default:
        std::cout << "Other grade" << std::endl;
}
Drag options to blanks, or click blank then click option'
A"B"
B'B'
CB
D"b"
Attempts:
3 left
💡 Hint
Common Mistakes
Using double quotes instead of single quotes for characters.
Using unquoted letters.
4fill in blank
hard

Fill both blanks to create a map from numbers to words using if statements.

C++
int num = 3;
if (num [1] 1) {
    std::cout << "One" << std::endl;
} else if (num [2] 3) {
    std::cout << "Three" << std::endl;
} else {
    std::cout << "Other" << std::endl;
}
Drag options to blanks, or click blank then click option'
A==
B!=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using inequality operator '!=' instead of '=='.
Using greater or less than operators incorrectly.
5fill in blank
hard

Fill all three blanks to create a switch statement that prints the day of the week for 1, 2, and 3.

C++
int day = 2;
switch (day) {
    case [1]:
        std::cout << "Monday" << std::endl;
        break;
    case [2]:
        std::cout << "Tuesday" << std::endl;
        break;
    case [3]:
        std::cout << "Wednesday" << std::endl;
        break;
    default:
        std::cout << "Other day" << std::endl;
}
Drag options to blanks, or click blank then click option'
A1
B2
C3
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong numbers for case labels.
Missing break statements (though not asked here).