1.5.6 Representing if and if/else Statements in Activity Diagrams

Overview

An activity diagram shows what an application does and which path it follows. A decision node is a diamond with one incoming flow and outgoing flows for possible results. A simple if/else becomes one decision with a true path and a false path.

Imagine an airline employee checking whether a Flight object has any seats left:

if (availableSeats > 0)
{
    status = "Seats available";
}
else
{
    status = "Flight full";
}

DisplayStatus(status);

The condition is evaluated once. One action sets status, and both paths reach DisplayStatus(status) afterward.

Obtain the seat count, choose an available or full status, merge the paths, and display the status.
An if/else Activity Diagram
  • New
  • Changed
  • Removed

Start with an initial node and the action that obtains availableSeats. Place a decision node labeled availableSeats > 0? after that action. Label one outgoing flow Yes and lead it to the action Set status to "Seats available". If there is no else in the code, the No flow skips that action and proceeds to the next shared activity.

The diagram's branches should reflect the actual behavior: a false result does not run the true action.

Follow Both Diagram Paths

For the code above, lead the No flow to Set status to "Flight full". Rejoin the two paths before Display status, and put the activity's final node after that display action. The join here is a merge node: it brings alternative flows back to one continuation.

Match code and diagram
Code element Diagram element
availableSeats > 0 Condition on the decision node
Statements inside if Action along the Yes flow
Statements inside else Action along the No flow
DisplayStatus(status) Shared action after the paths merge

When availableSeats is 3, take Yes, set the available status, merge, and display it. When availableSeats is 0, take No, set the full status, merge, and display it. Both paths reach the same display action, but only one status-setting action executes during each run.

Follow the arrows from the decision to see which actions can run for a given value. The diagram and the C# code should agree on both the chosen action and the point where execution continues.