Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting a number instead of the variable inside switch parentheses.
Writing 'switch' or 'case' inside the parentheses.
✗ Incorrect
The switch statement requires the variable or expression to check inside parentheses. Here, it should be
choice.2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name instead of a constant value.
Writing 'default' as a case label.
✗ Incorrect
The case label must be the constant value to match. Here, it is 2.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting break causes fall-through.
Using continue or return incorrectly inside switch.
✗ Incorrect
The break statement is needed to exit the switch after a case is handled to avoid falling through.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using default instead of case labels.
Not using break after the shared cases.
✗ Incorrect
Cases 1 and 2 both print "Low" by falling through to the same code block.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers instead of default for the last case.
Omitting break statements.
✗ Incorrect
Cases 1 and 2 print Monday and Tuesday, and default handles all other values.