code_junkie

Why was the switch statement designed to need a break?

Nov 12th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. switch (int)
  2. {
  3. case 1 :
  4. {
  5. printf("1n");
  6. break;
  7. }
  8.  
  9. case 2 :
  10. {
  11. printf("2n");
  12. }
  13.  
  14. case 3 :
  15. {
  16. printf("3n");
  17. }
  18. }
  19.  
  20. switch (operator->num_of_operands) {
  21. case 2: process_operand( operator->operand_2);
  22. /* FALLTHRU */
  23.  
  24. case 1: process_operand( operator->operand_1);
  25. break;
  26. }
  27.  
  28. case 0:
  29. case 1:
  30. case 2:
  31. // all do the same thing.
  32. break;
  33. case 3:
  34. case 4:
  35. // do something different.
  36. break;
  37. default:
  38. // something else entirely.
  39.  
  40. case optionA:
  41. // optionA needs to do its own thing, and also B's thing.
  42. // Fall-through to optionB afterwards.
  43. // Its behaviour is a superset of B's.
  44. case optionB:
  45. // optionB needs to do its own thing
  46. // Its behaviour is a subset of A's.
  47. break;
  48. case optionC:
  49. // optionC is quite independent so it does its own thing.
  50. break;
  51.  
  52. dsend(to, from, count)
  53. char *to, *from;
  54. int count;
  55. {
  56. int n = (count + 7) / 8;
  57. switch (count % 8) {
  58. case 0: do { *to = *from++;
  59. case 7: *to = *from++;
  60. case 6: *to = *from++;
  61. case 5: *to = *from++;
  62. case 4: *to = *from++;
  63. case 3: *to = *from++;
  64. case 2: *to = *from++;
  65. case 1: *to = *from++;
  66. } while (--n > 0);
  67.  
  68. switch(foo) {
  69. case 1:
  70. /* stuff for case 1 only */
  71. if (0) {
  72. case 2:
  73. /* stuff for case 2 only */
  74. }
  75. /* stuff for cases 1 and 2 */
  76. case 3:
  77. /* stuff for cases 1, 2, and 3 */
  78. }
Add Comment
Please, Sign In to add comment