Switch case logical expression statement in Java - versus JS or PHP

前端 未结 2 395
醉梦人生
醉梦人生 2021-01-25 20:32

in JavaScript and also I\'ve seen it in PHP : You can use a logical expression inside you cases : For Example :

switch(true){
case (d<10):
document.write(\"Le         


        
相关标签:
2条回答
  • 2021-01-25 20:50

    It is nice to have, but you cannot. Quoting JLS:

    The body of a switch statement is known as a switch block.

    Any statement immediately contained by the switch block may be labeled with one or more switch labels, which are case or default labels.

    These labels are said to be associated with the switch statement, as are the values of the constant expressions (§15.28) or enum constants (§8.9.1) in the case labels.

    These labels have to be constant expressions or enum constants.

    0 讨论(0)
  • 2021-01-25 21:00

    No You cannot do that using switch in java. You have to use if-else to accomplish this.

    See this link for more details.

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