O'Reilly logo
  • Yuan Peng Chiang thinks this is interesting:

If one of the case values matches the expression, execution jumps to those statements and continues to the end of the switch block unless a break statement is encountered


Cover of Sams Teach Yourself C++ in 24 Hours, fifth edition


如果純綷比對數字或字元的話,建議使用switch,因為它只會在一開始的switch括號中取出變數 值一次,然後將這個值與下面所設定的case比對,但如果您使用if的話,每次遇到條件式時,都要取出變數值,效率的差異就在這.

The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered