Search code examples
umlgrammarsequence-diagram

How do you represent a do while control structure in a sequence diagram?


The only solution I can think of is creating a variable before the loop so it can enter the first time. However, I don't consider this as optimal.


Solution

  • In an interaction (shown through a sequence diagram) the natural way to support a loop is to use a combined fragment with the operand loop.

    As said in formal/2017-12-05 §17.6.3.17 Loop (from page 584) :

    The Guard may include a lower and an upper number of iterations of the loop as well as a Boolean expression. The semantics is such that a loop will iterate minimum the ‘minint’ number of times (given by the iteration expression in the guard) and at most the ‘maxint’ number of times. After the minimum number of iterations have executed and the Boolean expression is false the loop will terminate.

    Contrarily to a while in case of a do-while the test is at the end of the loop, an other way to say is the test (without side effect) is done at the beginning of the loop but its result is not taken into account the first time, and this is exactly the semantic of the combined fragment loop with ‘minint’ valuing 1 (but 0 in case of a while) and ‘maxint’ valuing * (means unlimited, see §17.6.4.9 page 586) => the notation for the loop operand is loop(1,*) and the Boolean expression is the test of the while.