3

for(int i(5) ; i-->0 ; ) { //use i as iterator here }

Is this the same as

for(int i=5; i>0 ; i--) ??

I tried to find similar declarations in google but didn't find anything

Also please suggest cleaner ways to declare the same thing ?

7
  • 2
    I'm going to go ahead and say - even if it does work, never ever do this - it would be semantically wrong. Commented Jan 10, 2013 at 15:37
  • Yes it is someone else's code. Really ol, I am trying to make it cleaner? any suggestions for a clean declaration to do the same thing ? Commented Jan 10, 2013 at 15:41
  • 2
    for(int i=4; i>=0 ; i--) Commented Jan 10, 2013 at 15:46
  • Yes, a for loop can be declared like that. No, it should never be, because as you've discovered it's much harder to understand than the usual form. Commented Jan 10, 2013 at 15:51
  • @MarcGlisse thanks I will try that implementation Commented Jan 10, 2013 at 15:51

4 Answers 4

8

No these aren't the same. In the first case, i will be decremented before the loop body is executed for the first time.

See http://ideone.com/AbHF3m for a working demo.

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

10 Comments

So it is --i instead of i-- ?
@user1967230: no. That would decrement it before checking the loop condition.
Can you type an alternate way to declare the first statement ?
Not true. i-- FIRST evaluates i and then decrements so i-->0 evaluates to 5, then compares 5>0 and then decrements to 4 in the first iteration
@Y.Ecarri: Yes, but then i is 4 when the loop body executes. Unlike the other version, where it's 5 the first time it executes.
|
4

No, it's not the same. The first version compares the value of i before the decrement, and performs the decrement before each iteration; the second compares the value after the decrement, and performs the decrement after each iteration.

So the first iterates over {4,3,2,1,0}, while the second iterates over {5,4,3,2,1}.

4 Comments

Not true. i-- FIRST evaluates i and then decrements so i-->0 evaluates to 5, then compares 5>0 and then decrements to 4 in the first iteration.
@user1967230: The i>0 in that version has no effect, but otherwise yes; i-- will give the same boolean result as i-->0 if i starts out non-negative.
@Y.Ecarri: That's exactly what I said. The first version compares the value of i before the decrement (i.e. compares 5>0), and performs the decrement before each iteration (i.e. decrements to 4 for the first iteration).
Ok, you're right. Both loops iterate the same number of times but the variable will have different values.
4

It's not the same. In the first case, the value of i will be 4 in the first iteration and 0 in the last one. In the second case, i will be 5 in the first iteration and 1 in the last one.

Edit

For @Y.Ecarri:

First case:

  1. Set i to 5
  2. Compare i > 0, that is, 5 > 0
  3. Decrement i. i is now 4
  4. Enter body of loop with i having value 4
  5. Body finishes

Second case:

  1. Set i to 5
  2. Compare i > 0, that is, 5 > 0
  3. Enter body of loop with i having value 5
  4. Body finishes
  5. Decrement i. i is now 4

2 Comments

Not true. i-- FIRST evaluates i and then decrements so i-->0 evaluates to 5, then compares 5>0 and then decrements to 4 in the first iteration.
@Y.Ecarri: You keep posting this, but that's exactly why the two versions are different!
0

They are not the same as others have pointed out as order is:

  1. first part of the for loop (before first semicolon) is evaluated once only at the start.
  2. Second section is evaluated prior to the block of the code, which will not be evaluated at all of the second section returns a zero-value (false, null pointer or 0 numeric). Within this section which parses as i-- > 0, the comparison takes place before the decrement, as it is a post-decrement operator. Thus when i was initially 1 it will be true, but then i will decrement to 0 for what comes next which is
  3. The body of the loop.
  4. The 3rd part of the for loop is executed at the end of the block of the loop if it is not abnormally terminated (with break or return).

If i is a signed int as here so it can reach -1, you can replace with

for( int i=4; i >= 0; --i ) { // body 

which looks a bit more obvious in its intention. If you have an unsigned int e.g. size_t, you can still make it look a bit more obvious in its intent by switching the order

for( size_t i = 5; 0 < i--; ) { // body 

You can also remove the 0 and the comparison altogether and just do

for( size_t i = 5; i--; ) { // body 

In both of the above two I am still uncomfortable with the fact that you start i=5 when you really want it to start as 4 for the first iteration. So I might even switch to do...while.. Of course I have to initialise i outside the loop.

size_t i = 5; do { // body --i; // clearly 4 now on first iteration } while( i != 0 ); 

3 Comments

i is a unsigned long int, none of the declarations seem clean enough ? thanks though
Note that the value of i is different between the first and the last two examples. I will be decremented before the execution of the loops body while in the first example it gets decremented after the body. Have a look at Mike Seymours answer. Maybe you should highlight this with //body i =4 in your example.
sorry edited the first block to use i=4 as the first value, which now gives you the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.