Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I like to draw a UML sequence diagram to display the sequence of actions and interactions of different parts of my system (I think they called actors).

However, I need to include if then else statements here. Do I do something wrong? Can I use conditional if statements in my UML sequence diagram? How?

share|improve this question
up vote 14 down vote accepted

As in many types of UML diagrams, you can use guards to denote a condition. A guard is a boolean expression that you can write over a transition.

Here's a picture with an example: enter image description here

Notice the "[pastDueBalance = 0]" condition.

You can also use combined fragments for more complicated conditional logic. See this nice msdn article for further information.

share|improve this answer
Thanks for the article link! – quantme Apr 2 '13 at 20:32

There are four possibilities I know of (with increasing power):

  1. The easiest is just using guards, which are simple, side-effect free boolean expressions.
  2. With combined fragments (see e.g. page 54), you can group sets of messages together to show conditional flow in a sequence diagram (alternatives, options, loops)
  3. Much more powerful is the Object Constraint Language (OCL), which can additionally quantify (for all, exists) and offers a full fledged (DesignBy-)Contract language (that is nearly as expressive as full first order logic).
  4. The most powerful way is to express your constraint using a UML Action Language from Executable UML (xUML) that conforms to the UML Action Semantics.

My choice would be the least powerful one that is still sufficiently expressible for what you want to do. For instance, actions in 4. can have side-effects, which makes them much more fragile than OCL.

share|improve this answer

While sequence diagrams are perfect for your description, make sure to check Activity Diagrams.

When you want to show internal details (including detailed rules), Activity Diagrams is what you want to use. The plain activity diagrams focus on sequence of activities and rules and are poor in showing Actor responsibilities, nevertheless, you can use swim lanes to hi-light individual actor's responsibilities. Each lane would include activities an actor is responsible for.

These links discusses activity diagrams with swim lanes:

share|improve this answer

protected by gnat Mar 5 '15 at 20:40

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.