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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!infonode!ingr!b30news!mueller
  3. From: mueller@b30news.b30.ingr.com ( Phil Mueller )
  4. Subject: Re: a question about if and case
  5. Message-ID: <1992Dec14.165638.9597@b30.ingr.com>
  6. Sender: mueller@b30.ingr.com (Phil Mueller)
  7. Organization: Intergraph
  8. References: <1gi6poINN3lv@nz12.rz.uni-karlsruhe.de>
  9. Date: Mon, 14 Dec 1992 16:56:38 GMT
  10. Lines: 69
  11.  
  12. In article <1gi6poINN3lv@nz12.rz.uni-karlsruhe.de> "hans friedrich steffani" <GM08@DKAUNI2.BITNET> writes:
  13. >
  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.  
  48. I would do this
  49.  
  50.   switch( ii ) {
  51.     case   1:
  52.     case  25:
  53.     case 125:
  54.       /* something */
  55.     break; /* 1,25,125 */
  56.  
  57.  
  58.     case   2:
  59.     case  30:
  60.     case 244:
  61.       /* something */
  62.     break; /* 2,30,244 */
  63.  
  64.     default:
  65.       error( "%s, ii has value %d, should be on of %s, in %s:%d",
  66.              "internal error", ii, "{1,2,25,30,125,244}", __FILE__, __LINE__ );
  67.     break;
  68.   } /* switch ii */
  69.  
  70.  
  71. 1) I can just scan down the line to see the values.
  72. 2) defensive error message gives me all the info I have so I can fix.
  73.  
  74. ( I split up the error line to fit on one line.  You should get the idea. )
  75.  
  76. I hard-coded in the valid values.  Does anyone know a way to make that 
  77. automatic?
  78. -- 
  79. Phil Mueller    mueller@b30news.b30.ingr.com
  80. The preceeding opinions are not necessarily those of Intergraph Corporation.
  81.