home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18411 < prev    next >
Encoding:
Text File  |  1992-12-15  |  2.4 KB  |  82 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!usc!cs.utexas.edu!csc.ti.com!tilde.csc.ti.com!mksol!mccall
  3. From: mccall@mksol.dseg.ti.com (fred j mccall 575-3539)
  4. Subject: Re: a question about if and case
  5. Message-ID: <1992Dec15.170940.21140@mksol.dseg.ti.com>
  6. Organization: Texas Instruments Inc
  7. References: <1gi6poINN3lv@nz12.rz.uni-karlsruhe.de> <Mf=CaVW00aw4QDV2dx@andrew.cmu.edu>
  8. Date: Tue, 15 Dec 1992 17:09:40 GMT
  9. Lines: 71
  10.  
  11. In <Mf=CaVW00aw4QDV2dx@andrew.cmu.edu> dw3u+@andrew.cmu.edu (Daniel C. Wang) writes:
  12.  
  13. >GM08@DKAUNI2.BITNET (hans friedrich steffani) writes:
  14. >> i have something like
  15. >> 
  16. >> int i;
  17. >> 
  18. >> if( i == 1 || i == 25 || i == 125 )
  19. >>  {
  20. >>   /* do something sophisticated */
  21. >>  }
  22. >> if( i == 2 || i == 30 || i == 244 )
  23. >>  {
  24. >>   /* do something different */
  25. >>  }
  26. >> 
  27. >> or should i use
  28. >> 
  29. >> switch( i )
  30. >>  {
  31. >>   case 1:
  32. >>   case 25:
  33. >>   case 125:
  34. >>   /* do something sophisticated */
  35. >>   break;
  36. >> 
  37. >>   case 2:
  38. >>   case 30
  39. >>   case 244:
  40. >>   /* do something different */
  41. >>   break;
  42. >> 
  43. >>   default:
  44. >>   break
  45. >>  }
  46. >> 
  47. >> or is there a third way??
  48. >> i need contructions like this very often so i need a GOOD
  49. >> solution.
  50. >> 
  51. >> hans friedrich   steffani@imbaibrs1.bau-verm.uni-karlsruhe
  52. >> steffani         gm08@rz.uni-karlsruhe.de
  53. >>                  gm08@dkauni2.bitnet
  54.  
  55. >The first way is more readable 
  56.  
  57. For you.  I personally find switch constructs to be much easier to
  58. read and understand than a whole mess of 'if' constructs, plus using
  59. the switch will provide you with the 'default' case for those times
  60. when you have to do error recovery because your value winds up being
  61. out of band (this is messy to do for the 'if' case unelss one
  62. structures it as a series of 'if - else if - else if -else').
  63.  
  64. >the second probably marginaly faster
  65. >during compile time.  
  66.  
  67. It will also be faster during run time (generate better code) on a lot
  68. of compilers (depending on how bright your optimizer is).  Who cares
  69. how fast things COMPILE?
  70.  
  71. >Can't think of a third way off hand that would
  72. >be worth mentioning.  I'd stick with readablity over 'speed.'
  73.  
  74. So would I.  I find it handy that I can get both by using the
  75. 'switch'.
  76.  
  77. -- 
  78. "Insisting on perfect safety is for people who don't have the balls to live
  79.  in the real world."   -- Mary Shafer, NASA Ames Dryden
  80. ------------------------------------------------------------------------------
  81. Fred.McCall@dseg.ti.com - I don't speak for others and they don't speak for me.
  82.