Switch statement using or

前端 未结 7 2392
时光说笑
时光说笑 2020-12-10 10:29

I\'m creating a console app and using a switch statement to create a simple menu system. User input is in the form of a single character that displays on-screen

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 10:47

    'q' || 'Q' results in bool type result (true) which is promoted to integral type used in switch condition (char) - giving the value 1. If compiler allowed same value (1) to be used in multiple labels, during execution of switch statement menuChoice would be compared to value of 1 in each case. If menuChoice had value 1 then code under the first case label would have been executed.

    Therefore suggested answers here use character constant (which is of type char) as integral value in each case label.

提交回复
热议问题