Switch-case statement without breaking

According to this book, I read:

Q What happens if I omit the gap in the switch-case statement?

A break statement allows program execution to exit the switch design. Without it, execution continues to evaluate the following case statements.

Suppose I have codes similar to

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

Does this mean if I choose case 1, A and C are done. If I choose case 2, B and C will be executed. If I do not select any, then only C.

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

I guess this is a bad programming practice, but I'm curious about what will happen to gain a deeper understanding of how it all works. Thanks

+7
source share
5 answers

A gap acts like a GOTO command, or, perhaps a better example, it is like using return for a void function. So, since this is the end, it does not matter if it is there or not. Although I really like to include it.

+2
source

You do everything from the selected case until you see the completion of the break or switch . Thus, only C or B can be executed, and then C, or both B and C, but never A and C

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

if your option is 1 it does everything until it finds the keyword break ..., which means that the interrupt ends with highlighting switchcase Output: A then B then C, so it is recommended to put a break after each case:

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

if your option is 1 output will be: just A ...

note: by default , break is not required;

+6
source
  • If you do not turn on the gap in any case, then all of the following case will be executed and until he sees the gap.

  • And if you do not enable the default break, this will not cause any effect, since in this case there is not a single case by default.

  • And do not use the gap, usually considered bad practice, but for some time it can also come in handy due to its falling character. For instance:

    case variant A:

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

    caseB option:

     // 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; 
+5
source

I have seen in many comments and answers that it is a bad practice to omit break lines. I personally find this very useful in some cases.

Let's just take a very simple example. This is probably not the best, just take it as an illustration:
- if the login is unsuccessful, you need to register an unsuccessful attempt.
- for the third unsuccessful attempt, you want to log in and do something else (alert admin, block account, ...).

Since the same action is for the first and second attempts, there is no need to break between the two and rewrite the same commands a second time.
Now for the third time you want to do other things, as well as record a journal. First, perform other actions, then let it start (no break ) through the action log 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 were crazy practice to have common code for different cases, the C structure would simply NOT allow.

+2
source

Source: https://habr.com/ru/post/1234051/


All Articles