How to show "if" condition on a sequence diagram?
Asked Answered
D

4

288

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.

Desberg answered 13/11, 2011 at 21:11 Comment(1)
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?Googolplex
F
404

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

Fraternity answered 13/11, 2011 at 21:15 Comment(9)
In Visio, is Alt called something else? I can't seem to find itDesberg
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) softwarestencils.com/umlFraternity
Are there any other UML tools apart from Visio. Is enterprise architect good enough?Urban
@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 goodFraternity
@Venkat I'm personally a fan of visual paradigm. (visual-paradigm.com)Colicweed
In Astah this is called combinedFragment alt. Look for an icon between Stop and InteractionUseMerow
@Jam There is an Alternative fragment in Visio 2013Shellback
draw.io and Google Drawings offer free UML drawing tools from what I can see.Pipe
Late and maybe not what everybody wants, but I just discovered the joys of plantuml (plantuml.com). It's a UML markup language which can automatically be rendered as UML diagrams. Way faster and more maintable than drawing diagrams by hand. There is a free online editor if you want to try it. There is also an IntelliJ plugin which seamlessly renders the diagrams within a markdown document, and a GitHub action (github.com/marketplace/actions/generate-plantuml) which allows viewing it there too.Keese
D
49

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

Diploid answered 21/11, 2017 at 0:18 Comment(8)
@Gholamali-Irani have you tried the Chrome extension?Diploid
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.Lazes
@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.Diploid
Sure, here is the code : wandbox.org/permlink/PURdGJc8YxJmpUh7 -- there are three calls reuired for all the condition to be met. UML Sequence should show three calls, not one.Lazes
Of course, Activity diagrams are better to show algorithm internals/Lazes
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.Lazes
imgur.com/a/7OL26FJ like this? SO should have a DM functionality :)Diploid
Let us continue this discussion in chat.Lazes
T
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!

Trophic answered 6/6, 2020 at 10:12 Comment(1)
See https://plantuml.com/sequence-diagram, Grouping message section.Bozarth
E
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

Empressement answered 27/6, 2018 at 19:44 Comment(2)
Just as an heads up, the UML designer has been removed from VS 2017 onwards. learn.microsoft.com/en-gb/visualstudio/modeling/…Duckworth
To clarify, UML is still in Visio - just not in Visual Studio. support.microsoft.com/en-us/office/…Painstaking

© 2022 - 2024 — McMap. All rights reserved.