lua-users home
lua-l archive

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


You rock! 

I think I may adopt this as I couldn't figure out how to avoid ugly code with no 'continue' especially in cases past *trivial* ones. Granted, 'continue' is not used in every loop, but it is used often enough to warrant its presence in Lua. IMHO. "Switch' statement is not a big deal as it can be done in a variety of ways ('if' chain, case table, etc) none of which are ugly, if somewhat unrefined perhaps. But no 'continue' is really a bomber when the situation calls for it, as there is no good replacement for it as others had suggested.

Alex

 -----Original Message-----
From: 	lb@get.it.pl [mailto:lb@get.it.pl] 
Sent:	Tuesday, June 17, 2003 2:23 PM
To:	Lua list
Subject:	Re: why no "continue" statement for loops?

 << File: continue.tar.gz >> JL> Every time I write a little program I end up wanting a continue
JL> statement for loops. Does this exist already or is there an alternative 
JL> that I've missed? If not then how to people not write ugly
JL> "if this then ... if that then ... if other then ... end end end"
JL> code to serve the same purpose of just jumping back to the top of the loop?

Adding `continue' statement to Lua is quite simple. The attached file
contains modified Lua 5 files: lex.h, lex.c and lparser.c. Test code
is as follows (also in attached file):

--
-- `continue' statement test
--

i = 0
while i < 7 do
    i = i + 1
    if i == 3 then continue end
    if i == 6 then break end
    io.write(i)
end

io.write(" ")

i = 0
repeat
    i = i + 1
    if i == 3 then continue end
    io.write(i)
until i == 5

io.write(" ")

for i = 1, 5 do
    if i == 3 then continue end
    io.write(i)
end

print()

print(loadstring("if 2 > 1 then continue end"))
print(loadstring("do continue end"))

--
-- the result is:
--
-- 1245 1245 1245
-- nil  [string "if 2 > 1 then continue end"]:1: no loop to continue near `end'
-- nil  [string "do continue end"]:1: no loop to continue near `end'
 
Enjoy!
Leszek Buczkowski