Lost1
Lost1

Reputation: 1062

switch-case statement without break

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

Upvotes: 40

Views: 144912

Answers (8)

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.

Upvotes: 0

nav
nav

Reputation: 440

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

Upvotes: 0

Craig Mosey
Craig Mosey

Reputation: 152

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.

Upvotes: 10

soch guru
soch guru

Reputation: 211

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.

Upvotes: 2

Adham Gamal
Adham Gamal

Reputation: 795

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;

Upvotes: 8

fpierrat
fpierrat

Reputation: 804

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.

Upvotes: 4

Khatri
Khatri

Reputation: 646

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

Upvotes: 20

Shoe
Shoe

Reputation: 76300

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

Upvotes: 55

Related Questions