 # Java: Difference between while(i-->0) and for(i=10; i>=1; i--)

For the Arranging Cupcakes problem ( http://www.codechef.com/problems/RESQ ) I submit a solution using for-loop and it was A.C. I then modified

``````while(tests-->0){
int start = (int) Math.sqrt(a); //start checking at dimensions where length and width are nearest
//width >= length because of limit on dimensions analyzed

for(int length = start; start >= 1; length -= 1){
if(a%length == 0){ //optimal side length of the rectangle
//minimum difference between optimal length and width
ans += (a/length - length) + "\r\n";
break;
}
}
}
``````

To be (modified code in bold)

``````while(tests-->0){
**int length** = (int) Math.sqrt(a); //start checking at dimensions where length and width are nearest
//width >= length because of limit on dimensions analyzed

**while(length-->0){**
if(a%length == 0){ //optimal side length of the rectangle
//minimum difference between optimal length and width
ans += (a/length - length) + "\r\n";
break;
}
**}**
}
``````

I then tried the second solution and now it gave me wrong answers and got NZEC on CodeChef. Can anyone please explain to me why this is happening?

I always use while(tests–>0) for my different tests cases. Is there some reason I don’t know about that you shouldn’t access variables being used in situations like while(var–>x) ?

A.C. solution using for(int length = start; start >= 1; length -= 1)
http://www.codechef.com/viewsolution/4340798

Wrong solution using while(length–>0)
http://www.codechef.com/viewsolution/4340808

Thanks!

When you use `while(length-- > 0)`, the loop begins with the value of `length` decremented by 1.
When you used `for(int length = start; start >= 1; length -= 1)`, the loop begins without decrementing the value of `length`. This is the difference in the two solutions. It will create problems when `a` is a perfect square.

PS: `for(int length = start; start >= 1; length -= 1)` is a non-terminating loop as the value of `start` is unchanged in the loop. But it will definitely terminate when `length == 1` as 1 is a divisor of all numbers.

1 Like

The first one runs for i = 10 to i = 0 (since you are doing post decrement) and in second case it runs from i = 10 to i = 1. That’s why you get wrong answer for the first case.

1 Like

Yes , there is between while(i–>0) and for(i=10; i>=1; i–);

``````for(i=10; i>=1; i--)
``````

{
//some code
}

What is does is , for the first iteration value of i is 10. It first checks the condition i>=1 , then it enters the loop and after completing the loop value of i decreased(i–) and i becomes 9. And so on…

``````i=10;
while(i-->0)
{
//  some code
}
``````

i-- means that it first takes value i , and in the next step i is decreased.

suppose i=10;
it takes it as while(10>0), then its enters in while loop and i is decreased. So in the while loop value of i is 9 (not 10 , this is where the difference lies)

By the way , you were getting NZEC error because of dividing by zero.(length=0)

I just changed it to as – >

``````while(length>0)
{
``````

` // some code

``````  lenght--;
`}
``````

Happy Coding… 