0

Is it compulsory to have break statement at the end of the case....??

28th Oct 2016, 5:14 PM
Vaibhav Kumar
Vaibhav Kumar - avatar
4 Antworten
+ 4
When you don't use break, all cases after first case which was execuded will be executed, for example: a = 1; switch(a) { case 0: print(0); case 1: print(1); case 2: print(2); default: print(3); } In this pseudocode 123 will be printed.
28th Oct 2016, 5:18 PM
Daniel Oravec
Daniel Oravec - avatar
+ 1
C# compiler raises error if you don't use break. But it allows you to use multiple cases in a row with single body. This is not allowed: switch(a) { case 0: print(0); case 1: print(1); case 2: print(2); default: print(3); } But this is allowed: switch(a) { case 0: case 1: case 2: print(2); break; default: print(3); break; } It will print 2 if a will be 0, 1 or 2
29th Oct 2016, 9:28 PM
Ivan G
Ivan G - avatar
0
yes, because if you don't you may get unexpected results or error.
28th Oct 2016, 7:21 PM
Péricles Correia
Péricles Correia - avatar
- 1
yes,we must give break after the each case, otherwise it ll print all the values until ll get null value in RAM.
29th Oct 2016, 2:36 AM
Prashantha M.P
Prashantha M.P - avatar