Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 547
Lost1 Avatar asked Oct 19 '15 19:10

Lost1


People also ask

Can Switch case be used without break?

Break will return control out of switch case.so if we don't use it then next case statements will be executed until break appears. The break statement will stop the process inside the switch.

What happens without break in switch case?

Without break , the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached. This continuation may be desirable in some situations. The default statement is executed if no case constant-expression value is equal to the value of expression .

Is Break necessary in switch case with return?

Yes, you can use return instead of break ... break is optional and is used to prevent "falling" through all the other case statements. So return can be used in a similar fashion, as return ends the function execution.

Can the last case of a switch statement skip including the break?

Can the last case of a switch statement skip including the break? Even though the last case of a switch statement does not require a break statement at the end, you should add break statements to all cases of the switch statement, including the last case.


2 Answers

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

like image 189
Shoe Avatar answered Sep 20 '22 23:09

Shoe


  • 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; 
like image 28
Khatri Avatar answered Sep 22 '22 23:09

Khatri