Calculator Community > Lua

Reducing the number of If statements

(1/2) > >>

Roondak:
So I was programming a function that looked like this:


function name(input)
    if input==1 then
        code
    elseif input==2 then
        code
    elseif input==3 then
        code
    ...
    end
end


Is there a better way to do this? Could I use some sort of table or something?

Keoni29:
Depends on the "code" in between really.

Streetwalrus:
In lua you can put functions in a table, though a cleaner way to do that would be the "case" structure if it exists in lua.

Roondak:

--- Quote from: Keoni29 on July 17, 2014, 05:39:46 pm ---Depends on the "code" in between really.

--- End quote ---
Here's an example from my code:

drawstr="Do you want to continue"
options={"Yes", "No"}
gamestate="choice"
timer.start(textspeed)

drawstr, options, and gamestate are all my own variables.


EDIT: I guess if I'm just assigning variables, I could have it just do that from a table.



--- Quote from: Streetwalrus on July 17, 2014, 05:40:00 pm ---In lua you can put functions in a table, though a cleaner way to do that would be the "case" structure if it exists in lua.

--- End quote ---
Some quick Google-Fu says that there isn't any case structure in Lua.

bb010g:
This may be helpful: http://lua-users.org/wiki/SwitchStatement

Navigation

[0] Message Index

[#] Next page

Go to full version