2.1.4 Representing Switch Statements in Activity Diagrams

Represent switch Statements in an Activity Diagram

The Wildcats drill selector chooses one instruction from a drill name. Translate the source into one selection with named routes, then check that the chosen route leaves the switch.

Read the Code Before Drawing

The code below uses the same soccer rule as the C# lesson. Read that lesson if the control statement is unfamiliar.

The string input drill already exists inside the method. This excerpt stores the chosen text in instruction.

Source to translate
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;
}
Rewrite the Execution in Ordinary Words
Pseudocode for the same behavior
Look at the drill name
    Pass: select Pass to a teammate, then leave the selection
    Shoot: select Shoot at the goal, then leave the selection
    Otherwise: select Choose a supported drill, then leave the selection
Continue after the selection

Pseudocode describes the behavior without requiring C# punctuation. Preserve the order, alternatives, and repetition when you simplify the wording. Draw the actions and questions; braces and semicolons do not become nodes.

Map Code to Nodes and Arrows

The switch selector becomes one decision diamond. Case values and default label its outgoing arrows; each assignment becomes an action on the corresponding route. The arrows leaving those actions represent the switch breaks.

Build the Diagram in Violet
  1. Connect the start node to a diamond labeled Drill?. Add outgoing arrows labeled Pass, Shoot, and default. Here default means every value with no named match, including pass and an empty string.
  2. Connect Pass to an action labeled Select Pass to a teammate. Connect Shoot to Select Shoot at the goal. Connect default to Select Choose a supported drill.
  3. Connect every action to one merge diamond, then connect that merge to the final node. These arrows represent leaving the selected section at break. No arrow goes from one instruction action into another. The switch breaks do not need separate action boxes in this drawing.
Compare the Completed Soccer Diagram
Select Pass, Shoot, or default from the drill name. The three alternatives merge before the activity ends.
Represent switch Statements in an Activity Diagram
  • New
  • Changed
  • Removed
Trace One Route at a Time

Trace Shoot from the selector to its instruction and then to the merge. Next trace lowercase pass: it follows default because it does not exactly match Pass. Each path visits only one instruction action.

Verify the Drawing Against the Code
Predict and Check a Route