home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!usc!cs.utexas.edu!csc.ti.com!tilde.csc.ti.com!mksol!mccall
- From: mccall@mksol.dseg.ti.com (fred j mccall 575-3539)
- Subject: Re: a question about if and case
- Message-ID: <1992Dec15.170940.21140@mksol.dseg.ti.com>
- Organization: Texas Instruments Inc
- References: <1gi6poINN3lv@nz12.rz.uni-karlsruhe.de> <Mf=CaVW00aw4QDV2dx@andrew.cmu.edu>
- Date: Tue, 15 Dec 1992 17:09:40 GMT
- Lines: 71
-
- In <Mf=CaVW00aw4QDV2dx@andrew.cmu.edu> dw3u+@andrew.cmu.edu (Daniel C. Wang) writes:
-
- >GM08@DKAUNI2.BITNET (hans friedrich steffani) writes:
- >> i have something like
- >>
- >> int i;
- >>
- >> if( i == 1 || i == 25 || i == 125 )
- >> {
- >> /* do something sophisticated */
- >> }
- >> if( i == 2 || i == 30 || i == 244 )
- >> {
- >> /* do something different */
- >> }
- >>
- >> or should i use
- >>
- >> switch( i )
- >> {
- >> case 1:
- >> case 25:
- >> case 125:
- >> /* do something sophisticated */
- >> break;
- >>
- >> case 2:
- >> case 30
- >> case 244:
- >> /* do something different */
- >> break;
- >>
- >> default:
- >> break
- >> }
- >>
- >> or is there a third way??
- >> i need contructions like this very often so i need a GOOD
- >> solution.
- >>
- >> hans friedrich steffani@imbaibrs1.bau-verm.uni-karlsruhe
- >> steffani gm08@rz.uni-karlsruhe.de
- >> gm08@dkauni2.bitnet
-
- >The first way is more readable
-
- For you. I personally find switch constructs to be much easier to
- read and understand than a whole mess of 'if' constructs, plus using
- the switch will provide you with the 'default' case for those times
- when you have to do error recovery because your value winds up being
- out of band (this is messy to do for the 'if' case unelss one
- structures it as a series of 'if - else if - else if -else').
-
- >the second probably marginaly faster
- >during compile time.
-
- It will also be faster during run time (generate better code) on a lot
- of compilers (depending on how bright your optimizer is). Who cares
- how fast things COMPILE?
-
- >Can't think of a third way off hand that would
- >be worth mentioning. I'd stick with readablity over 'speed.'
-
- So would I. I find it handy that I can get both by using the
- 'switch'.
-
- --
- "Insisting on perfect safety is for people who don't have the balls to live
- in the real world." -- Mary Shafer, NASA Ames Dryden
- ------------------------------------------------------------------------------
- Fred.McCall@dseg.ti.com - I don't speak for others and they don't speak for me.
-