[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Re[2]: why no "continue" statement for loops?
- From: "Nick Trout" <nick@...>
- Date: Thu, 19 Jun 2003 15:49:15 -0700
> Programming would be sometimes easier if it was a possibility
> to break or continue any of a few nested loops. For example:
>
> a = 0
> while a < 10 do
> a = a + 1
> if a == 5 then continue end
> b = 0
> while b < 10 do
> b = b + 1
> if b == 5 then continue end
>
> if a == 9 and b == 9 then
> break 2 -- break the `while a < 10' loop
> end
>
> if a == b then
> continue 2 -- next iteration of the `while
> a < 10' loop
> end
>
> print(a, b)
> end
> end
Don't forget you have nested functions as well now. There is nothing to
stop you using state tables which can passed between functions (and out
of the scope of the nesting function). The following does not replicate
the behaviour of the above example. You can get some complex flow
control using nested functions and return.
function foo()
local state = { a = 0, b = 0 }
local loop = function()
state.a = 0
while state.a < 10 do
if state.a == 5 then return end
io.write(state.a, " ")
state.a = state.a + 1
end
end
local loop2 = function()
while state.b < 10 do
loop()
state.b = state.b + 1
end
end
loop2()
end
foo()
--nick