Elegant breaking from nested loops

In that discussion they arrive at the same discussion we have here. Basically, there’s no such thing as goto in Nim, and according to a member there shouldn’t be:

Nim’s optimizer does not understand control flow hiding in emit and is free to break your program. Instead of goto, use structured programming constructs like if and while (there is also return and break).

Then, another member proposed the use blocks, which allow for a directed break. This is what you need in your example, and differs significantly from the liberal goto.

Now, I went ahead and tried two things. First, I use a Proc to be able to return from it. This is in essence what a Nim block would do (but my solution doesn’t allow for multiple breaking points in different nested loops; only works for this case). Then, I implemented the throw/catch solution with an empty callstack as suggested by Ary. To my surprise, they’re equivalent in speed.

BTW, what takes 64s? Your example is immediate to me when calling sozpg(541).

3 Likes