lua-users home
lua-l archive

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


>>local flag = false
>>if i == 1 or i == 2 then
>>   if i == 1 then
>>     flag = true
>>   end
>>   print(flag)
>>else
>>   print"Unknown!"
>>end
>
>But now you check for the condition twice.

Just do this:

if i == 2 then
  goto label
elseif i == 1 then
  code()
label:
  more_code()
end

The above pattern can be easily extended to handle an arbitrary number
of cases. Fallthrough can be nice but it's so rarely useful it's hard
to justify adding a new language feature to handle it when the "same"
performance effects (but conditions are now checked in reverse) can be
achieved thusly with goto.