Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pre and postfix operator in for loop

 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm confused about the below code.
There is no difference in the pre or postfix operator location, whether I use ++1 or 1++. I have done a search on this site and have learned about what goes in memory when using either one, and I understand how it works when I use it with a while loop, but in the for loop I'm wondering if it ever makes any difference where the ++ operator goes. Can someone walk me through what happens at each stage with this code, so I will understand it better? Thank you so much!

Von


 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except for possible performance differences you won't even be able to measure properly (so don't even bother ), when used as a separate statement there is no difference between i++ and ++i. There is only a difference when used in an expression. For instance, array[index++] is remarkably different from array[++index].

But like I said, if the statement is only i++ or ++i, both ignore the return of the statement and simply increment i.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The prefix/postfix operators do their work within the context of a single statement. So for example:

In line 1 i is set to 3
In line 2 first i is incremented: i = i+1, i=4.
Then m is set to i: m=i, m=4
In line 3 j is set to i: j=i, j=4


In line 1 i is set to 3
In line 2 first m is set to i: m=i, m=3
Then i is incremented: i = i+1, i=4
In line 3 j is set to i: j=i, j=4

Notice how the value of m behaves differently with the pre/post fix, but the value of j behaves the same.

In a for loop, since each part of the for loop is its own statement, whether you use ++i, or i++ does not matter. Just like it doesn't matter to the value of j. The variable comparison is made as the next statement after the increment is done.
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A related question came up today: here.
 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand all the examples you all have given me, but I still don't understand how it works in the for loop.

with ++i, I get that it increments one and then assigned, but with i++ what happens to the i while it is waiting to be incremented? Exactly when does it increment?

I guess what I am asking is, with i++, when this statement is executed, what happens next? For example, as with my previous example:



when it comes to i++ statement, what happens directly after that? Does i get printout out as 1, or does it get evaluated again?

And when it comes to ++i, I know that i instantly becomes 2, but is that before it has printed out the number 1???

Does the print statment always happen right after the evaluation?

Maybe I'm not understanding the for loop, though I thought I did.

Can someone walk me through each action with the for loop and the i++ or ++i? I'm trying to understand WHY there is no difference in output.

Sorry, I'm such a beginner (and a dumb one, at that). If I can't get this I'm thinking of giving up the whole thing. This will be my test.

Thanks for any help!!
Von
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said earlier:
"and I understand how it works when I use it with a while loop"

In the for loop, it works no different than in a while loop. The for loop that is written like this:

behaves just like a while loop written like this:


 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so in your example (as in the for loop), what I am asking is:

Exactly WHEN does the i increment to 1?



I understand that with ++1 it increments right away, but for i++ when does it turn into a 1?

I think I am confusing the single statement with a statement that assigns the result to a variable, such as in c = ++i or c = i++. In those cases it makes a difference, but if the statement is just i++ as in your while loop it does not matter.

Have I got it right now?

Thanks, Von
 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the above I meant to say:


I understand that with ++i it increments right away, but for i++ when does it turn into a 1?

rather than ++1.

Sorry, and thanks.


 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since you under stand the wile loop, see if this helps.



Loop 1 and loop2 are exactly the same.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vonique Leary wrote:Okay, so in your example (as in the for loop), what I am asking is:

Exactly WHEN does the i increment to 1?

...

I understand that with ++1 it increments right away, but for i++ when does it turn into a 1?


By the end of the statement (where the ; is).


I think I am confusing the single statement with a statement that assigns the result to a variable, such as in c = ++i or c = i++. In those cases it makes a difference, but if the statement is just i++ as in your while loop it does not matter.

Have I got it right now?

Thanks, Von


Exactly.
 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Steve. Your answers really helped, especially your last one. That really make it click for me.

Thanks, everyone!

Love this board.

Von
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic