I am new to Objective-C, But from all I know about switch statements, the following code should not print anything, because to me it seems that there should be a compilation err
You can think of the switch(value){case label:, ...}
construct as a variable goto
statement, where:
1) switch(arg)
determines which label
execution will flow to next.
2) The key word case : defines the label. Example: case label:
.
In a switch statement, the case
key word is followed by a label (constant expression followed by a :
), which is treated like the label used in goto
statements. Control passes to the statement whose case constant-expression
matches the value of arg in the statement switch(arg)
.
So legally there is nothing syntactically wrong with your code. That is, it will compile and build, and run just fine. The only thing the syntax in your example code violates is readability in that the execution flow ignores the block {...}
, which in most cases would direct execution flow, and jumps directly to the targeted label defined by the case
key word, just as it should.
It's not often that ignoring well established precedent to experiment with new hybrid constructs will yield useful results. But when it does, the results can become legendary. For example, see Duff's Device.