+ 7

Switch case - question

How is this result 17? I don't get it: int x = 3; switch(x){ case 1: x+=x; case 3: x+=x; case 5: x+=x; default: x+=5; } System.out.print(x);

5th Apr 2018, 10:57 AM
Mais NĂŒsse
Mais NĂŒsse - avatar
3 Answers
+ 9
OK, now I realize there's no "break;" statement. Now I get it. Sorry for the unnecessary question.
5th Apr 2018, 10:59 AM
Mais NĂŒsse
Mais NĂŒsse - avatar
0
first u use the break statement . break statement break the switch case. and we get the result 6
6th Apr 2018, 1:01 PM
Kapil Mewada
Kapil Mewada - avatar
0
Easy to miss break statements, or to miss that they aren't there when you are looking at some code you've not looked at in a while. There is one reason why you wouldn't want to put in a break - if there are some scenarios when you need additional code first, but I guess those are fairly rare (so she they do happen they could catch you out)
12th Apr 2018, 3:39 PM
Phil
Phil - avatar