2.1.3 Switch Statements

Choose a Soccer Drill with switch

The Wildcats application accepts a drill name. The text Pass selects a passing instruction and Shoot selects a shooting instruction. Any other text receives a fallback instruction.

These statements belong inside an existing method. The string input drill already contains the selected name. Each example chooses an instruction; it does not perform the drill.

Read the Selector and Cases
Choose one instruction by drill name
string instruction;

switch (drill)
{
    case "Pass":
        instruction = "Pass to a teammate";
        break;
    case "Shoot":
        instruction = "Shoot at the goal";
        break;
    default:
        instruction = "Choose a supported drill";
        break;
}
Select One Section

For Shoot, execution begins in the Shoot section instead. The Pass assignment is skipped. After the selected section ends, execution continues after the closing switch brace.

The default section handles a value with no matching case. String matching here is case sensitive: pass does not match Pass.

Try Matching and Unmatched Values
Drill selection
Input drill Selected section Stored instruction
Pass case Pass Pass to a teammate
Shoot case Shoot Shoot at the goal
pass default Choose a supported drill
Rest default Choose a supported drill
Check Your Prediction