Is there a way to do an early exit from a loop

+6 votes
asked Feb 8, 2016 in Wanted features by anonymous

It would be great if it was possible to do an early exit out of a loop in activity diagrams (new syntax). There is an early version of goto, but it only works backwards, so it is not so useful for this one.

Something along the lines of (this does not work yet):

:loop over all instances to find a free one
while (instance exists?)
if (free?) then (yes)
:reserve this instance;
break
endif
:get next instance;
endwhile

Regards, Ville

 

1 Answer

0 votes
answered Nov 13, 2020 by The-Lu (64,340 points)

Hello V.,

Just to answer to an old unanswered question (and confirm that is now OK);
from 'break' feature creation:

Then here is the result of your code:


[See on PlantUML server]


[Just to answer to an old unanswered question (and confirm that is now OK)]
Regards,
Th.

commented Nov 13, 2020 by anonymous
Hi The-Lu,

The result does not look very pleasing to the eye, even though it is technically correct.

If the "get the next instance" would be parallel to the "reserve this instance" the result would look much easier to understand. I wonder if one could do this by some alterations to the source code.

Regards, Ville
commented Nov 13, 2020 by The-Lu (64,340 points)

Hello Ville,

Yes... for that:

  • Put just the action ":get next instance;" on a else branch on the if:


[See on PlantUML server]

If that can help,
Regards,
Th.

...