Post increment operation in while while (java)

I recently came across this question

int i = 10;
while (i++ <= 10) {
    i++;
}
System.out.print(i);

Answer: 13, can someone explain how is it 13?

+4
source share
2 answers

This is one of the alternative ways that I could wrap myself around this. Let be f(ref i)a function that takes in I by reference and increases its value by 1. So,f(ref i) = i + 1

Now that we have f(ref i), the above code can be written as

int i = 10
while( (f(ref i) -1) <=10 )
{
   f(ref i);
}

I would replace with f(ref i)equivalent values ​​I will return it and get an answer like

while(11 - 1 <= 10) {12}
while (13 -1 <= 10) -> break;

so i = 13.

0
source
  • i = 10. Look at i, compare with 10
  • i = 10. 10 <= 10, so enter a loop.
  • i = 10. Increment i (according to while expression)
  • = 11. i
  • = 12. i, 10
  • = 12. 12 <= 10, .
  • = 12. ( while)
  • = 13
+8

All Articles