277

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

if (somethingShouldBeDone) {
       // Do it
} else {
       // Do something else
}

Can it be represented at all? The thing is ... in my code, fair amount of conditions are checked to determine a variety of actions. If i am going to show the actions, I'd like to explicitly state that actions are caused by particular events.

If possible create an image representation of a solution.

James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • 3
    I know this question is old and I haven't done a search yet, but it made me wonder whether showing branching is even a good idea for sequence diagrams. I always thought the conditions for the sequence were explicitly described in the scenario and thus no branching took place during the sequence. Alternative paths were handled by describing a variant of the scenario which had its own sequence diagram. It's somewhat subjective so maybe I should post it on Meta or possibly Software Engineering? – Kelly S. French Apr 17 '19 at 14:20

4 Answers4

388

If else condition, also called alternatives in UML terms can indeed be represented in sequence diagrams. Here is a link where you can find some nice resources on the subject http://www.ibm.com/developerworks/rational/library/3101.html

branching with alt

John Saunders
  • 160,644
  • 26
  • 247
  • 397
GETah
  • 20,922
  • 7
  • 61
  • 103
  • 1
    In Visio, is `Alt` called something else? I can't seem to find it – James Raitsev Nov 13 '11 at 21:41
  • 13
    Visio in itself is not a UML modeling tool. If you want to use UML 2.0 then you need to find a good Visio Stencil that fully implements the 2.0 specifications. There are several free stencils available on the web, you might want to give some of them a try :) Here is one that looks good (did not try all the features though) http://softwarestencils.com/uml/ – GETah Nov 14 '11 at 09:09
  • Are there any other UML tools apart from Visio. Is enterprise architect good enough? – v3nM Dec 12 '13 at 14:15
  • 1
    @VenkatMadhav I can't really say. I've never used it to its full power but from the reading I found on google, it should be pretty good – GETah Dec 12 '13 at 18:51
  • @Venkat I'm personally a fan of visual paradigm. (http://www.visual-paradigm.com/) – Cody Poll Feb 12 '14 at 20:21
  • In Astah this is called combinedFragment alt. Look for an icon between Stop and InteractionUse – Vijay Kumar Nov 25 '14 at 17:26
  • 1
    @Jam There is an `Alternative fragment` in Visio 2013 – SMUsamaShah Jun 14 '15 at 01:01
  • [draw.io](https://www.draw.io/) and [Google Drawings](https://docs.google.com/drawings/) offer free UML drawing tools from what I can see. – Dib Aug 27 '19 at 13:59
45

If you paste

A.do() {
  if (condition1) {
   X.doSomething
  } else if (condition2) {
   Y.doSomethingElse
  } else {
   donotDoAnything
  }
}

onto https://app.zenuml.com. It will generate a diagram for you.If/else sequence diagram

Devs love ZenUML
  • 11,344
  • 8
  • 53
  • 67
  • @Gholamali-Irani have you tried the Chrome extension? – Devs love ZenUML Dec 23 '17 at 00:47
  • If it is `A.do(int condition)` -- If .. else ... else, can not all happen as a result of one call. Flow depends on the condition argument. It would be lovely if ZenUML could draw that. It would be three calls ordered in time. – Chef Gladiator Jun 28 '19 at 08:43
  • @ChefGladiator I'm not sure if I understand what you mean. Could you please add some pseudo code here? Or you can point me in the site. – Devs love ZenUML Jun 28 '19 at 08:47
  • Sure, here is the code : https://wandbox.org/permlink/PURdGJc8YxJmpUh7 -- there are three calls reuired for all the condition to be met. UML Sequence should show three calls, not one. – Chef Gladiator Jun 28 '19 at 08:55
  • Of course, Activity diagrams are better to show algorithm internals/ – Chef Gladiator Jun 28 '19 at 08:59
  • I think not :) You need to have three calls coming from the left, each call is making the program flow inside F different. One call one activity. – Chef Gladiator Jun 28 '19 at 11:46
  • https://imgur.com/a/7OL26FJ like this? SO should have a DM functionality :) – Devs love ZenUML Jun 28 '19 at 11:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195701/discussion-between-chef-gladiator-and-xiao-peng-zenuml-com). – Chef Gladiator Jun 28 '19 at 13:59
27

Very simple , using Alt fragment

Lets take an example of sequence diagram for an ATM machine.Let's say here you want

IF card inserted is valid then prompt "Enter Pin"....ELSE prompt "Invalid Pin"

Then here is the sequence diagram for the same

ATM machine sequence diagram

Hope this helps!

vishwampandya
  • 1,067
  • 11
  • 11
2

In Visual Studio UML sequence this can also be described as fragments which is nicely documented here: https://msdn.microsoft.com/en-us/library/dd465153.aspx

David Burg
  • 1,064
  • 12
  • 14
  • Just as an heads up, the UML designer has been removed from VS 2017 onwards. https://learn.microsoft.com/en-gb/visualstudio/modeling/what-s-new-for-design-in-visual-studio?view=vs-2017#uml-designers-have-been-removed – alelom Jul 15 '19 at 10:09
  • To clarify, UML is still in Visio - just not in Visual Studio. https://support.microsoft.com/en-us/office/uml-diagrams-in-visio-ca4e3ae9-d413-4c94-8a7a-38dac30cbed6 – David Gerding Dec 15 '20 at 21:24