34

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

Lost1
  • 990
  • 1
  • 14
  • 34
  • 15
    "if I choose case 1, the A and C are done": No A, B and C is done. – 101010 Oct 19 '15 at 19:36
  • 12
    Just compile some test code that does this and you'll find out I guess :p –  Oct 19 '15 at 19:36
  • 7
    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. – Jon Oct 19 '15 at 19:36
  • 6
    BTW this is not bad programming practice. – 101010 Oct 19 '15 at 19:37
  • 7
    Obligatory reference to [Duff's Device](https://stackoverflow.com/q/514118/865719) :) – maddouri Oct 19 '15 at 19:38
  • 2
    Even if you omit the break after "do C", there is no difference in the execution of the code. – Rahul Nori Oct 19 '15 at 19:39
  • 3
    Good practice would be to annotate those missing `break`s with e.g. `[[clang::fallthrough]]`, or at least leave a comment. Otherwise everyone thinks there must be a bug (as is the case most of the time). – Emil Laine Oct 19 '15 at 19:48
  • @101010 but if we have case 1, then it won't be case 2 at the same time. It will check whether `option==2` but it will not do B if it did A – lucidbrot Apr 19 '17 at 10:11
  • @Jon I'm confused by your comment: "You execute everything from the selected case up". Shouldn't that be "You execute everything from the selected case _down_"? – Thaddaeus Markle Aug 16 '20 at 13:51

8 Answers8

49

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

Shoe
  • 74,840
  • 36
  • 166
  • 272
19
  • 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;
    
Khatri
  • 616
  • 6
  • 19
9

The break acts like a goto command. Or, as a better example, it is like when using return in a void function. Since it is at the end, it makes no difference whether it is there or not. Although, I do like to include it.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Craig Mosey
  • 142
  • 7
  • 1
    I suspected this after the comments left beneath the questions. This is a good explanation, thanks. – Lost1 Oct 19 '15 at 19:47
  • 1
    Hm, I think emphasizing the `GOTO`-like nature of `break` is important. – Kyle Strand Oct 19 '15 at 20:34
  • @Kyle Strand I was thinking it might be better described as a return in a void function because of the scope. – Craig Mosey Oct 19 '15 at 20:58
  • @craigmosey Hm, I think what I meant is that it's important to realize that the `switch` itself is like a `GOTO`, because the individual `case`s are *not* separate scopes (unless you explicitly add a scope). So unlike functions, there is *no* automatic setup or cleanup on entering/exiting a `case`. – Kyle Strand Oct 19 '15 at 21:01
  • @ Kyle Strand ahhh, didn't know there was no clean up, figured because the switch case as a whole has its own scope, it would clean up. so basically the switch case as a whole does not clean up on exit and acts just like a bunch of goto's. Although it, as a whole, does have its own scope. – Craig Mosey Oct 19 '15 at 21:07
  • 3
    It would be nice that the accepted answer covers all aspects of the OP. Especially, it should be edited to point out that in case 1, A, B and C will all be executed, not only A and C. – fpierrat Oct 25 '15 at 00:48
  • Grim reaper here bringing the issue from the grave: this is plain wrong. See CPP refference https://en.cppreference.com/w/cpp/language/switch if there is no break code contained in all further cases is just run as each case was true. In provided example specyfying 1 as input yields A B C; 2 yields B, C; any other value yields C – Marcin Feb 12 '21 at 21:01
8
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;

Adham Gamal
  • 775
  • 8
  • 13
4

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.

fpierrat
  • 739
  • 7
  • 25
  • You should use if-statements with this, not a `switch`. A switch is supposed to stack up unrelated cases of the value of an expression, no-one reading your code will find the vertical direction of the code to correspond to its logical direction. –  Mar 29 '20 at 12:03
  • 1
    Then you mean no-one is able to read and understand basic samples in Microsoft online doc? https://learn.microsoft.com/en/cpp/c-language/switch-statement-c?view=vs-2019 , see sample code-blocks 2 and 4. – fpierrat Mar 30 '20 at 16:00
2

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.

soch guru
  • 191
  • 1
  • 3
  • "goto calls" Good way of thinking of it. I learned something new. Thanks –  Apr 12 '20 at 04:51
0

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
nav
  • 410
  • 6
  • 14
0

Without break statements, each time a match occurs in the switch, the statements for that case and SUBSEQUENT CASES execute until a break statement or the end of the switch is encountered.