Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. switch vs. if

switch vs. if

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studioquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JKallen
    wrote on last edited by
    #1

    I had been taught not to use goto or switch. However a lot of professional code uses both. Is the switch statement any faster than a lot of if anf else if's? In other words, is switch the same thing as a lot of if else ifs from a machine code point of view?

    C T G 3 Replies Last reply
    0
    • J JKallen

      I had been taught not to use goto or switch. However a lot of professional code uses both. Is the switch statement any faster than a lot of if anf else if's? In other words, is switch the same thing as a lot of if else ifs from a machine code point of view?

      C Offline
      C Offline
      Cliff Hatch
      wrote on last edited by
      #2

      I agree it's a good idea to avoid goto, but in my opinion there are correct applications for switches and I would not be surprised to see them in professional code. I doubt there is much difference between if's and switches in terms of speed. I think the main issue is clarity. The switch is just easier to read if there are a lot of states to be tested. Best Regards Cliff -- modified at 5:11 Sunday 4th September, 2005

      1 Reply Last reply
      0
      • J JKallen

        I had been taught not to use goto or switch. However a lot of professional code uses both. Is the switch statement any faster than a lot of if anf else if's? In other words, is switch the same thing as a lot of if else ifs from a machine code point of view?

        T Offline
        T Offline
        Tim Smith
        wrote on last edited by
        #3

        With today's compilers, switches and ifs can perform about the same. You will often find that a compiler will convert a switch statement to a series of ifs and maybe even visa-versa. Tim Smith I'm going to patent thought. I have yet to see any prior art.

        1 Reply Last reply
        0
        • J JKallen

          I had been taught not to use goto or switch. However a lot of professional code uses both. Is the switch statement any faster than a lot of if anf else if's? In other words, is switch the same thing as a lot of if else ifs from a machine code point of view?

          G Offline
          G Offline
          Gary R Wheeler
          wrote on last edited by
          #4

          While there may be a valid use for goto, I've not used it in years. You can always organize your code so that it isn't necessary, and the modified code is almost invariably more robust. I am curious. Why were you taught to not use switch? While it can be misused, overall it has its applications. Any time you have a discrete value (an enum, for example) that specifies an action, a switch statement is appropriate. The object-oriented programming crowd would argue that you should create a class structure, and derive unique classes for each case. That approach is inefficient and overkill a lot of the time. It also makes it a lot more difficult to follow program logic.


          Software Zen: delete this;

          J 1 Reply Last reply
          0
          • G Gary R Wheeler

            While there may be a valid use for goto, I've not used it in years. You can always organize your code so that it isn't necessary, and the modified code is almost invariably more robust. I am curious. Why were you taught to not use switch? While it can be misused, overall it has its applications. Any time you have a discrete value (an enum, for example) that specifies an action, a switch statement is appropriate. The object-oriented programming crowd would argue that you should create a class structure, and derive unique classes for each case. That approach is inefficient and overkill a lot of the time. It also makes it a lot more difficult to follow program logic.


            Software Zen: delete this;

            J Offline
            J Offline
            JKallen
            wrote on last edited by
            #5

            I agree. Thanks for your response.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups