objective-c switch case with multiple arguments
Probably a basic question but I would like to reduce some code using multiple arguments on switch case statements. Possible? Correct syntax?
switch (myInteger){ case (1): //here I would like to apply multiple arguments as case (1 || 3 || 5) <#statements#> break; case (2): <#statements#> break; default: break;
Answers
You can use multiple cases right below each other.
switch (myInteger) { case 1: case 3: case 5: // statements break; case 2: // statements break; default: // statements break; }
case 1: case 3: case 5: statements; break; case 2: statements; break; default: break;
For Swift 3 there is a modification that I would like to mention
switch some value to consider { case 1: //single argument print("ABC") case 2,3: // multiple arguments print("KLM") default: print("XYZ") }
Hope it helps you. Thanks
Switch case must declare inside the main method
SYNTAX
Switch (variable r expression) { Case 1 : Body ; Break Case 2 : Body; Break; Default : Body ; Break; }