• Post Reply Bookmark Topic Watch Topic
  • New Topic

what's the difference between ++i and i+1 in the following program's for loop  RSS feed

 
kiran kumar reddy
Ranch Hand
Posts: 94
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

in the above program's specified for loop what we actually have to do is that assigning 1st index element to 0th index element , 2nd index element to 1st element and so on. considering the statement in the for loop

'elements[i]=elements[i++]; why we are getting correct output when we use i+1 instead of ++i.
whats our requirement is
assigning 1st index element to 0th index element , 2nd index element to 1st element and so on

lets see, for every loop here it goes when we use
elements[i] elements[++i]
i=0 i=1
i=1 i=2
i=2 i=3

so here by using ++i we are getting our reequirement, then why it is not working. did i miss anything?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your question basically boils down to:

Why does this not work:


remember that "++i" CHANGES the value of i. so each time through this loop, you change i twice - once on the assignment line, and once each time you restart the loop.>
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i++ is "assign then increment" and ++i is "increment then assign".

However, given your requirement, to keep it simple why not use another variable j as the destination index.

>
 
kiran kumar reddy
Ranch Hand
Posts: 94
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I think your question basically boils down to:

Why does this not work:


remember that "++i" CHANGES the value of i. so each time through this loop, you change i twice - once on the assignment line, and once each time you restart the loop.>


fine it is changing twice, consider first iteration for i=0, then
elements[0]=elements[1];//as preincrement operator is present i value present in right side will be 1
then for 2nd iteration i due to for loop will be 1, then it will be
elements[1]=elements[2]// i value will be 2 due to pre increament operater.
then for 3rd iteration i will be 2
elements[1]=elements[3];// same here also


same thing is happening when we use i+1 instead of ++i. but i+1 is giving correct output. y? am i missing anything. did i go wrong somewhere?

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kiran kumar reddy wrote:
then for 2nd iteration i due to for loop will be 1, then it will be

no - I will be 2 when you start the second loop. You initialize it to 0. You increment it on the assignment, and again on the loop condition.


i is 0.
i gets incremented to 1. element 1 is assigned to element 0.

you print the value of i (1).

you loop around, and the "i++" in the for statement increments i to 2.

you hit the assignment line, where I gets incremented, and element 3 is assigned to element 2.

you loop around, and i gets incremented to 4.
 
kiran kumar reddy
Ranch Hand
Posts: 94
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
kiran kumar reddy wrote:
then for 2nd iteration i due to for loop will be 1, then it will be

no - I will be 2 when you start the second loop. You initialize it to 0. You increment it to 1 when you hit


i is 0.
i gets incremented to 1. element 1 is assigned to element 0.

you print the value of i (1).

you loop around, and the "i++" in the for statement increments i to 2.

you hit the assignment line, where I gets incremented, and element 3 is assigned to element 2.

you loop around, and i gets incremented to 4.


thanks a lot for your explaination, i got it now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!