switch vs. if
-
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?
-
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?
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
-
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?
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.
-
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?
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 useswitch
? While it can be misused, overall it has its applications. Any time you have a discrete value (anenum
, for example) that specifies an action, aswitch
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;
-
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 useswitch
? While it can be misused, overall it has its applications. Any time you have a discrete value (anenum
, for example) that specifies an action, aswitch
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;