Sequence diagrams: loop fragments
简介 / Introduction
Bulk exports, status polling, retrying a request — in a sequence diagram these all mean the same thing: a group of messages is executed repeatedly. PlantUML and Mermaid both draw this with the loop keyword, and the skeletons are nearly identical, but the gap shows up as soon as you want to say “repeat N times” or “keep going while a condition holds”. This article compares both with the same “bulk export report” scenario.
PlantUML version
1 | @startuml |
A PlantUML loop block starts on a loop line carrying the loop description and is closed by end; the messages inside are executed repeatedly. The description is purely a label for human readers — PlantUML does not use it to drive the actual iteration count, so the real count belongs in the label (e.g. loop per 100 rows). When you need “continue only while a condition holds”, PlantUML offers the while (conditional loop) and repeat (do-while) blocks instead.
Mermaid version
1 | sequenceDiagram |
Mermaid’s loop also opens with loop and closes with end, with the enclosed messages repeating. It supports only this one loop form: neither while nor repeat exists in Mermaid, and conditional branching has to be approximated with alt/else or critical/option. Mermaid also allows the description to be omitted (a bare loop), but then readers lose the semantics of the loop.
对比 / Side-by-side
| Feature | PlantUML | Mermaid |
|---|---|---|
| Loop block keyword | loop … end |
loop … end |
| Description | loop description on the same line |
loop description on the same line |
| Description optional | ✅ | ✅ |
| Label drives the real count | ❌ comment only | ❌ comment only |
| Conditional loop | while / repeat |
❌ not supported, emulate with alt |
| Other fragment types | alt opt par group break critical |
alt else opt par and loop critical option break rect |
| Nested loop fragments | ✅ supported | ✅ supported |
| Plain messages inside a fragment | ✅ | ✅ |
小结 / Wrap-up
loop is the most consistent fragment type across the two engines: loop to open, end to close, repeated messages inside, and a label that is documentation only. The real difference is conditional expression — PlantUML expresses “keep going while…” directly with while / repeat, while Mermaid has to approximate it with alt or critical. Mermaid’s exclusive rect and option, and PlantUML’s group and repeat, are also the easiest things to miss when migrating a diagram between engines, so check the target engine’s fragment list before writing.
- Title: Sequence diagrams: loop fragments
- Author: puml.online
- Created at : 2026-11-08 09:00:00
- Updated at : 2026-11-26 01:07:01
- Link: https://puml.online/blog/sequence-diagram-loop-fragment-en/
- License: This work is licensed under CC BY-NC-SA 4.0.