C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

why i++ instead of ++i in for loops
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
newtothis
Guest





PostPosted: Tue Oct 28, 2003 1:34 pm    Post subject: why i++ instead of ++i in for loops Reply with quote




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
Back to top
Peter van Merkerk
Guest





PostPosted: Tue Oct 28, 2003 2:20 pm    Post subject: Re: why i++ instead of ++i in for loops Reply with quote



* snip *

Quote:
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







Back to top
Artie Gold
Guest





PostPosted: Tue Oct 28, 2003 2:27 pm    Post subject: Re: why i++ instead of ++i in for loops Reply with quote



newtothis wrote:
Quote:
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"!

Quote:

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.
Quote:

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

Sorry. Can't be done. Wink 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

Quote:

--
Posted via http://dbforums.com



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


Back to top
Andrew Koenig
Guest





PostPosted: Tue Oct 28, 2003 3:01 pm    Post subject: Re: why i++ instead of ++i in for loops Reply with quote

Quote:
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.



Back to top
jeffc
Guest





PostPosted: Tue Oct 28, 2003 3:16 pm    Post subject: Re: why i++ instead of ++i in for loops Reply with quote


"newtothis" <member45182 (AT) dbforums (DOT) com> wrote

Quote:

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.



Back to top
Howard
Guest





PostPosted: Tue Oct 28, 2003 3:26 pm    Post subject: Re: why i++ instead of ++i in for loops Reply with quote


"Andrew Koenig" <ark (AT) acm (DOT) org> wrote


Quote:
...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





Back to top
Andrew Koenig
Guest





PostPosted: Tue Oct 28, 2003 4:33 pm    Post subject: Re: why i++ instead of ++i in for loops Reply with quote

Quote:
...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++.



Back to top
Howard
Guest





PostPosted: Tue Oct 28, 2003 5:38 pm    Post subject: Re: why i++ instead of ++i in for loops Reply with quote


"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote

Quote:
Howard wrote:


"Andrew Koenig" <ark (AT) acm (DOT) org> wrote in message
news:3xvnb.197400$0v4.15332398 (AT) bgtnsc04-news (DOT) ops.worldnet.att.net...

...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







Back to top
Howard
Guest





PostPosted: Tue Oct 28, 2003 7:22 pm    Post subject: Re: why i++ instead of ++i in for loops Reply with quote


"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote


Quote:
Seems logical to me:


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

-Howard





Back to top
newtothis
Guest





PostPosted: Tue Oct 28, 2003 11:11 pm    Post subject: Re: why i++ instead of ++i in for loops Reply with quote


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
Back to top
Duane Hebert
Guest





PostPosted: Wed Oct 29, 2003 1:21 am    Post subject: Re: why i++ instead of ++i in for loops Reply with quote


Quote:
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.

Actually that's not true. i++ has to create a temporary and ++i doesn't.
This may
not be a big deal when i is an int, but it gets pretty inefficient when i is
a larger type or
an iterator etc.



Back to top
Andrew Koenig
Guest





PostPosted: Wed Oct 29, 2003 7:29 am    Post subject: Re: why i++ instead of ++i in for loops Reply with quote

Quote:
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.

Not quite. There's no difference in practice if i is an integer variable,
but if it's an iterator, there may be a performance difference. There is
also the philosophical question of whether it's a good thing to ask the
machine to do work that you know is going to be discarded.

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

Some of them do.



Back to top
Peter Koch Larsen
Guest





PostPosted: Wed Oct 29, 2003 9:01 am    Post subject: Re: why i++ instead of ++i in for loops Reply with quote

newtothis <member45182 (AT) dbforums (DOT) com> wrote

Quote:
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.

Yes.

Quote:

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

Not at all. In situations as the one you mentioned, you usually
acquire a habit of either writing i++ or ++i. If you write ++i you are
assured of optimal performance, if you write i++, you're not. Thus you
should always prefer the ++i version.
Quote:

Personal preference.



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

I do believe that most good textbooks will tell you that you should

prefer ++i. At least they ought to do so.
Quote:


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

Kind regards
Peter Koch Larsen

Back to top
Rolf Magnus
Guest





PostPosted: Wed Oct 29, 2003 1:31 pm    Post subject: Re: why i++ instead of ++i in for loops Reply with quote

newtothis wrote:

Quote:

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.

For builtin types, it really doesn't matter. But in C++, you can write
and operator++ for your own class. And then it might matter, becaure
postfix ++ has to create a copy of the object so that the old value can
be returned. If you don't need the return value, that copy is
unnecessary. If the compiler doesn't do named return value
optimization, that copy might even need to be copied again, and all
that just to throw the result away. The postfix operator++ for an own
class might look something like this:

MyClass MyClass::operator++(int)
{
MyClass retval(*this); // copy the object
// do whatever is needed to "increment" the object
reutrn retval; // return the copy by value
}

while prefix ++ might look like:

MyClass& MyClass::operator++()
{
// do whatever is needed to "increment" the object
return *this; // return a refernce to the object
}

Therefore, it's considered a good habit to always use prefix ++ if the
return value is not needed.

Back to top
newtothis
Guest





PostPosted: Wed Oct 29, 2003 1:32 pm    Post subject: Re: why i++ instead of ++i in for loops Reply with quote


Thanks for all the comments. I did not intend to cause a general stir.
This though has confirmed that my use of ++i, when appropiate, in such
loops has been correct.



I realise at times the use of i++ is appropiate and is dependent on the
intent of the design and code.



I now just have to get over the issues of mixed case or underscores in
variables. That seems to have been discussed at some length anyway.



Thanks again



Newtothis


--
Posted via http://dbforums.com
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.