lua-users home
lua-l archive

Re: How to break out of nested loops

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


On 8/23/06, Luis Carvalho <carvalho@dam.brown.edu> wrote:
> I found myself using this trick very often but I have no idea what
> price in terms of performance and memory consumption it costs
> (creating closures just for a single use, etc). Is it a good idiom and
> are there any alternatives??
You can also use a 'do' block:
--breaking from nested loops
t={ {0,1,2,3,4}, {10,11,-12,13,14} } -- two dimensional array
do
 for i=1,#t do
 for j=1,#t[i] do
 if t[i][j]<0 then return end --break out of all loops if negative
 print(i,j,t[i][j])
 end
 end
end
It might be not be cheaper, but it looks better IMHO. :)
Cheers,
Luis.
Your suggestion simply does not work. do...end construct does not
affect "return" statement. The entire calling function that encloses
do...end will be terminated.
--Leo--

AltStyle によって変換されたページ (->オリジナル) /