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
break
or theswitch
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. – Ygernebreak
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). – Dipeptideoption==2
but it will not do B if it did A – Chelate