too many "else if" statements

This is a discussion on too many "else if" statements within the C++ Programming forums, part of the General Programming Boards category; I've run into this situation a long time ago, and for the life of me I can't remember the answer. ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    55

    too many "else if" statements

    I've run into this situation a long time ago, and for the life of me I can't remember the answer.

    Within a function, what's the maximum number of "else if" statements you can use before they're no longer read?

    For example:

    Code:
    else if (SKILL >= 10 && SKILL <= 49)
              {
                   text_from_sql(ch, SQL_TITLE_APPRAISAL_1);
                   return;
              }
              else if (SKILL >= 50 && SKILL <= 99)
              {
                   text_from_sql(ch, SQL_TITLE_APPRAISAL_2);
                   return;
              }
              else if (SKILL >= 100 && SKILL <= 149)
              {
                   text_from_sql(ch, SQL_TITLE_APPRAISAL_3);
                   return;
              }
              else if (SKILL >= 150 && SKILL <= 199)
              {
                   text_from_sql(ch, SQL_TITLE_APPRAISAL_4);
                   return;
              }
              else if (SKILL >= 200 && SKILL <= 249)
              {
                   text_from_sql(ch, SQL_TITLE_APPRAISAL_5);
                   return;
              }
              else if (SKILL >= 250 && SKILL <= 299)
              {
                   text_from_sql(ch, SQL_TITLE_APPRAISAL_6);
                   return;
              }
              else if (SKILL >= 300 && SKILL <= 349)
              {
                   text_from_sql(ch, SQL_TITLE_APPRAISAL_7);
                   return;
              }
              else if (SKILL >= 350 && SKILL <= 399)
              {
                   text_from_sql(ch, SQL_TITLE_APPRAISAL_8);
                   return;
              }
              else if (SKILL >= 400 && SKILL <= 449)
              {
                   text_from_sql(ch, SQL_TITLE_APPRAISAL_9);
                   return;
              }
              else if (SKILL >= 450 && SKILL <= 499)
              {
                   text_from_sql(ch, SQL_TITLE_APPRAISAL_10);
                   return;
              }
              else if (SKILL >= 500 && SKILL <= 599)
              {
                   text_from_sql(ch, SQL_TITLE_APPRAISAL_11);
                   return;
              }
              else if (SKILL >= 600 && SKILL <= 699)
              {
                   text_from_sql(ch, SQL_TITLE_APPRAISAL_12);
                   return;
              }
              else if (SKILL >= 700 && SKILL <= 799)
              {
                   text_from_sql(ch, SQL_TITLE_APPRAISAL_13);
                   return;
              }
              else if (SKILL >= 800 && SKILL <= 899)
              {
                   text_from_sql(ch, SQL_TITLE_APPRAISAL_14);
                   return;
              }
              
              //  etc, etc...

    Let's just say I kept going with 50 more "else if"s. As I stated, I ran into this problem before where I actually had "too many" and the function pooped out. I recall that I fixed the problem with the "switch" statement. However, I'm not able to use "switch" in this case.

    Anyway...... I know there is a maximum number of "else if"s you can use, but I don't remember. Does anyone know/recall?

    Thanks in advance.

  2. #2
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    This may be a bit overkill, but couldn't you just use a look up table instead of using if/else or switch's?
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    It's up to the implementation what the limit on nesting level is, but the standard does guarantee minimums.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    std::vector<std::string> SQL_Titles;
    SQL_Titles.push_back(SQL_TITLE_APPRAISAL_1);
    SQL_Titles.push_back(SQL_TITLE_APPRAISAL_2);
    // ...
    
    if(SKILL >= 10 && ((SKILL / 50) + 1) <= SQL_Titles.size())
      text_from_sql(ch, SQL_Titles[SKILL / 50]);
    That code will solve your problem neatly. It first creates a list to contain all the different values of SQL_TITLE_APPRAISAL_X which I guessed were std::string but you can change that easily. Then, it proceeds to check that SKILL is >= 10 and ensure that we do not access an index outside of the range the list can access. Dividing SKILL by 50 gives us the index in the list (49 / 50 will be rounded to 0, which is the first index, for example).

    Have fun.

    Edit: Fixed mistake in code as suggested by CornedBee.
    Last edited by Desolation; 11-10-2006 at 04:59 AM.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Good that compiler finally told you there's something wrong with the way you are coding. Your case is a bit more complicated than above, but you should still look out for patterns and use arrays or some other containers. (Skill needs to be at least 10. From 10 to 500 it goes in steps of 50, then in steps of 100. Etc. It wouldn't hurt to plan the rules of your game so, that you wouldn't have to wrestle with irregularities and exceptions later.)

    By the way, why do you think you can't do it with a switch:
    Code:
    switch (SKILL) {
        case 11: case 12: case 13: /*...*/ case 49:
            text_from_sql(ch, SQL_TITLE_APPRAISAL_1);
                   return;
        case 51: case 52: /*etc. etc.*/

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Desolation: std::vector, not std::list. std::list does not have random access.

    A range map might also work. A bit trickier to implement, because there's no default component for it, but it would work with irregular intervals.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by CornedBee
    Desolation: std::vector, not std::list. std::list does not have random access.

    A range map might also work. A bit trickier to implement, because there's no default component for it, but it would work with irregular intervals.
    I always forget about that bit =/ Thanks for the correction. I still think my solution is way more efficient than listing all of the possible solutions =/

  8. #8
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    If I remember correctly, compilers should support at least 28 layers of nesting, so that should be 26 or so else ifs.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    903
    There is generally always a better solution than having 26 else ifs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  4. Explanation of switch statements
    By ammochck21 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2006, 01:59 PM
  5. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21