home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18364 < prev    next >
Encoding:
Internet Message Format  |  1992-12-14  |  2.1 KB

  1. 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
  2. From: chuckb@stein.u.washington.edu (Charles Bass)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: a question about if and case
  5. Message-ID: <1992Dec15.045042.24860@u.washington.edu>
  6. Date: 15 Dec 92 04:50:42 GMT
  7. References: <1gi6poINN3lv@nz12.rz.uni-karlsruhe.de> <Mf=CaVW00aw4QDV2dx@andrew.cmu.edu>
  8. Sender: news@u.washington.edu (USENET News System)
  9. Organization: University of Washington
  10. Lines: 57
  11.  
  12. >GM08@DKAUNI2.BITNET (hans friedrich steffani) writes:
  13. >> i have something like
  14. >> 
  15. >> int i;
  16. >> 
  17. >> if( i == 1 || i == 25 || i == 125 )
  18. >>  {
  19. >>   /* do something sophisticated */
  20. >>  }
  21. >> if( i == 2 || i == 30 || i == 244 )
  22. >>  {
  23. >>   /* do something different */
  24. >>  }
  25. >> 
  26. >> or should i use
  27. >> 
  28. >> switch( i )
  29. >>  {
  30. >>   case 1:
  31. >>   case 25:
  32. >>   case 125:
  33. >>   /* do something sophisticated */
  34. >>   break;
  35. >> 
  36. >>   case 2:
  37. >>   case 30
  38. >>   case 244:
  39. >>   /* do something different */
  40. >>   break;
  41. >> 
  42. >>   default:
  43. >>   break
  44. >>  }
  45. >> 
  46. >> or is there a third way??
  47. >> i need contructions like this very often so i need a GOOD
  48. >> solution.
  49. >> 
  50. >> hans friedrich   steffani@imbaibrs1.bau-verm.uni-karlsruhe
  51. >> steffani         gm08@rz.uni-karlsruhe.de
  52. >>                  gm08@dkauni2.bitnet
  53.  
  54. >The first way is more readable the second probably marginaly faster
  55. >during compile time.  Can't think of a third way off hand that would
  56. >be worth mentioning.  I'd stick with readablity over 'speed.'
  57.  
  58. In my opinion the code with the case is more readable because
  59. I use the case construct often.  So I would use case.  If speed
  60. were definitly the issue I would profile the code and see.  The
  61. difference is likely to vary between machines and compilers not
  62. to mention possibly the values used in the case (if I recall
  63. information from someone's previous post on this group).  At any
  64. rate the rule of thumb should be readable code first and if the
  65. profiler shows a bottleneck go fix that code.  It's also a good
  66. idea to comment drop-thru's in a case statement.
  67.  
  68. chuckb
  69.