January 21, 2021
Hot Topics:

A Visual Introduction to Using UML Sequence Diagrams for Refining Object Models

  • By Mark Grand
  • Send Email »
  • More Articles »

Guarded Repetition in a Sequence Diagram

You have seen how a sequence diagram represents calls to operations and indefinite repetitions of calls that are like infinite loops. Conditional repetitions can also be represented in a sequence diagram.

If an operation is to be conditionally called multiple times, similar to a call in a while loop, this can be captured by putting the condition in square brackets like this:


The preceding indicates that while listIsOpen is true, calls will continue to be made to the collectNextToll operation.

The UML terminology for one of these conditions in square brackets is called a guard. The guard in this example is very simple in that it consists of just a single condition. Guards can contain whatever comparisons and logical connectors are needed to express more complicated conditions.

It is also possible to indicate that an operation is to be called some particular number of times, which is similar to a for loop. The way this is done is to indicate in the guard that a variable is to be assigned to a range of values. The UML specification does not say how this must be done. Figure 3 shows a common way to write this.

Figure 3: Operation Repeated a Fixed Number of Times

Notice that the syntax used in Figure 3 specifies a variable in the guard to contain the values. The same variable is also used as a parameter to the logItem operation. This ability to associate variables with values allows a sequence diagram to represent enough data flow to make the structure of loops clear. Variables also can be used to make it clear how each operation gets visibility to objects.

Before moving on to the next topic, there are a couple of things to notice about the sequence diagram in Figure 3. The first thing is that it looks different than the diagram in Figure 1. This is because they were drawn with different tools.

The UML standard allows for some flexibility in the way that the elements of a sequence diagram are drawn. Different tools draw some elements differently and some even have tool-specific extensions.

The other thing to notice about the sequence diagram in Figure 3 is the punctuation in the names in the objects. The object on the right has two names separated by a colon (:). The name to the right of the colon, Logger, is the name of the class that the object is an instance of. The name to the left of the colon, SafetyLog, is a name that identifies the specific object within the sequence diagram. This name can be used in such places as guard expressions and parameters to operations.

Conditional Operations

In a sequence diagram, you can associate the result returned by an operation with a variable. This is often used with conditional operations, as shown in the example in Figure 4.

Figure 4: Set and Test the Value of a Variable

Figure 4 shows a number of new things you have not seen in previous sequence diagrams. The first thing that you look at in Figure 4 is a call to an operation whose result becomes associated with a variable.


This says the an operation named isCurrent is called, that the isCurrent operation returns a boolean result, and that the variable currentFlag gets the result value returned by the isCurrent operation.

The next interaction you look at is similar to an if statement. It consists of a call to an operation with a guard.


The call to grantPermission happens if, and only if, currentFlag is true.

Page 2 of 3

This article was originally published on December 23, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date