Switch Case or If Else If

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dgirish1
    New Member
    • May 2007
    • 1

    Switch Case or If Else If

    Does either pose any particular benefit and is there a major difference between them?
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Hi,
    Switch case is faster compared to if-else if.
    Thats what i know abt it.
    Thanks
    Raghuram

    Comment

    • teesha
      New Member
      • May 2007
      • 4

      #3
      that depends of the number of your test case.
      in my opinion, there's no difference if you have 2 or 3 test conditions but over it "switch case" is faster than " if-else-if ".

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        switch is problematic when used badly in terms of maintainability , and can cause inadvertent errors:

        Code:
        switch(i) {
          case 1:
            // do stuff
          case 2:
            // do other stuff
            break;
        }
        In the previous example, the programmer may have not intended to cause case 2 to be executed in both cases. i.e. case 1 will 'do stuff' and 'do other stuff' before leaving the switch. Or the programmer may have done it intentionally. It is hard to say. Commenting is best in this situation if it was intentional.

        However, switch can be very fast and be maintainable if used appropriately. Depending on the compiler and the sparseness of the cases, it may be an O(1) operation to determine which case is to be executed, where an if would be an O(n) operation where n is the number of if/else if's there are.

        When you use one over the other is entirely up to you, but you should balance maintainability and speed as needed when you make your decision.


        Adrian

        Comment

        • pravin78
          New Member
          • May 2007
          • 4

          #5
          Originally posted by dgirish1
          Does either pose any particular benefit and is there a major difference between them?

          Hi,

          I think in switch case only char & int values are allowed but in if else you can also compare real numbers.

          Regards,

          Pravin

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #6
            Originally posted by pravin78
            Hi,

            I think in switch case only char & int values are allowed but in if else you can also compare real numbers.

            Regards,

            Pravin
            You are partially right. Only integral values are allowed. This includes all signed and unsigned of char, int, long, and long long.


            Adrian

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              A switch clause is just an integer or anything that can be widened to an int
              (such as char, short). The GNU compiler (as an example) recognized three
              different ways to compile a switch statement, depending on the case values:

              1) perfect
              2) dense
              3) normal

              A perfect switch statement has consecutive case values (in no particular order):
              Code:
              switch (value) {
                 case 3: do3(); break;
                 case 5: do5(); break;
                 case 4: do4(); break;
              }
              Code generated for this switch statement involves a simple jump table:
              0: address of do3();
              1: address of do4();
              2: address of do5();
              the address to be jumped to is table[value-3].

              A dense switch statement is like a perfect switch table where all the case
              clauses are almost consecutive and the resulting jump table would be dense.
              The empty entries point to the address where the (optional or empty) default
              case selector is located.

              A normal switch statement also uses a jump table and a case value table which
              again is sorted. The value table is consulted using a binary search for the case
              selector value. If the entry is found, the jump table destination is used,
              otherwise the default case clause address will be used.

              The first to variants are O(1) while the third variant is O(log(n)) where n is the
              number of case clauses in the switch statement. The AIX xlc compiler suite
              attempts to find a perfect hash function for a normal switch statement which
              can result in O(1) behaviour even for normal (sparse) switch statements. This
              attempt can fail and code similar to GNU's compiler generated code for the
              normal switch statement is generated.

              kind regards,

              Jos

              Comment

              • shubham agrawal

                #8
                The "if" statement allows multiple conditions while in switch case we use it only when we have ti check for equality.

                Another reason beween them is that "if" condition allows floating points whereas switch condition doesn't allows floating points.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  In a switch statement you only specify the name of the variable once; in an if-else-if cascade you specify the variable for each leg. Thus, the if-else-if cascade offers you more opportunity to misspell the variable.

                  It is easier for a maintenance programmer to see that each case refers to the same switch variable than it is to see that each leg of an if-else-if cascade refers to the same variable.

                  Some static analyzer tools (such as Flexelint) will warn you if the switch variable is an enumerated type and you don't have a case statement for every enumeration constant.

                  It is generally more important for your code to be easy for a human to understand than it is for the code to be optimized for size or speed.

                  Comment

                  Working...