Forum
Exit from more than one level - Printable Version

+- Forum (https://www.boriel.com/forum)
+-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11)
+---- Forum: Wishlist (https://www.boriel.com/forum/forumdisplay.php?fid=14)
+---- Thread: Exit from more than one level (/showthread.php?tid=2623)



Exit from more than one level - Zoran - 04-18-2025

Sometimes we need exit not only from innermost loop, but also from some outer loop (sometimes more than two levels).

As a workaround I use "do" in the outer loop and "while 1" in the inner, just to be able to use "exit do" or "exit while":

Code:
do
   ' some code
   while 1
      ' some code

      if somecondition then
         exit do ' exit from two levels
      end if

      ' some code...
      if someothercondition then
         exit while ' exit inner loop only
      end if

      ' some code...
   end while

    ' some code...
loop

When exit from more than two levels is needed, there is no other way but to put a label and use "goto". It would be much more elegant if we could exit from more levels directly. For example, "exit do do" or "exit do do do" would exit from two or three levels of "do". I'm not sure that "exit for for for" or "exit while while" is needed; supporting "do" loop only is probably quite enough -- "do" loop is the most powerful and you can easily write any loop as a "do" loop.

What do you think?


RE: Exit from more than one level - boriel - 04-21-2025

That's a common problem in most languages indeed. This feature is not yet implemented.
Will keep you posted.


RE: Exit from more than one level - Zoran - 04-21-2025

boriel Wrote:That's a common problem in most languages indeed. This feature is not yet implemented.
Will keep you posted.

Thank you!


RE: Exit from more than one level - Duefectu - 04-23-2025

GOTO is our friend in some cases!!!