Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why i++ instead of ++i in for loops

Reply
Thread Tools

why i++ instead of ++i in for loops

 
 
newtothis
Guest
Posts: n/a
 
      10-28-2003

I have been reading various texts in C/ C++ and Java. The for lops all
run along the lines of :



int i ;

for(i = 0 ; i < 4 ; i++)

{

.....

}



I understand the difference between ++i and i++, but I can not see why
i++ is used in these loops when, as I understand it, the steping
expression would be more alined to i = i + 1 in this type of case. The
logic of i++ is not the same. The final result might but the evaluation
process is not.



Ok I realise at the end of the loop the value of i is the same which
ever method you use. But that does not explaine why the preference.



Also from what I can see at the assembly code level



int i ;

for(i = 0 ; i < 4 ; ++i)

{

.....

}



and



int i ;

for(i = 0 ; i < 4 ; i++)

{

.....

}



are exactly the same.



Could some please explain the need to use i++ over ++i.


--
Posted via http://dbforums.com
 
Reply With Quote
 
 
 
 
Peter van Merkerk
Guest
Posts: n/a
 
      10-28-2003
* snip *

> Could some please explain the need to use i++ over ++i.


Strictly speaking there is no real need for i++, since "int x = i;
++i;" yields the same result as "int x = i++;". As you have noticed in
for loops with int as loop variable using ++i or i++ doesn't make a
difference with most compilers. Note that with iterators the ++i version
may very well be more efficient than i++.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl






 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      10-28-2003
newtothis wrote:
> I have been reading various texts in C/ C++ and Java. The for lops all
> run along the lines of :
>
> int i ;
>
> for(i = 0 ; i < 4 ; i++)
>
> {
> ....
> }
>
> I understand the difference between ++i and i++, but I can not see why
> i++ is used in these loops when, as I understand it, the steping
> expression would be more alined to i = i + 1 in this type of case. The
> logic of i++ is not the same. The final result might but the evaluation
> process is not.


Good call, "newtothis"!

>
> Ok I realise at the end of the loop the value of i is the same which
> ever method you use. But that does not explaine why the preference.
>
> Also from what I can see at the assembly code level
>
> int i ;
>
> for(i = 0 ; i < 4 ; ++i)
> {
> ....
> }
>
> and
>
> int i ;
>
> for(i = 0 ; i < 4 ; i++)
> {
> ....
> }
>
> are exactly the same.
>

Which, by itself is irrelevant -- and only because a compiler can see
that the return value of `i++', i.e. the original value of `i' is
thrown
away.
>
> Could some please explain the need to use i++ over ++i.
>

Sorry. Can't be done. There is no `need'.
What there *is* is `force of habit'. Historical artifact. Custom. Idiom.

While it is true that there is likely to be no difference in the
generated code when dealing with basic types, there often *is* a
difference -- and a potentially significant one -- when dealing with
user defined types (where `++', prefix and postfix are overloaded
operators).

Again, good call. Using the prefix form in such loops is to be
preferred -- it's just a hard habit to break!

HTH,
--ag

>
> --
> Posted via http://dbforums.com




--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      10-28-2003
> I have been reading various texts in C/ C++ and Java. The for lops all
> run along the lines of :


> int i ;


> for(i = 0 ; i < 4 ; i++)


> {


> ....


> }


> I understand the difference between ++i and i++, but I can not see why
> i++ is used in these loops when, as I understand it, the steping
> expression would be more alined to i = i + 1 in this type of case. The
> logic of i++ is not the same. The final result might but the evaluation
> process is not.


> Ok I realise at the end of the loop the value of i is the same which
> ever method you use. But that does not explaine why the preference.


You have hit on one of the reasons that "Accelerated C++" always uses the
"++i" form unless something is going to be done with the value of the
expression. So, for example, we would write

++i;

instead of

i++;

but we would write

n = a[i++];

if it were appropriate to do so--the point being that the value of "i++" is
used in the second example.

Incidentally, instead of

for (i = 0; i < 4; i++)

we would write

for (i = 0; i != 4; ++i)

because that way, we can use the same general form of loop for all kinds of
iterators in addition to integers.


 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      10-28-2003

"newtothis" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> I understand the difference between ++i and i++, but I can not see why
> i++ is used in these loops when, as I understand it, the steping
> expression would be more alined to i = i + 1 in this type of case. The
> logic of i++ is not the same. The final result might but the evaluation
> process is not.
> Ok I realise at the end of the loop the value of i is the same which
> ever method you use. But that does not explaine why the preference.


I think you've answered your own question. In that case, the result is the
same so it really doesn't matter. If it doesn't matter, i++ seems more
aesthetically pleasing to most people, since logically you see the variable
first, and then you see what to do with it. If these were classes with
member functions, it would look like this.

