Operator instruction syntax for the same action in different cases

Two constants (1 + 2) share the same case statement. I do not want to double the code.

What is the correct syntax for this?

switch (expression) { case 0: [self taskA]; break; case 1: [self taskB]; break; case 2: [self taskB] break; default: break; } 
+4
source share
2 answers

Using:

 switch (expression) { case 0: [self taskA]; break; case 1: case 2: [self taskB]; break; default: break; } 

Change 1:

In switch we pronounce the term fall-through . Whenever control reaches a label, case 0: it drops to break . In break control is sent to switch closing braces.

If break does not occur, it moves on to the next case , as in case , then case 2 . So above case 1 and case 2 contains one break statement.

+9
source

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 the break statement in case 1, execution proceeds to case 2.

+1
source

All Articles