+ 2

Help. what's the output? why?

int x=1; switch(x){ case 1:x++; case 2:x++; default:x++; } System.out.println(x);

22nd Mar 2019, 8:54 AM
Iftakher Hossain
Iftakher Hossain - avatar
3 Antworten
+ 3
Why 4: Normally you use a break; statement after a case: x = 1 --> case 1: x++ break; Jump out of the switch statement --> print 2 But without break; the code inside all cases (after the matched case) will be execute: x = 1 --> case 1, case 2, default x = 2 --> case 2, default x = 4 --> default
22nd Mar 2019, 6:39 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Result = 4 Run this program https://code.sololearn.com/cFrYPneSM16P/#java In switch break statement is missing.. When break is missing the flow continues to pass from all cases.
22nd Mar 2019, 8:59 AM
Prokopios Poulimenos
Prokopios Poulimenos - avatar
+ 1
Thanks
22nd Mar 2019, 1:48 PM
Iftakher Hossain
Iftakher Hossain - avatar