1.5.6 Representing if and if/else Statements in Activity Diagrams

Representing if and if/else Statements in Activity Diagrams

The Wildcats application chooses a message based on whether a player is available. This page represents a single if/else choice; the filename is retained for existing links.

Read the Code Before Drawing

This excerpt is inside an existing method with Boolean input isAvailable. The two assignments are the complete modeled work.

If the underlying C# feature is unfamiliar, use the lesson below while reading this example.

Source to translate
string message;

if (isAvailable)
{
    message = "Ready for practice";
}
else
{
    message = "Wait this round";
}
Rewrite the Meaning in Ordinary Words
Pseudocode for the same behavior
Ask whether the selected player is available
If true, store Ready for practice
Otherwise, store Wait this round
Continue after the one selected branch

Use these words to follow one execution before drawing it. Keep the same order and the same alternatives as the source; simplify the wording without adding work that the code does not perform.

Map the Source to Nodes and Arrows
Build the Diagram in Violet
  1. Create an activity diagram and connect start to Player available?.
  2. Place two rounded actions labeled Set message to Ready for practice and Set message to Wait this round.
  3. Draw true from the diamond to Ready for practice and false to Wait this round. Do not put an else question between the diamond and the false action.
  4. Join the two action exits at a merge diamond and connect it to the final node. Neither action should flow through the other.
  5. Save and trace both Boolean values. Each path stores exactly one message before leaving the conditional.
Compare the Completed Soccer Diagram

Compare this reference with the diagram you built in Violet. Layout may differ; the source meaning, labels, and relevant connections should agree.

Availability selects one of two practice messages, then the alternatives merge.
One if/else choice in diagram form
  • New
  • Changed
  • Removed
Trace One Route at a Time

True visits only the Ready assignment. False visits only the Wait assignment. If the source had no else, the false route would bypass the assignment and still continue after the if.

Verify the Drawing Against the Source
Predict and Check a Small Change