lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


----- Original Message ----- 
From: "Bilyk, Alex" <ABilyk@maxis.com>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Wednesday, June 18, 2003 2:22 AM
Subject: RE: why no "continue" statement for loops?


Funny enough, it is easier to simulate "break" with "continue" then the
other way around. Assuming there was no 'break' but 'continue' instead, one
could do something like
----- -----

It is possible to optimize Nick's version so it looks similar to yours.
In fact I use a little trick to change the meaning of "break" to "continue"
;)


local break_flag = false
while not break_flag and some_condition do
    while true do
        -- do this for a continue
        break

        -- do this for a "real" break
        break_flag = true; break

        -- at the end of the inner loop always do a break,
        -- so its executed only once
    break; end
end


You can replace "break_flag" by a "continune_flag" so you don't need a "not"
in the outer loop.
Simply switch the true/false at the "break_flag" assignments, too.

I always use this trick in languages where no "continue" exists but I need
it for a loop.
I think it is a pretty simple solution and the inner loop should not slow
down too much (if it at all), since its only executed once each time.