switch-case statement without break

后端 未结 7 1694
鱼传尺愫
鱼传尺愫 2021-02-04 01:44

According to this book I am reading:

Q What happens if I omit a break in a switch-case statement?

A The break statement enables program execution to exit the swi

7条回答
  •  暖寄归人
    2021-02-04 02:07

    The key is execution control is transferred to the statement for the matching case. E.g.

    1. switch(x) {
    2.   case 1:
    3.      do_step1;
    4.   case 2:
    5.      do_step2;
    6.   default:
    7.      do_default;
    8.   }
    

    Treat lines 2, 4, 6, as "Labels" for the goto calls. On x = 1, the control will be transferred to line 3 & execution of line 3, 5 & 7 will occur.

提交回复
热议问题