Author Topic: Reducing the number of If statements  (Read 7810 times)

0 Members and 1 Guest are viewing this topic.

Offline Roondak

  • LV3 Member (Next: 100)
  • ***
  • Posts: 54
  • Rating: +4/-3
    • View Profile
Reducing the number of If statements
« on: July 17, 2014, 05:38:35 pm »
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?

Offline Keoni29

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2466
  • Rating: +291/-16
    • View Profile
    • My electronics projects at 8times8
Re: Reducing the number of If statements
« Reply #1 on: July 17, 2014, 05:39:46 pm »
Depends on the "code" in between really.
If you like my work: why not give me an internet?








Offline Streetwalrus

  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3821
  • Rating: +80/-8
    • View Profile
Re: Reducing the number of If statements
« Reply #2 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.

Offline Roondak

  • LV3 Member (Next: 100)
  • ***
  • Posts: 54
  • Rating: +4/-3
    • View Profile
Re: Reducing the number of If statements
« Reply #3 on: July 17, 2014, 05:44:30 pm »
Depends on the "code" in between really.
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.


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.
Some quick Google-Fu says that there isn't any case structure in Lua.
« Last Edit: July 17, 2014, 05:50:47 pm by Roondak »

Offline bb010g

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 428
  • Rating: +22/-1
  • I do stuff
    • View Profile
    • elsewhere on the net
Re: Reducing the number of If statements
« Reply #4 on: July 31, 2014, 01:16:38 pm »
Arch Linux user
Haskell newbie | Warming up to Lua | Being dragged into C++
Calculators: HP 50g, HP 35s, Casio Prizm, TI-Nspire CX CAS, HP 28s, HP Prime, Mathematica 9 (if that counts)
π: 3.14...; l: 108; i: 105; e: 101; l+i+e: 314
THE CAKE IS A LIE IS A PIE

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Reducing the number of If statements
« Reply #5 on: November 20, 2014, 04:52:55 am »
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: [Select]

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


EDIT: Crap, over 3 month necropost!!! Just noticed.
« Last Edit: November 20, 2014, 05:05:52 am by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Happybobjr

  • James Oldiges
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2325
  • Rating: +128/-20
  • Howdy :)
    • View Profile
Re: Reducing the number of If statements
« Reply #6 on: November 20, 2014, 05:06:01 am »
I could be wrong, but you might be able to do something similar to axe and do:
Code: [Select]
function name(input)
    input
    !if -1 then
        code
    else!if -1 then
        code
    else!if -1 then
        code
    ...
    end
end
School: East Central High School
 
Axe: 1.0.0
TI-84 +SE  ||| OS: 2.53 MP (patched) ||| Version: "M"
TI-Nspire    |||  Lent out, and never returned
____________________________________________________________