## Switch Statement?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
kerperlo
Prole
Posts: 9
Joined: Wed Dec 24, 2014 9:46 pm

### Switch Statement?

I am trying to figure out how to implement a switch statement.

I read this forum, but it still doesn't help me on how to make one.
viewtopic.php?t=9711

If anyone has any recommendations, please let me know.

Thank you.

HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Somewhere in Brazil
Contact:

### Re: Switch Statement?

You can do this as a simple if/else block, like so:

Code: Select all

local a = 3

if a == 1 then --case 1
print("a = 1")
elseif a == 2 then --case 2
print("a = 2")
elseif a == 3 then --case 3
print("a = 3")
else --default
print("No other option fits")
end

BUT you can also make it a function, to make things simpler for you (if you're too used to Java or similar):

Code: Select all

local a = 3
switch(a,
{1, print, {"a = 1"}},
{2, print, {"a = 2"}},
{3, print, {"a = 3"}},
{print, {"No other option fits"}})

function switch(...)
local args = {...}
local a = args[1] --Your actual value
for i = 2, #args-1 do --for every "case"
local v = args[i]
if a == v[1] then
v[2](unpack(v[3]))
return
end
end
args[#args][2](args[#args][3]) --"default" thing
end

Didn't work TOO much on this, but should work just fine. Here's how it works:
switch(number,
{case 1, function, arguments (in a table)},
{case 2, function, arguments (in a table)},
{case 3, function, arguments (in a table)},
{function, arguments (in a table)} --default
)

This can be done in many ways (without tables, automatically detecting no arguments or single arguments, etc.), but I just wanted to give a fast example. I'm pretty sure a more experienced coder can do something better. I hope it works for you
HugoBDesigner - my Blog

Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

### Re: Switch Statement?

Personally I've been using just if-elseifs. Up to my understanding there really isn't switch-case in lua, but I suppose if and elseifs should be enough. I do miss it from time to time though, like checking keypress and especially when you have several different keypress doing same thing the fall through would be just using or.

c

Code: Select all

switch( key ) {
case 'q':
case 27: // escape in ascii
quit();
break;

case 'r':
restart();
break;

default:
doSomethingDefault();
break;
}

vs.
lua

Code: Select all

if key == 'q' or key == 'escape' then
quit()
elseif key == 'r' then
restart()
else
doSomethingDefault()
end

Edit: Changed the escape in c-language to 27 instead of 'escape'.

Edit2: Sometimes I have needed to have a simple from a <-> b thing, not fully related to switch case but something like. Lets say that I would want to use ascii graphics and then for some reason save the asciis in different values and load them back, I have been using something like:

Code: Select all

local convertTable = {
1 = '#',
2 = '@',
'#' = 1,
'@' = 2
}

print( convertTable[1] ) -- prints #
print( convertTable['#'] ) -- prints 1

I guess this is more of a special case + if you happen to have invalid values, the program might crash. Also I am not sure if this is exactly better than using if and elseifs. You can also put functions in table and call them, but I suppose the program might just get really unneccessary complex to read with jump tables.

kerperlo
Prole
Posts: 9
Joined: Wed Dec 24, 2014 9:46 pm

### Re: Switch Statement?

Thank you!

Big help

Positive07
Party member
Posts: 1006
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

### Re: Switch Statement?

Use tables

Code: Select all

switch = {
["a"] = function () --case "a":
--The code in case of a
end;
["b"] = function() --case "b":
--The code in case of b
end
}

switch["a"]() --The code in case that the switch is a

If you wanted to have a default value you could do:

Code: Select all

function myswitch (a)
if a and switch[a] then
return switch[a]()
else
return switch["defaul"]()
end
end

You could also implement the goto function, though it wont break, after executing it would go back to running the thing that called it

Code: Select all

function mygoto (a)
return switch[a]()
end

You could do some other neat things like if the function returns false you can execute the next entry in the table (for that you could use the lua function "next") but I wont get into it too much

The advantage of this approach is that you can easily reuse the switch over and over again
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

Party member
Posts: 135
Joined: Fri Oct 15, 2010 10:58 am

### Re: Switch Statement?

Lua-users has a pretty in depth look at lua switch.
http://lua-users.org/wiki/SwitchStatement

You probably don't need anything more then a table and if statements.
I use Workflowy but you can check out Dynalist as its the better offer.

### Who is online

Users browsing this forum: Google [Bot] and 19 guests