+ 13
Can anyone please explain the logic of this switch statement problem which outputs 3?
int a=2; int x=0; switch (a){ case 1: ++x; case 2: ++x; case 3: ++x; default: ++x; } s.o.p (x);
9 Answers
+ 8
You didnt use any break statements, which results in the program executing all following statements after case 2, including those who actually dont match.
Include break statements after each case and output will be 1.
+ 6
oh okay ...absence of break statement... thank you Naitomea and shobhit âș
+ 6
đokay , give it a try though... don't worry its for learning ... I am challenging with different levels just to learn ....i believe we'll learn more with question...â
+ 5
it is because you are missing break statement so on case 2 when a is 2 it also implements logic of case 3 as you are missing break statement.
+ 3
@vitalij I meant s.o.p for " System.out.print"
+ 2
what is means s.o.p?
0
Should his one not be 7 since it is the last statement?
0
i cant learn anythinf from this app can someone help me plsđ
0
thanx Jigme but how can i learn more fast