Renata Renata - 5 months ago 8
Objective-C Question

ios there is a way to combined switch cases?

I was wondering if there is a way to combined switch cases for example:

switch (value)
{
case 0,1,2:
nslog (@"0,1,2 cases");
break
case 3:
nslog (@"3 cases");
break;
default:
NSLog (@"anything else");
break;
}


I'll really appreciate your help

Answer

You mean, something like this?

switch (value)
{
case 0:
case 1:
case 2:
  NSLog (@"0,1,2 cases");
  break;
case 3:
  NSLog (@"3 cases");
  break;
default:
  NSLog (@"anything else");
  break;
}

You know, the switch case structure will execute each line inside the braces starting from the corresponding case line, until it reach the last one or a break. So, if you don't include a break after a case, it will go on executing the next case also.