syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
In Lua how to case-switch?

In Ruby I can use case-switch-end to walk down a conditional tree to a single branch.

Once I find the first 'true-branch', I follow the branch to its end and then I exit the tree.

Lua offers this feature using 'elseif' inside of if-then-else-end:
-- ~/ks/b/lua/elseif.lua

-- Lua does not offer a case-switch syntax.
-- Instead I should use if then, elseif, else, end

if 1>2 then
  print('1>2')
elseif 1>3 then
  print('1>3')
elseif 1>0 then
  print('1>0')
elseif 1>-1 then
  print('1>-1')
else
  print('1 == 1')
end


Screen Dump:

dan@feb ~/ks/b/lua $ 
dan@feb ~/ks/b/lua $ 
dan@feb ~/ks/b/lua $ lua elseif.lua
1>0
dan@feb ~/ks/b/lua $ 
dan@feb ~/ks/b/lua $ 


syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me