code_junkie

Multiple Cases in Switch:

Nov 10th, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. switch (value)
  2. {
  3. case 1:
  4. case 2:
  5. case 3:
  6. //do some stuff
  7. break;
  8. case 4:
  9. case 5:
  10. case 6:
  11. //do some different stuff
  12. break;
  13. default:
  14. //default stuff
  15. break;
  16. }
  17.  
  18. switch (value)
  19. {
  20. case 1,2,3:
  21. //Do Something
  22. break;
  23. case 4,5,6:
  24. //Do Something
  25. break;
  26. default:
  27. //Do the Default
  28. break;
  29. }
  30.  
  31. Dim number As Integer = 8
  32. Select Case number
  33. Case 1 To 5
  34. Debug.WriteLine("Between 1 and 5, inclusive")
  35. ' The following is the only Case clause that evaluates to True.
  36. Case 6, 7, 8
  37. Debug.WriteLine("Between 6 and 8, inclusive")
  38. Case 9 To 10
  39. Debug.WriteLine("Equal to 9 or 10")
  40. Case Else
  41. Debug.WriteLine("Not between 1 and 10, inclusive")
  42. End Select
  43.  
  44. Switch (value)
  45. {
  46. case 1: case 2: case 3:
  47. // Do Something
  48. break;
  49. case 4: case 5: case 6:
  50. // Do Something
  51. break;
  52. default:
  53. // Do Something
  54. break;
  55. }
  56.  
  57. switch (value)
  58. {
  59. case 1...3:
  60. //Do Something
  61. break;
  62. case 4...6:
  63. //Do Something
  64. break;
  65. default:
  66. //Do the Default
  67. break;
  68. }
  69.  
  70. int c = 2;
  71. if(Enumerable.Range(0,10).Contains(c))
  72. DoThing();
  73. else if(Enumerable.Range(11,20).Contains(c))
  74. DoAnotherThing();
  75.  
  76. public static void MySwitchWithEnumerable(int switchcase, int startNumber, int endNumber, Action action)
  77. {
  78. if(Enumerable.Range(startNumber, endNumber).Contains(switchcase))
  79. action();
  80. }
  81.  
  82. MySwitchWithEnumerable(c, 0, 10, DoThing);
  83. MySwitchWithEnumerable(c, 10, 20, DoAnotherThing);
  84.  
  85. public static void MySwitchWithEnumerable(int startNumber, int endNumber, Action action){
  86. MySwitchWithEnumerable(3, startNumber, endNumber, action);
  87. }
  88.  
  89. string s = foo();
  90.  
  91. switch (s) {
  92. case "abc": /*...*/ break;
  93. case "def": /*...*/ break;
  94. }
  95.  
  96. switch (x)
  97. {
  98. case 1:
  99. DoSomething();
  100. break;
  101. case 2:
  102. DoSomething();
  103. break;
  104. case 3:
  105. DoSomething();
  106. break;
  107. ...
  108. }
  109.  
  110. private void DoSomething()
  111. {
  112. ...
  113. }
  114.  
  115. switch (x)
  116. {
  117. case 1:
  118. case 2:
  119. case 3:
  120. DoSomething();
  121. break;
  122. ...
  123. }
  124.  
  125. private void DoSomething()
  126. {
  127. ...
  128. }
  129.  
  130. switch (i) {
  131. case 0:
  132. CaseZero();
  133. break;
  134. case 1:
  135. CaseOne();
  136. break;
  137. default:
  138. CaseOthers();
  139. break;
  140. }
  141.  
  142. switch (i) {
  143. case 0:
  144. CaseZero();
  145. case 1:
  146. CaseZeroOrOne();
  147. default:
  148. CaseAny();
  149. }
  150.  
  151. switch (i) {
  152. case 0:
  153. CaseZero();
  154. goto case 1;
  155. case 1:
  156. CaseZeroOrOne();
  157. goto default;
  158. default:
  159. CaseAny();
  160. break;
  161. }
  162.  
  163. switch (i) {
  164. case 0:
  165. CaseZero();
  166. break;
  167. case 1:
  168. CaseOne();
  169. break;
  170. case 2:
  171. default:
  172. CaseTwo();
  173. break;
  174. }
  175.  
  176. switch (intNumber)
  177. {
  178. case 1 | 3 | 5:
  179. DoSomething();
  180. break;
  181. case 2 | 4 | 8:
  182. DoSomethingElse();
  183. break;
  184. default:
  185. break;
  186. }
  187.  
  188. switch(value){
  189. case 1:
  190. goto case 3;
  191. case 2:
  192. goto case 3;
  193. case 3:
  194. DoCase123();
  195. //This would work too, but I'm not sure if it's slower
  196. case 4:
  197. goto case 5;
  198. case 5:
  199. goto case 6;
  200. case 6:
  201. goto case 7;
  202. case 7:
  203. DoCase4567();
  204. }
Add Comment
Please, Sign In to add comment