switch statement in C / Objective C

前端 未结 1 929
-上瘾入骨i
-上瘾入骨i 2021-02-19 13:20

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

1条回答
  •  走了就别回头了
    2021-02-19 13:32

    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.

    0 讨论(0)
提交回复
热议问题