loops

Can anybody help me with 2 these problems ?
1)
int count = 3;
while (count-- > 0)
cout << count << " ";

the output of this code is 2,1,0

But
2)int count = 3;
while (--count > 0)
cout << count << " ";

the output of this code is 2,1 .

Can you help me understand why it is?
Last edited on
Please use code tags when posting code, to make it more readable.

Do you understand the difference between --count and count-- ?
yes. but in those 2 cases I am so confused. It is so different from what i've knew
Just think about it logically. If you know the difference between those two, then you'll know that they evaluate to different values, so that the expression (count-- > 0) will be true under different values of count from those for which (--count > 0) is true.

EDIT: Most people think they understand what the difference between pre-increment and post-increment is, but don't really. If the problem isn't making sense to you, I'd recommend looking up what the difference really is, in case you've misunderstood it.
Last edited on
Ah! Could you know why the first case it appears 0, but in 2 it doesn't?
As I said:

If the problem isn't making sense to you, I'd recommend looking up what the difference really is, in case you've misunderstood it.
x++ increments the value of variable x after processing the current statement.

++x increments the value of variable x before processing the current statement.
We have the condition that count is always greater than 0. But in the situation 1, as I think the value count will never be 0.
x++ increments the value of variable x after processing the current statement.

++x increments the value of variable x before processing the current statement.


As I suspected - you've only half-understood it, in the vague kind of way that most people think they have.

To reiterate one more time:

I'd recommend looking up what the difference really is
Topic archived. No new replies allowed.