+ 1

What is the output this code? Explain.

int x=3; switch(x){ case 1:{x+=x;} case 3:{x+=x;} case 5:{x+=x;} default:{x+=5;} } System.out.print(x);

28th Aug 2017, 7:14 PM
shadab gada
2 Respostas
+ 5
should output 17. case 3 is a match, but it falls through to case 5 and then to the default case since none of them have break; statements.
28th Aug 2017, 7:21 PM
ChaoticDawg
ChaoticDawg - avatar
+ 5
17 x is 3, so "case 3" is run. x += x is equal to: 3 + 3 = 6. There is no break statement, so the next case will run as well. x += x is equal to: 6 + 6 = 12. There is no break, so the next case will run (the default case). x += 5 is equal to: 12 + 5 = 17.
28th Aug 2017, 7:23 PM
Rrestoring faith
Rrestoring faith - avatar