home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!spool.mu.edu!uwm.edu!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!usenet.coe.montana.edu!news.u.washington.edu!stein.u.washington.edu!chuckb
- From: chuckb@stein.u.washington.edu (Charles Bass)
- Newsgroups: comp.lang.c
- Subject: Re: a question about if and case
- Message-ID: <1992Dec15.045042.24860@u.washington.edu>
- Date: 15 Dec 92 04:50:42 GMT
- References: <1gi6poINN3lv@nz12.rz.uni-karlsruhe.de> <Mf=CaVW00aw4QDV2dx@andrew.cmu.edu>
- Sender: news@u.washington.edu (USENET News System)
- Organization: University of Washington
- Lines: 57
-
- >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 the second probably marginaly faster
- >during compile time. Can't think of a third way off hand that would
- >be worth mentioning. I'd stick with readablity over 'speed.'
-
- In my opinion the code with the case is more readable because
- I use the case construct often. So I would use case. If speed
- were definitly the issue I would profile the code and see. The
- difference is likely to vary between machines and compilers not
- to mention possibly the values used in the case (if I recall
- information from someone's previous post on this group). At any
- rate the rule of thumb should be readable code first and if the
- profiler shows a bottleneck go fix that code. It's also a good
- idea to comment drop-thru's in a case statement.
-
- chuckb
-