0

I am trying to do in Java:

int i=5;
while(i-- >0) {
   System.out.println(i);

}

When running this program the output is:

4
3
2
1
0

I am very surprised to see 0 in output. I am new in development. Can anyone justify this?

Biffen
  • 6,249
  • 6
  • 28
  • 36
Dheeraj Upadhyay
  • 336
  • 2
  • 12

7 Answers7

5

In your while condition i-- > 0, the variable i is evaluated first and then decremented.

When i reaches the value 1, it will pass the loop test and then get decremented to 0. This is why the print statement shows 0 in the output.

Here is a mnemonic you can use to keep track of how the decrement and increment operators work:

int i = 5;
System.out.println("When i = 5 then:");
System.out.println("i-- is " + i--);
i = 5;
System.out.println("--i is " + --i);

Output:

When i = 5 then:
i-- is 5
--i is 4
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
4

Simply, because you compare i>0 and decrement i afterwards.

// If I is 1, you compare 1>0 and decrement i afterwards.
// This is how the postdecrement operator works
while(i-- >0) {
   System.out.println(i);
}

the loop will behave like the following.

is i=5 > 0?
decrement i to 4
output i = 4.
is i=4 > 0?
decrement i to 3
output i = 3.
...
and so on

As you can see the value you compare to 0 is allways higher then the one you are outputing. This happens due to how the -- operator works. If it´s preceding to the i as --i it will decrement the variable i first and return it´s value afterwards. If it´s not preceding as in your case i-- you will have the value of i returned first and i beeing decremented afterwards.

SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
2

Postdecrement/Increment operator works on the principle "Use first and then Change"

Initially value of i=5, when it enters while loop it will compare value of i first and then it prints the decremented value. Here i will show you each iteration along with checks performed in each iteration,

  1. Now value of i=5(in memory), inside while(5>0), it prints 4.

  2. Now value of i=4(in memory), inside while(4>0), it prints 3.

  3. Now value of i=3(in memory), inside while(3>0), it prints 2.

  4. Now value of i=2(in memory), inside while(2>0), it prints 1.

  5. Now value of i=1(in memory), inside while(1>0), it prints 0.

Hope now you are clear to go ahead. Gud Luck.

Mohit
  • 522
  • 4
  • 10
2

The post-decrement operator -- is like a post-paid service. Like a credit card, first you use, then you pay.

I thought I can give you a real-life idea of what really is occurring in this statement, when i == 1

while(i-- >0) 

So, first you check if i(1)>0. 1>0 So, yes it is. Right after this statement is done, i becomes 0. Then, you print that value.

Alternatively, you might also get this intuition by noticing that although your loop started with i=5, the value 5 never got printed.

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
1

Since you are using the post-decrement operator in the while loop, when i is 1, i-- returns 1, and then inside the loop you get 0 when you print i for the last time.

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

Only because of post decrement operator (i--) will check the condition first then decrease the value of i. Output is giving such. Thank you

int i=5; //initialize with 5
while(i-- >0) { //post decrements operator so, check condition first then decrease the value. 
   System.out.println(i);
}

In first iteration of while loop will check 5 > 0 will be checked after that decrease the value of i and i will become 4 So, Print it 4 not 5.

  • When i = 5 conditional statement will be (5>0) (true) and print 4.
  • i = 4 conditional statement will be (4>0) (true) and print 3.
  • i = 3 conditional statement will be (3>0) (true) and print 2.
  • i = 2 conditional statement will be (2>0) (true) and print 1.
  • i = 1 conditional statement will be (1>0) (true) and print 0.

Now, i became 0 so conditional statement will be (0>0) (False).

So, loop exits.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
-2

To get desired output try this

while(--i >0) {
   System.out.println(i);

}
Raghu Nagaraju
  • 3,278
  • 1
  • 18
  • 25
  • 1
    He didn't ask for the 'corrected code', he asked to explain the current code – Stultuske Apr 07 '16 at 06:37
  • 1
    Still think it's useful to explain ways to change it, the OP might not have known about the post or pre decrement option. – Draken Apr 07 '16 at 06:38
  • @Stultuske comparing this, he can understand difference. Its not bad solution to downvote – Raghu Nagaraju Apr 07 '16 at 06:39
  • 1
    @RaghuNagaraju it doesn't answer the question asked, so it isn't exactly a good 'answer'. sure, he can compare the two, but that doesn't mean he will understand it. He's looking for an explanation, not a solution. – Stultuske Apr 07 '16 at 06:40