+ 15

Why doesn't the Switch without break statements print all the cases?

The code I encountered is: int age=42; switch (age){ case 16: cout << "Too young"<< endl; case 42: cout << "Adult"<< endl; case 70: cout << "Senior"<< endl; default: cout << "This is the default case"<< endl; } And the output is: Adult Senior This is the default case

17th Oct 2016, 12:59 PM
Rebeka Asryan
Rebeka Asryan - avatar
10 Answers
+ 9
spandan is correct, switch would go through the cases until it finds the right case and then runs all the cases from then on. that is why it has not run the ones prior to case 42
14th Dec 2016, 1:11 PM
Jason Hoffman
Jason Hoffman - avatar
+ 6
Yeah, what Zen said! Remember that C++ (and C) are super old, and so every possible way to squeeze out some performance from machines from the 80s was welcomed. If programming was invented today, there would probably be no switch-statements, but it can be useful, exactly for this so-called "fall-through". And just for fun, the craziest thing I've ever seen regarding switch statements is "Duff's device", where Tom Duff mushed a while-loop and a switch into one single thing (https://en.wikipedia.org/wiki/Duff%27s_device). I mean, look at that! So gross yet so beautiful. switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while (--n > 0); }
17th Oct 2016, 3:53 PM
Schindlabua
Schindlabua - avatar
+ 5
the switch statement directs to the case that is similar to the given condition. however accoring to the block system the code starts to be running hence after line by line. so actually the switch statement skips the codes before the desired case. now break actually helps to exit from the loop. you can use break in for or while loops too. so after the codes in the case is exicuted break helps to get out of the switch statement. if you don't use it then simply the next code, that is the next case will be executed.
14th Dec 2016, 11:49 AM
Spandan Mondal
Spandan Mondal - avatar
+ 5
the switch will encounter the given no. and then starts printing no.s below that no. until it encounters break statement
14th Dec 2016, 2:19 PM
Am@n
Am@n - avatar
+ 4
My guess is that it was implemented like this to allow for the matching of several different values at the same time, like this: switch(val){ case 1: case 2: case 3: //do something if val is 1, 2, or 3 break; case 4: case 5: //do something else if val is 4 or 5 break; }
17th Oct 2016, 1:07 PM
Zen
Zen - avatar
+ 1
switch statement without break, is just like do while statement, it will execute from the true case until it reach to the break statement, also do while statement executes the codes until it reaches the condition these statements are not similar but some how they have related concepts
16th Oct 2018, 2:51 PM
Erick Raphael
Erick Raphael - avatar
0
None of the cases would have executed if the switch condition was never met by all of cases In order for any condition when all have no break statement to be executed they should follow one case whitch matches the switch condition /* please forgive my English */
11th Feb 2017, 9:14 AM
Yongz
Yongz - avatar
0
hehehe so kindly use break statement.
18th Feb 2017, 10:18 AM
Nomi Numan
Nomi Numan - avatar
- 1
u know what.. just chill
14th Dec 2016, 3:57 PM
Faran Ahmed
Faran Ahmed - avatar
- 1
Without the break statement the current case statement excuses infinitely so the other case statements are not executed. So the break statement stops execution of the current case thus making it possible to execute the next case
15th Dec 2016, 5:40 PM
Agaba Ivan
Agaba Ivan - avatar