Switch statement syntax for same action through different cases - objective-c

Two constants (1+2) share the same case statement. I donĀ“t want to double the code.
What is the right syntax to do this?
switch (expression) {
case 0:
[self taskA];
break;
case 1:
[self taskB];
break;
case 2:
[self taskB]
break;
default:
break;
}

Use :
switch (expression) {
case 0:
[self taskA];
break;
case 1:
case 2:
[self taskB];
break;
default:
break;
}
Edit 1:
In switch we say a term called fall-through. Whenever control reaches to a label say case 0: it falls till break is found. On break control is sent to the closing braces of switch.
If break is not encountered it goes to next case as in case then case 2. So above case 1 and case 2 shares one break statement.

Multiple case labels can refer to the same statement if break or return are not used at the end of the case.
If you do not use a break statement in case 1, the execution flows into case 2.

Related

What is causing this: Cannot jump from switch statement to this case label [duplicate]

This question already has an answer here:
Defining a block in a switch statement results in a compiler error
(1 answer)
Closed 7 years ago.
This is a switch statement that I am getting errors on:
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
// show wait view here
statusLabel.text = #"Processing...";
break;
case SKPaymentTransactionStatePurchased:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view and unlock iClooud Syncing
statusLabel.text = #"Done!";
NSError *error = nil;
[SFHFKeychainUtils storeUsername:#"IAPNoob01" andPassword:#"whatever" forServiceName: kStoredData updateExisting:YES error:&error];
// apply purchase action - hide lock overlay and
[oStockLock setBackgroundImage:nil forState:UIControlStateNormal];
// do other thing to enable the features
break;
case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
statusLabel.text = #"";
break;
case SKPaymentTransactionStateFailed:
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(#"Error payment cancelled");
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
statusLabel.text = #"Purchase Error!";
break;
default:
break;
}
The last two cases, plus the default, are giving me the following error:
Cannot jump from switch statement to this case label
I have used the switch statement many, many times; this is the first time I have seen this. The code has been copied from a tutorial (here), which I am trying to adapt for my app. Would appreciate the help on this one. SD
C is not Swift. You'll be happier if you structure your switch statements using curly braces round all of the cases interiors, like this:
switch (tag) {
case 1: { // curly braces
// ...
break;
}
case 2: { // curly braces
// ...
break;
}
case 3: { // curly braces
// ...
break;
}
}
The extra level of curly braces allows you to do things you can't do otherwise.

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
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.
Alternatively, you can do this...
case 0:
case 1:
case 2:
NSLog();
break;
case 3:
NSLog()
break;
default:
NSLog();
break;
It is also possible to use ranges (a little bit less code). The following example illustrates it:
switch (value)
{
case 0 ... 2:
NSLog (#"0,1,2 cases");
break
case 3:
NSLog (#"3 cases");
break;
default:
NSLog (#"anything else");
break;
}

Section's and cases

A small issue over here i have 4 sections when i click on the first TableCell of a section (got 4 sections) the NSlogs output is:
2012-02-20 20:33:16.870 [8880:c07] 00
Can i do something like;
if switch(indexPath.row == 0) {
case
} else if(indexPath.row == 1) {
case
}
to 'link' each Tablecell to another function?
The problem is when i use the code ;
case 0:
NSLog(#"00");
break;
each first Tablecell of a section gives me the NSlog 00
UIViewController *controller;
switch(indexPath.row) {
case 0:
NSLog(#"00");
break;
case 1:
NSLog(#"01");
break;
case 2:
NSLog(#"02");
break;
case 3:
NSLog(#"03");
break;
case 4:
NSLog(#"04");
break;
}
I think you are looking for something like this:
if (indexPath.section == 0) {
switch(indexPath.row) {
case 0:
NSLog(#"00");
break;
case 1:
NSLog(#"01");
break;
case 2:
NSLog(#"02");
break;
case 3:
NSLog(#"03");
break;
case 4:
NSLog(#"04");
break;
}
} else if (indexPath.section == 1) {
switch(indexPath.row) {
case 0:
NSLog(#"10");
break;
case 1:
NSLog(#"11");
break;
case 2:
NSLog(#"12");
break;
case 3:
NSLog(#"13");
break;
case 4:
NSLog(#"14");
break;
}
}
The if statement separates the execution path on the section, and then switch further separates it on the row. If the number of sections is significant, you could use an outer switch outside of the inner switches.

Strange Switch Parse error

switch (indexPath.section) {
case 0: //products used
NSString * chemical = [selectedChemicals objectAtIndex:indexPath.row];
cell.textLabel.text = chemical;
break;
case 1: //areas sprayed
return [selectedAreas count];
break;
case 2://target pests
return [selectedPests count];
break;
case 3://notes
return 1;
break;
}
gives me: "/Users/grady/programming/ObjectivelyBetter/bioguard/Classes/JobWizardViewController.m:147: error: 'chemical' undeclared (first use in this function)"
putting a blank semi-colon at the beginning of the case fixes it.
switch (indexPath.section) {
case 0: //products used
;
NSString * chemical = [selectedChemicals objectAtIndex:indexPath.row];
cell.textLabel.text = chemical;
break;
case 1: //areas sprayed
return [selectedAreas count];
break;
case 2://target pests
return [selectedPests count];
break;
case 3://notes
return 1;
break;
}
When you declare variables within a case statement, it's a good practice (and required to avoid these kinds of errors) to enclose the statements inside curly braces, e.g.
case 0:
{
int i = 0;
....
break;
}
Not sure why a semicolon along would have "solved" the issue. That's kind of odd... the curly braces are what you need.
In your particular case you could also just eliminate the local variable declaration and set the cell textLabel like so:
cell.textLabel.text = [selectedChemicals objectAtIndex:indexPath.row];

Odd compiler error when using Obj-C objects in a switch statement

I get a compiler error when using an Objective-C object within a switch statement:
switch (myConstant)
{
case 0:
UIViewController *myController = [[[UIViewController alloc] init] autorelease];
[self.navigationController pushViewController:myViewController animated:YES];
break;
case 1:
// stuff
break;
default:
break;
}
The error states:
Expected expression before 'UIViewController'
'myViewController' undeclared (first use in this function)
I understand that the second error is a direct result of the first error, but what I don't understand is why I get the 'expected expression' error in the first place...
If I put a ; at the end of the case 0: line, then it will compile, but I shouldn't have to do this, no?
This will also fail to compile, with the same error:
switch (0)
{
case 0:
int a = 0;
break;
default:
break;
}
However, if I declare the int a; outside of the switch block, then a = 0; compiles fine.
I thought I understood switch statements - clearly I don't. Could someone please explain?
Just add {} within the case if you declare a new variable. The case part of a switch statement is not a correct scope to declare variables in C.
case 0:
{
int a = 0;
break;
}
...