It prints output from case 10: but prints x=11 how? after which line it increments the value of x
int x = 10;
switch (x++) {
case 10:
System.out.println("case 1 ::: "+x);
break;
case 11:
System.out.println("case 2 ::: "+x);
break;
default:
System.out.println("default ::: "+x);
}
OUTPUT
case 1 ::: 11
The post increment operator x++ increments x but returns the previous value of x - 10. Therefore the switch statement gets the value 10 (that's the value of the x++ expression), and executes the block of case 10:, at which point System.out.println("case 1 ::: "+x); prints the incremented value of x - 11.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With