The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Are switch statements pointless?
Discuss Are switch statements pointless? in the C Programming forum on Dev Shed. Are switch statements pointless? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 21st, 2012, 10:43 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 17
Time spent in forums: 3 h 31 m 51 sec
Reputation Power: 0
|
|
|
Are switch statements pointless?
I don't know, but I just see them as if and elseif statements that can only take conditions involving number values. What makes them better than if and elseif statements?
|

December 22nd, 2012, 07:35 AM
|
 |
Contributing User
|
|
Join Date: Nov 2012
Location: Iran / France
|
|
|
To some extent it is related to the compiler and the way it optimizes the code. For limited set of decision statements, switch/case could be better from a performance point of view (not necessarily always)
|

December 22nd, 2012, 08:15 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
|
An if/else if/else chain is indeed more flexible in that each condition expression may be different and more complex. Therein lies your answer - a switch is much simpler and more constrained so can be optimised more easily by the compiler. The simplest possible construct that achieves the desired results will be much more efficient, and easier to maintain.
Try writing a large switch statement and a corresponding if/else if/else chain and compare the generated code in your debugger or compiler assembler output. Certainly where the case labels are contiguous range of integers, the compiler will generate a jump-table for the switch and go directly from the switch to the case, whereas in an if/else-if/else chain each condition in the chain will be tested in turn until the correct one is found. Even sparse and out-of-order case values are likley to generate simpler code that the equivalent if/else-if/else code.
|

December 22nd, 2012, 12:35 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
IN early UNIX code you'll see some instances of switch...case which use the default fallthrough feature (admittedly, this feature isn't used very much though in most code).
Code:
switch (value) {
case 23:
do_something();
case 15:
do_some_more_stuff();
break;
case 32:
case 47:
do_other_stuff();
break;
}
Here, if the value is 15, it will run do_more_stuff() and jump out because of the break below it. If value is 23, it will run do_something() and then fall through and run do_some_more_stuff() because there is no break in between case 23 and case 15. In most of our programming situations, people put break in between two case statements, but in the early UNIX kernel code, there were some situations where they used the fall-through feature.
Also if the value is 32 or 47, it will run do_other_stuff();
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|