Calculator Community > Lua

Reducing the number of If statements

<< < (2/2)

Jonius7:
I think using a table would be quite efficient, it just depends on what variables/attributes you want to use for each case (if statement).
You can also name attributes of a table as well.
Here is some random example, you can possibly use a for loop, depending on the situation.


--- Code: ---
mtable = { {id = 1, message = "This is the first one", gamestate = "Mode1"},
{id = 2, message = "This is the second one", gamestate = "Mode2"},
{id = 3, message = "This is the third one", gamestate = "Mode3"}
}

function on.paint(gc)
  for g=1, #mtable do     --up to mtable length
    gc:drawString(mtable[g].message, 10, g*20, "top")
  end
end


--- End code ---

EDIT: Crap, over 3 month necropost!!! Just noticed.

Happybobjr:
I could be wrong, but you might be able to do something similar to axe and do:

--- Code: ---function name(input)
    input
    !if -1 then
        code
    else!if -1 then
        code
    else!if -1 then
        code
    ...
    end
end

--- End code ---

Navigation

[0] Message Index

[*] Previous page

Go to full version