+ 1

Explain please the output of this code

int x = 3; switch(x){ case 1: { x+=x; } case 3: { x+=x; } case 5: { x+=x; } default: { x+=5; } } System.out.println(x); The correct answer is 17 As far as I understand, our case is "3", so x becomes 6; there is no "break;" statement, so switch continues working. Then, x becomes 11, because 6+5 is 11. Where am I making mistake? Help pls

8th Apr 2018, 8:53 PM
Ä°skender GĂŒneƟ
Ä°skender GĂŒneƟ - avatar
3 Answers
+ 4
You’re skipping case 5. Since there is no break it will fall through to all cases after that, including case 5
8th Apr 2018, 9:00 PM
Ariela
Ariela - avatar
+ 2
x=3, so switch starts from case 3. x=6, as there's no break we go to case 5 x=12, next default x=17.
8th Apr 2018, 9:00 PM
Sergey Semendyaev
Sergey Semendyaev - avatar
+ 2
I got it, guys. Thankyou Ariela 🙄 Spasibo Sergey Semendyaev
8th Apr 2018, 9:01 PM
Ä°skender GĂŒneƟ
Ä°skender GĂŒneƟ - avatar