+ 2

Switch

why does it prints 3 instand of 1? int x = 0; int a = 2; switch(a); { case 1: ++x; case 2: ++x; case 3: ++x; default: ++x; } System.out.println(x);

12th Jul 2017, 7:53 PM
Giorgi Gogelashvili
Giorgi Gogelashvili - avatar
1 Réponse
+ 5
because there is no break statement.. the value of x will increase till the end unless it finds break or default: case.. now if u add case 4: ++x; case 5: ++x; and then default: case.. the output will be 5
12th Jul 2017, 8:00 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar