When I was asked to write this post, an old story about loops came to mind… A few lines of dry theory first, though.

What is a loop?

We have a loop, when one or more operations or messages are carried out several times in a row. ‘Several’ could mean:

  • a given number of times;
  • until a condition is met;
    or
  • indefinitely.

The diagram below shows a loop with a condition.

It was back in university during the exam period. There were long nights of studying, reinforced by spirits and coffee in equal measures… During the spirit phase of one of these a friend of mine, let’s call him Poor Sod, encouraged by other friends, managed to change the BIOS password of his computer. Exactly why the decision was made probably falls into the ‘it made sense at the time’ category.

During the sobering up phase of the next morning however Poor Sod discovered he had no idea what he changed his password to…

A council was gathered briefly and everyone fired a random guess, based on partial memories from the night before. Thus three quarters of an hour passed in attempts to break the password. All to no avail.

Then somebody came up with the idea that “spirits giveth, spirit taketh away”. It was decided that Poor Sod & Co must obviously repeat the events of the previous night and get in the same state every evening, until the new password is recovered.

Breaking out of the loop

When the loop has a condition or when there is a special event, which needs to be attended to in some way (other than by the actions performed inside the loop), the loop has a break.

In our example a special event could be ‘run out of liquor’ or ‘access granted’.

Studying hard for an exam, I didn’t do my fair share of the password recall marathon, so the diagram above is only a guess. Strangely, none of the participants was too enthusiastic about telling how the story ended…

Moral: boys and girls, don’t do this at home.

You might have noticed a new thing that I sneakily put in both diagrams – it is called self-call and is used when a participant in a sequence diagram passes a message to themselves. For more details on calls and messages have a look at Manuel’s post, called Messages in UML 2.0 Sequence Diagrams – “I’ll call you…”

Definitions

  • UML – Unified Modelling Language
  • UML Sequence Diagram – a diagram, which describes a scenario in the behaviour of groups of collaborating objects
  • loop – “A loop is a sequence of statements which is specified once but which may be carried out several times in succession. The code “inside” the loop is obeyed a specified number of times, or once for each of a collection of items, or until some condition is met, or indefinitely.” (“Control flow,” Wikipedia, The Free Encyclopedia. Wikimedia Foundation, Inc. 26 Nov. 2011. ).

Resources

As much as I would like to take credit for the knowledge in this article, all the UML stuff comes from:

The cool cartoons are courtesy of zlotence.

About the Author