0

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?

6
  • 2
    while(temp--!=0) gets executed once more before the loop is terminated Commented Nov 13, 2011 at 20:39
  • 1
    temp -= temp++; it's lines like these that give me a headache. Commented Nov 13, 2011 at 20:40
  • 1
    @mikejones That won't exhibit the same behavour because temp is not reduced by one each step Commented Nov 13, 2011 at 20:49
  • 1
    @Lion is this puzzle code you are trying to understand or real code you are trying to fix? Commented Nov 13, 2011 at 21:03
  • 1
    @Bob Vale:) Please, don't mind at all about my saying but I just play with some tricky codes in Java believing that it may make my way of learning Java more interesting! Commented Nov 13, 2011 at 22:34

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

temp-- inspects the contents then decreases the value

so if temp==0

temp--!=0 returns false because temp==0; But straight after the comparison temp is reduced to -1

if you change the while loop to read

while (--temp!=0)

Then you will find inside and outside the loop have the same value

EDIT -- (I missed the a bit about why one loop)

temp-=temp++;

This is reducing temp by temp, written longer

temp = temp - (temp++);

because the evaluation is performed after reading its value, this ends up becoming

temp = temp - temp;

or by elimination

temp = 0;

I am uncertain as to the defined behaviour for an assignment with an incrementor of the same variable on the right hand side but from your explaination of the results you are getting I would assume that the left hand assignent is taking precedence over the right hand side. This may be moving into undefined territory and definitely should be avoided.

2 Comments

while (--temp!=0) gets stuck into an infinite loop.
@Lion, looking at it yes it will, because the logic inside will never let it work. Loop1: temp will change to 999999999 inside while condition (before evaluation) and 0 during while body Loop2: temp will change to -1 inside while condition (before evaluation) and 0 during while body Loopn: repeat loop2 add infinitum
1

After while(temp--!=0) have been evaluated it will decrease the variable temp by one. Simply put: During the evaluation it will be zero but as soon as the variables has been used it will be decreased.

Comments

1

The last operation before the system out println from the outer loop is temp--!=0 so you are decrementing the value to -1.

Comments

1

In this line:

temp-=temp++;

You make temp zero, and afterwards, in this line:

while(temp--!=0)

The test returns false and the loop exists, but right befor exiting, temp is decremented one last time, ending with a -1 value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.