What would be the value of x after the following statements
What would be the value of x after the following statements were executed?
int x = 10;
switch (x)
{
case 10:
x = x + 15;
case 12:
x = x + 5;
break;
default:
x = x + 3;
}
5
20
25
30
| 5 | ||
| 20 | ||
| 25 | ||
| 30 |
Solution
Initially after entering the switch statement, as the value of x is 10. The statement enters case 10, so now x becomes 25. But as there is no break statement, it enter the next case statement, now x becomes 20. Now there is a break, so the switch statement is exited. So the final value of x is 30.
