If you've ever looked at a UML sequence diagram and felt confused by those nested boxes labeled loop, alt, or opt, you're not alone. These combined fragments are the backbone of showing conditional and repetitive behavior between objects. Understanding the sequence diagram loop alt opt notation means the difference between a diagram that communicates clearly and one that confuses everyone who reads it.

What are combined fragments in a sequence diagram?

Combined fragments are rectangular boxes drawn around a portion of messages in a sequence diagram. They define the logic conditionals, loops, options that govern how and when those messages get exchanged. The notation and symbols used in sequence diagrams include these fragments as a core part of the UML specification.

Each combined fragment has an interaction operator shown in a small tab at the top-left corner of the box. The three most commonly used operators are:

  • loop repeats a set of messages
  • alt shows alternative paths (like if/else)
  • opt shows an optional path (like if with no else)

How does the loop fragment work?

The loop fragment wraps a group of messages that repeat. You write the loop condition inside square brackets right after the keyword. For example:

loop [items remaining]

This means the messages inside the box keep executing as long as items remaining is true. You can also add a iteration bounds notation like loop [1..n] to indicate the minimum and maximum number of times the loop runs.

Practical example: An e-commerce system processing each item in a shopping cart. The loop fragment wraps the sequence where the Order Service sends a "check stock" message to the Inventory Service for every item in the cart. Without the loop notation, you'd have to draw the same pair of messages once for each item which is messy and impractical.

Loop with iteration bounds

You can specify minimum and maximum iterations like this:

  • loop [5] exactly 5 times
  • loop [1..10] between 1 and 10 times
  • loop [] zero or more times
  • loop [0..] same as above, zero or more

How does the alt fragment work?

The alt fragment represents mutually exclusive alternatives it works like an if/else statement. Inside the box, you draw a horizontal dashed line to separate the two (or more) alternatives. Each section has its own guard condition in square brackets.

Practical example: A user login system. The alt fragment has two sections:

  1. [credentials valid] the system sends a success response
  2. [else] the system sends an error message

Only one path executes at runtime. The [else] keyword is the catch-all when no other guard condition is met. This is one of the most used combined fragment patterns in sequence diagrams.

How does the opt fragment work?

The opt fragment shows a single optional path. It's simpler than alt because there's no alternative the behavior either happens or it doesn't. Think of it as an if statement without an else.

Practical example: A notification system where the application sends a push notification only if the user has notifications enabled:

[notifications enabled]

If the condition is false, nothing happens. No alternative path, no fallback. The interaction simply continues past the opt fragment.

When should you use loop vs. alt vs. opt?

Choosing the right fragment depends on the behavior you're modeling:

  • Use loop when the same interaction repeats based on a condition or count
  • Use alt when two or more mutually exclusive paths exist
  • Use opt when something might happen but there's no fallback

Here's a quick decision rule: if you find yourself writing "do this for each..." use loop. If you're writing "if X then A, otherwise B," use alt. If you're writing "if X then A, otherwise do nothing," use opt.

What do these fragments look like in PlantUML?

If you're using a text-based tool to create diagrams, the PlantUML sequence diagram syntax makes writing these fragments straightforward:

  • loop ... end wraps repeating messages
  • alt ... else ... end separates alternative paths
  • opt ... end wraps an optional section

This is useful when you need to version-control your diagrams or generate them automatically as part of your documentation pipeline.

What common mistakes do people make with these notations?

1. Using alt when opt is enough. If one branch just does nothing, don't add an empty [else] section. Use opt instead. It's cleaner and easier to read.

2. Forgetting the guard condition. Every fragment should have its condition in square brackets. A loop without a condition is ambiguous will it run once? Forever? Always specify it.

3. Nesting too deep. You can put a loop inside an alt inside an opt, but three or more levels of nesting makes the diagram hard to follow. If your diagram needs that much nesting, consider breaking it into separate diagrams.

4. Drawing the wrong number of compartments in alt. The alt fragment needs at least two sections separated by dashed lines. One section with a guard, and at least one more with either another guard or [else].

5. Confusing ref with loop. The ref fragment refers to another defined interaction. The loop fragment repeats the messages within it. They serve different purposes even though both deal with repetition at the diagram level.

Can you combine these fragments together?

Yes, and this is common in real-world models. You might have an alt fragment where one branch contains a loop. For example:

  • An alt fragment that checks [user authenticated]
  • In the true branch, a loop fragment that iterates through [pending tasks] and processes each one
  • In the else branch, an opt fragment that sends a [retry count < 3] login retry message

This kind of nesting is valid UML, but keep your audience in mind. If your team struggles with complex diagrams, consider annotating the diagram with plain-language comments or linking to a text explanation. You can find more on the official UML specification at omg.org for the full interaction fragment semantics.

Quick checklist: Is your fragment notation correct?

  • Does every loop have a guard condition in brackets?
  • Does every alt have at least two compartments separated by dashed lines?
  • Does the opt fragment have a single clear guard condition?
  • Are your fragments drawn tightly around only the relevant messages?
  • Is nesting kept to two levels or fewer?
  • Did you label alt's fallback branch with [else]?
  • Did you specify iteration bounds on loops when the exact count matters?
  • Is the diagram readable without needing to squint at overlapping boxes?

Next step: Take a real feature from your current project user signup, order checkout, or API authentication and model it using all three fragments: loop for repeated calls, alt for success/failure paths, and opt for optional behaviors. Sketch it on paper first, then transfer it to a tool like PlantUML to validate your syntax.