switch-case statement without break

后端 未结 7 1676
鱼传尺愫
鱼传尺愫 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 01:49

    I've seen in many comments and answers that it's a bad practice to omit break lines. I personally find it very useful in some cases.

    Let's just take a very simple example. It's probably not the best one, just take it as an illustration:
    - on bad login, you need to log the failed attempt.
    - for the third bad attempt, you want to log and do some further stuff (alert admin, block account, ...).

    Since the action is the same for first and second try, no need to break between these two and rewrite the same commands a second time.
    Now the third time, you want to do other things AND also log. Just do the other things first, then let it run (no break) through the log action of the first and second attempts:

    switch (badCount) {
        case 3: //only for 3
            alertAdmin();
            blockAccount();
        case 2: //for 2 AND 3
        case 1: //for 1 AND 2 and 3
            errorLog();
            badCount++;
    }
    

    Imho, if it was soooo bad practice to have common code for different cases, the C structure would simply NOT allow it.

    0 讨论(0)
  • 2021-02-04 01:50

    Try yourself - Run the code using ideone available here.

    #include <stdio.h>
    
    void doA(int *i){
        printf("doA i = %d\n", *i);
        *i = 3;
    }
    
    void doB(int *i){
        printf("doB i = %d\n", *i);
        *i = 4;
    }
    
    void doC(int *i){
        printf("doC i = %d\n", *i);
        *i = 5;
    }
    
    int main(void) {
        int i = 1;
        switch(i){
            case 1:
                doA(&i);
            case 2:
                doB(&i);
            default:
                doC(&i);
                break;
        }
        return 0;
    }
    

    Output:

    doA i = 1
    doB i = 3
    doC i = 4
    

    Note:

    • It will execute all the options from the selected case until it sees a break or the switch statement ends. So it might be that only C is executed, or B and then C, or A and B and C, but never A and C
    • If you change the value of the variable analysed in switch inside the handle function (e.g doA), it does not affect the flow as describe above
    0 讨论(0)
  • 2021-02-04 01:52

    You execute everything starting from the selected case up until you see a break or the switch statement ends. So it might be that only C is executed, or B and then C, or A and B and C, but never A and C

    0 讨论(0)
  • 2021-02-04 01:53
    • If you don't include break in any of case then all the case below will be executed and until it sees break.

    • And if you don't include break in default then it will cause no effect as there are not any case below this 'Default' case.

    • And not using break generally considered as a bad practice but some time it may also come handy because of its fall-through nature.For example:

      case optionA:

      //optionA needs to do its own thing, and also B's thing.
      //Fall-through to optionB afterwards.
      //Its behaviour is a superset of B's.
      

      case optionB:

      // optionB needs to do its own thing
      // Its behaviour is a subset of A's.
      break;
      

      case optionC:

      // optionC is quite independent so it does its own thing.
      break;
      
    0 讨论(0)
  • 2021-02-04 01:59
    switch (option}{
        case 1:
        do A;
        case 2:
        do B;
        case 2:
        do C;
        break;  
        default:
        do C;
    }
    

    if your option is 1 it executes everything til it finds the break keyword... that mean break end the excution of the switch --> case Output :A then B then C so it is recommended to put break after each case like :

    switch (option}{
            case 1:
            do A;
            break;
            case 2:
            do B;
            break;
            do C;
            break;        
            default:
            do D;
        }
    

    if your option is 1 the Output will be : just A ...

    note: default doesn't need a break;

    0 讨论(0)
  • 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.

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