Int i(1);
i.increment();
vs.
increment().i;

The first looks more natural.


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      10-28-2003

"Andrew Koenig" <(E-Mail Removed)> wrote in message
news:3xvnb.197400$(E-Mail Removed)...

> ...So, for example, we would write
>
> ++i;
>
> instead of
>
> i++;
>
> but we would write
>
> n = a[i++];
>
> if it were appropriate to do so--the point being that the value of "i++"

is
> used in the second example.
>


What do you mean that "the value of "i++" is used in the second example"?
If you're referring to n = a[i++], then the value that is used to index into
a is i, not i++. The increment is done afterwards. If, instead, you're
just saying that you should only use i++ when you need the value of i++,
again, I don't see how you can get that value, except in a following
statement, as in

x = a[i++];
n = a[i]; // here we use the incremented value of i

I think I see what you were getting at, but your phrasing is a little
confusing.

-Howard




 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      10-28-2003
> > ...So, for example, we would write
> >
> > ++i;
> >
> > instead of
> >
> > i++;
> >
> > but we would write
> >
> > n = a[i++];
> >
> > if it were appropriate to do so--the point being that the value of "i++"
> > is used in the second example.
> >

>
> What do you mean that "the value of "i++" is used in the second example"?
> If you're referring to n = a[i++], then the value that is used to index

into
> a is i, not i++.


I meant exactly what I said: The value of i++ is used in the second
example. Now, as it happens, if i is an integer, then the value of i++ is a
copy of what the value of i was before i was incremented. If i is a
user-defined type, the value of i++ might be something different. Either
way, evaluating the expression a[i++] involves using the value of i++.


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      10-28-2003

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:bnm57f$sbn$07$(E-Mail Removed)-online.com...
> Howard wrote:
>
> >
> > "Andrew Koenig" <(E-Mail Removed)> wrote in message
> > news:3xvnb.197400$(E-Mail Removed)...
> >
> >> ...So, for example, we would write
> >>
> >> ++i;
> >>
> >> instead of
> >>
> >> i++;
> >>
> >> but we would write
> >>
> >> n = a[i++];
> >>
> >> if it were appropriate to do so--the point being that the value of
> >> "i++"

> > is
> >> used in the second example.
> >>

> >
> > What do you mean that "the value of "i++" is used in the second
> > example"? If you're referring to n = a[i++], then the value that is
> > used to index into a is i, not i++.

>
> i and i++ have the same value. The value of postfix++ is the value that
> the operand had before the increment.
>
> > The increment is done afterwards.

>
> Right.
>
> > If, instead, you're just saying that you should only use i++ when you
> > need the value of i++, again, I don't see how you can get that value,

>
> int a = i++;
>
> now a has the value of i++.
>
> > except in a following statement, as in
> >
> > x = a[i++];
> > n = a[i]; // here we use the incremented value of i
> >
> > I think I see what you were getting at, but your phrasing is a little
> > confusing.

>
> You seem to be confused by the term "value of i++". The value of i++ is
> what you get when you e.g. assign "i++" to something, not the value of
> i after i++ has been executed.
>


That's exactly where I was confused. Which is why I asked what he meant by
"the value of i++".

It seems very strange to refer to the "value of i++" unless you were
refering to the value you get by performing the ++ operation upon the i
variable, which is obviously not the intent. To me, it would be like
referring to the "value of 7+1", and intending to refer to the value 7
(before adding one), whereas in normal conversation, you'd be talking about
the value 8, which is what 7+1 is equal to. See why it's confusing (at
least to me)?

No argument here on the "truth" of what Andrew said...just confusion on the
terminology.

-Howard






 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      10-28-2003

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:bnmaat$ir6$06$(E-Mail Removed)-online.com...

> Seems logical to me:
>


Oh well. I find it confusing, you don't. We'll both live.

-Howard




 
Reply With Quote
 
newtothis
Guest
Posts: n/a
 
      10-28-2003

So from what has been said, except for may be the situation of an
oveloaded operator, the only reason for ++i vs i++ in the for loop is
programmer preference.

This then is close to the arguments of underscore vs mixedcase
vqariables.

Personal preference.



Why then dont the writeres of texts explain this instead of trying to
lay down some form of undefinable law?



Thanks for these comments as it clears up some programming "rules"
people have tried to push on me.


--
Posted via http://dbforums.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
looping through json array loops through the characters instead ofthe values Aaron Javascript 2 04-10-2011 05:58 PM
regex search loops instead of findall brad Python 1 08-05-2008 06:15 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM



Advertisments