switch-case statement without break

让人想犯罪 __ 提交于 2019-12-21 07:12:03

问题


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 switch construct. Without it, execution continues evaluating the following case statements.

Suppose if I have codes looking like

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

Does this mean if I choose case 1, the A and C are done. If I choose case 2, B and C are done. If i choose neither, then only C is done.

if so, what happens if we omit the break after do C.

I assume these are bad programming practice, but I am curious what would happen to get a deeper understanding how it all works. Thanks


回答1:


The break acts like a "GOTO" command, or , what might be a better example is , its like when using the "return" on a "void" function. So since its a the end, it makes no difference whether its there or not. Although I do like to include it.




回答2:


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




回答3:


  • 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;
    



回答4:


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;




回答5:


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.



来源:https://stackoverflow.com/questions/33222437/switch-case-statement-without-break

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!