0
0
C++programming~10 mins

Switch statement in C++ - Interactive Code Practice

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

Complete the code to start a switch statement on variable choice.

C++
switch([1]) {
    case 1:
        // code
        break;
    default:
        // code
}
Drag options to blanks, or click blank then click option'
Achoice
B1
Cswitch
Dcase
Attempts:
3 left
💡 Hint
Common Mistakes
Putting a number instead of the variable inside switch parentheses.
Writing 'switch' or 'case' inside the parentheses.
2fill in blank
medium

Complete the code to add a case for when choice equals 2.

C++
switch(choice) {
    case [1]:
        std::cout << "Choice is 2" << std::endl;
        break;
    default:
        std::cout << "Other choice" << std::endl;
}
Drag options to blanks, or click blank then click option'
A1
B2
Cchoice
Ddefault
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name instead of a constant value.
Writing 'default' as a case label.
3fill in blank
hard

Fix the error in the switch statement by completing the missing keyword.

C++
switch(choice) {
    case 1:
        std::cout << "One" << std::endl;
        [1];
    default:
        std::cout << "Default" << std::endl;
}
Drag options to blanks, or click blank then click option'
Abreak
Bcontinue
Creturn
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting break causes fall-through.
Using continue or return incorrectly inside switch.
4fill in blank
hard

Fill both blanks to create a switch that prints "Low" for 1 or 2, and "High" for 3.

C++
switch(choice) {
    case [1]:
    case [2]:
        std::cout << "Low" << std::endl;
        break;
    case 3:
        std::cout << "High" << std::endl;
        break;
}
Drag options to blanks, or click blank then click option'
A1
B2
C3
Ddefault
Attempts:
3 left
💡 Hint
Common Mistakes
Using default instead of case labels.
Not using break after the shared cases.
5fill in blank
hard

Fill all three blanks to complete a switch that prints the day name for 1, 2, or default.

C++
switch(day) {
    case [1]:
        std::cout << "Monday" << std::endl;
        break;
    case [2]:
        std::cout << "Tuesday" << std::endl;
        break;
    [3]:
        std::cout << "Other day" << std::endl;
}
Drag options to blanks, or click blank then click option'
A1
B2
Cdefault
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers instead of default for the last case.
Omitting break statements.