[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: why no "continue" statement for loops?
- From: lb@...
- Date: Tue, 17 Jun 2003 23:23:18 +0200
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
Attachment:
continue.tar.gz
Description: GNU Zip compressed data