Let’s look at the following day-to-day example of Java.
package loop;
final public class Main
{
public static void main(String[] args)
{
long temp=1000000000;
while(temp--!=0)
{
temp-=temp++;
System.out.println("Inside loop = "+temp);
}
System.out.println("Outside loop = "+temp);
}
}
In the above simple code, the while loop iterates over only once though the local variable temp of type long contains a large value which is 1000000000.
Through the statement System.out.println("Inside loop = "+temp);, it displays Inside loop = 0 and through this statement System.out.println("Outside loop = "+temp);, it displays Outside loop = -1. Why is it so?
because when it hits the while loop for the last time, it does a temp–
ie…. temp == 0, so it will quit the while loop, and do a — after the comparison.
the joys of post increment/decrement.