search

Home  >  Q&A  >  body text

c++ - C语言switch语法的一个问题

请问图片当中这个问题,为什么答案是C呢?

怪我咯怪我咯2886 days ago600

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 14:43:23

    The conditions in switch must be certain. I believe this should be the case for most languages ​​​​you come into contact with.
    In other words, there should be no ambiguity between switch conditions.
    In D, there may be ambiguity between condition 1 and condition c. Because c is a variable, when c equals 1, it may cause unpredictable situations in the program. So the compiler simply prohibits this situation. (Although c is assigned a value of 2 here, since it is not a constant, the compiler cannot guarantee whether the variable will be changed at runtime, so it still thinks it will cause ambiguity)
    To put it simply, it is not allowed to appear in the switch condition variable. (A variable here refers to a quantity that can change the result of this conditional judgment. In swift, the syntax allows assigning conditional matching values ​​​​to variables)

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 14:43:23

    To put it simply, because A B D it’s wrong

    reply
    0
  • Cancelreply