6

I was wondering, how can one represent "if cond1 else if cond2" statement on a sequence diagram?

    if (condition1) {
      // Do something
    } else if(condition2)
    {
      // Do something else if
    }

Im not sure if is it with two independent "Opt" clause

If possible create an image representation of a solution.

Christophe
  • 68,716
  • 7
  • 72
  • 138
Mauricio Pastorini
  • 762
  • 2
  • 11
  • 24
  • I know this is not helpful comment at all, but I have to advise against everything more complicated than a simple if/else in a sequence diagram. Personally, I wouldn't even use an if/else — just make 2 diagrams. – jim Jul 02 '16 at 21:09
  • Possible duplicate of [How to show "if" condition on a sequence diagram?](https://stackoverflow.com/questions/8114770/how-to-show-if-condition-on-a-sequence-diagram) – pringi Jul 12 '17 at 09:07

1 Answers1

5

In the sequence diagram, you can use a combined fragment with an alt operator. This allows you to show alternative behaviors:

  • Graphically, the alternatives are in tiled regions separated by dashed horizotal lines.
  • You can document the precise condition for each alternative in a guard (i.e. [condition]).

Example:
enter image description here

Additional reading:

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Thank you for the reply. I`ve considered an alt operator, the thing is that "Alt" is for literal "If else" and this situacion is not. So i think it must be a better choice. – Mauricio Pastorini Jul 03 '16 at 15:46
  • 1
    @MauricioPastorini The alt is for a choice between several alternatives not just an if else. The [else] guard is executed if none of all the other alternatives is suitable. So in your case, you would have **3 lanes** (separated by 2 dashed lines). The first would have a guard `[condition1]`, the second a guard `[condition2]` and the third an `[else]`. – Christophe Jul 03 '16 at 15:56
  • 1
    Just to complete the previous comment the alt doesn't necessarily have to have an option that will be executed (i.e. all guards might resolve to false and that's absolutely fine). Section with [else] guard is optional. – Ister Jul 03 '16 at 18:31