3

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
BHAR4T
  • 338
  • 3
  • 14
  • 1
    Increment operator works the same way it does in all other statements. It doesn't behave different for a `switch` statement. – Andreas May 04 '17 at 07:20
  • The value of x is used first which is 10 and it increments afterwards which becomes 11 after post increment... for better understanding read post incrementer and pre incremaentor – dhS May 04 '17 at 07:26
  • The increment operators are explained in the Java Tutorial, http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html. Read the Fine Manual. – Lew Bloch May 04 '17 at 10:11

5 Answers5

5

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

Your question "after which line it increments the value of x?" show where you're totally missing the point of the increment operator.

It immediately increments the value. The increment is not deferred until the end of any statement.

E.g. if x = 10, the statement y = x++ + x++ + x++ will calculate y = 10 + 11 + 12 and result in y = 33 and x = 13.

In contrast, the statement y = ++x + ++x + ++x will calculate y = 11 + 12 + 13 and result in y = 36 and x = 13.

As you can see, both post-increment and pre-increment operators will immediately affect the value of the variable being incremented. The difference is whether the expression uses the value before (pre) or after (post) the increment.

For a more complex example, see this answer: Incrementor logic

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • if it immediately increases x's value then why it selects case 10:? – BHAR4T May 04 '17 at 07:27
  • Because the expression uses the *before*-increment value. If you want the *after*-increment value, use `++x`, not `x++`. That's the difference between the two. – Andreas May 04 '17 at 07:28
0

X++ is a post-incrementation. As such, the initial value of x (10) is taken for the switch, and then and only then it is incremented.

This is why your have case 1 and then 11

Kuu Aku
  • 320
  • 2
  • 6
0

The switch statement gets evaluated with x=10 which matches the case 1 and the post increment operator increments its value by 1 at the same time and so x becomes 11. That's why you are getting the result case 1 ::: 11

Master Po
  • 1,497
  • 1
  • 23
  • 42
  • so it is not evaluated with --> switch(x++) ?? – BHAR4T May 04 '17 at 07:28
  • you have a post increment operator there it follows the principle *Use then Change*. It uses the initial value of x (i.e 10) and updates it to 11 immediately. – Master Po May 04 '17 at 07:30
0

In Post Incremet first Switch is evaluated, then only x is incremented. as follows...

x = 10;
switch (x++){...}

switch will be evaluated First.

case 10: will be chosen. Then only x will get incremented by 1 (x becomes 11).

Then statements in the case block get executed ( o/p will be == case 1 ::: 11)

//Hope u may get more clear, also by comparing it with pre increment condition

x = 10;
switch (++x){...}

x will get incremented first.(x becomes 11)

then switch condition get evaluated

case 11: will be chosen.

Then statements in the case block get executed ( o/p will be == case 2 ::: 11)

Jonas
  • 6,915
  • 8
  • 35
  • 53
Jayakumar
  • 16
  • 1