Is switch statement is alternate of if else staement ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Maruf
    New Member
    • Aug 2010
    • 2

    Is switch statement is alternate of if else staement ?

    Is switch statement is alternate of if else staement ?
  • Maruf
    New Member
    • Aug 2010
    • 2

    #2
    ans me quickly plz

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      no, because in some cases switch can't be used instead of if/else.

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        I think every switch case can be replaced by every else if.

        to newb16
        Can you please give me an example that is not replaceable

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          if (somestring=="f oo") ...
          else if (somestring == "bar") ...
          else if ...

          switch(somestri ng) <- wrong, not an integral type
          case "foo":

          Yet the reverse is true (but if you use the switch's lacking of implicit break, it may be tricky)

          Comment

          • johny10151981
            Top Contributor
            • Jan 2010
            • 1059

            #6
            Mr. newb16,
            I agree with you :)

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              You can have enums if you want and then use if else.

              Regards
              Dheeraj Joshi

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                The following if statements cannot be expressed by switch statements even if the arguments are integers.
                Code:
                if ((a==2) && (b<c))
                   ...
                
                if (a==2)
                   ...
                else if (b < c)
                   ...
                However, the reverse is true: any switch statement can be replaced by a single if-elseif-else cascade.

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  However one of the original purposes of the switch was to quickly and efficiently jump between multiple options. To this end switch statements often use to use jump tables where as switch statements processing the condition a single time where as if, else if ... else statements have to process multiple conditions.

                  In early compilers this had the effect of making switch more efficient than if, else if ... else, however I believe the advent of optimising compilers has tended to reduce this effect with the optimiser being able to choose the best way to implement both a switch and an if, else if ... else.

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    Here are a few more reasons to NOT replace a switch statement with an if-elseif-else cascade:
                    1. It is less obvious to a maintainer that each leg of the if-elseif-else refers to the same variable than it is for a switch statement.
                    2. You might mistakenly type the wrong variable name in one of the if-elseif expressions. This particular error is impossible with a switch statement.
                    3. If the switch variable is an enumeration, some lint tools will warn you if any enumeration constants are not mentioned in the case statements.

                    Comment

                    • sandip kalbhile
                      New Member
                      • Jul 2010
                      • 2

                      #11
                      yes , it is possible but in some cases if statement doesn't have option.

                      Comment

                      Working...