#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12

    if vs. switch memory


    Lets say the user is given 1-10 options to be taken to a function (each number going to a different function). To see what the user typed in, would it be more efficient to use if statements or a switch to redirect them? It won't make a huge deal in this situation, but lets say the program was huge, which one would be the better call? Thanks.

    -andy
    hmmm...
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    In this case I would suggest using switch
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    Of course a third option is having an array of function pointers and dereferencing an index based on the user's choice (after bounds checking, of course). This can handle huge ranges of possibilities in a few lines of code.

    Not sure which method is fastest, but the case structure is definitely the easiest to read...

IMN logo majestic logo threadwatch logo seochat tools logo