+ 2
Help. what's the output? why?
int x=1; switch(x){ case 1:x++; case 2:x++; default:x++; } System.out.println(x);
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
+ 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.
+ 1
Thanks