473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

basic for loop question

Is there any difference between

for(int i = 0; some condition; i++)
{....}

and

for(int i = 0; some condition; ++i) ?
{....}

It seems not, by some experimenting, but I thought it was worth
checking since j++ and ++j usually mean different things in the
language ++c .

Thanks a lot,

Paul Epstein


Jan 25 '08 #1
11 1261
pa**********@att.net wrote:
Is there any difference between

for(int i = 0; some condition; i++)
{....}

and

for(int i = 0; some condition; ++i) ?
{....}

It seems not, by some experimenting, but I thought it was worth
checking since j++ and ++j usually mean different things in the
language ++c .
You mean difference in...performance, semantic,...?

Semantically, there is a difference because in the first case
temporary object is created.
There may or may not be difference in speed and performance. it depends
on C++ implementation you use, optimization, etc.

Cheers
--
Mateusz Loskot
http://mateusz.loskot.net
Jan 25 '08 #2
On 25 Jan, 05:59, Mateusz Loskot <see...@signature.netwrote:
pauldepst...@att.net wrote:
Is there any difference between
for(int i = 0; some condition; i++)
{....}
and
for(int i = 0; some condition; ++i) ?
{....}
It seems not, by some experimenting, but I thought it was worth
checking since j++ and ++j usually mean different things in the
language ++c .

You mean difference in...performance, semantic,...?

Semantically, there is a difference because in the first case
temporary object is created.
There may or may not be difference in speed and performance. it depends
on C++ implementation you use, optimization, etc.
Generally speaking (and the FAQ book from Cline et al concurs:
Q23.08), one should use the prefix increment if the previous value of
the operand is discarded, as in the example above. The rationale is
that the postfix operator needs to make a copy of the operand,
increment the operand, then return the value of the copy; the prefix
operator merely needs to increment the operand and return its value.
Jan 25 '08 #3
On Fri, 25 Jan 2008 02:40:43 -0800, keith wrote:
On 25 Jan, 05:59, Mateusz Loskot <see...@signature.netwrote:
>pauldepst...@att.net wrote:
Is there any difference between
for(int i = 0; some condition; i++)
{....}
and
for(int i = 0; some condition; ++i) ? {....}
It seems not, by some experimenting, but I thought it was worth
checking since j++ and ++j usually mean different things in the
language ++c .

You mean difference in...performance, semantic,...?

Semantically, there is a difference because in the first case temporary
object is created.
There may or may not be difference in speed and performance. it depends
on C++ implementation you use, optimization, etc.

Generally speaking (and the FAQ book from Cline et al concurs: Q23.08),
one should use the prefix increment if the previous value of the operand
is discarded, as in the example above. The rationale is that the
postfix operator needs to make a copy of the operand, increment the
operand, then return the value of the copy; the prefix operator merely
needs to increment the operand and return its value.
Out of interest, does the standard mandate that the postfix version is
*required* to make a copy? Obviously it's not going to make any
difference for an int (and I'd expect an optimising compiler to generate
identical code here), but when incrementing an object with a non-trivial
copy constructor it potentially could make a difference.

--
Lionel B
Jan 25 '08 #4
On 25 Jan, 11:05, Lionel B <m...@privacy.netwrote:
On Fri, 25 Jan 2008 02:40:43 -0800, keith wrote:
Generally speaking (and the FAQ book from Cline et al concurs: Q23.08),
one should use the prefix increment if the previous value of the operand
is discarded, as in the example above. The rationale is that the
postfix operator needs to make a copy of the operand, increment the
operand, then return the value of the copy; the prefix operator merely
needs to increment the operand and return its value.

Out of interest, does the standard mandate that the postfix version is
*required* to make a copy? Obviously it's not going to make any
difference for an int (and I'd expect an optimising compiler to generate
identical code here), but when incrementing an object with a non-trivial
copy constructor it potentially could make a difference.
I doubt the standard has anything to say on the matter, but how else
would you increment the operand (whatever that might mean for the
object concerned), _and_then_ return the previous value?
Jan 25 '08 #5
Lionel B <me@privacy.netwrote in news:fn**********@south.jnrs.ja.net:
>
Out of interest, does the standard mandate that the postfix version is
*required* to make a copy? Obviously it's not going to make any
difference for an int (and I'd expect an optimising compiler to generate
identical code here), but when incrementing an object with a non-trivial
copy constructor it potentially could make a difference.
The way I look at it is that ++i and i++ take the same number of keystrokes
to type and ++i better indicates what I mean (increment i and use that
value). That it has the side benefit of being more efficient without
relying on QOI is just a side benefit. In the case of user defined objects
with overloaded operator++, I am not sure how you could do it without
making a copy, but even if you could I don't think I would like to rely on
QOI to make that decision.

joe
Jan 25 '08 #6
On Jan 25, 6:24 am, pauldepst...@att.net wrote:
Is there any difference between
for(int i = 0; some condition; i++)
{....}
and
for(int i = 0; some condition; ++i) ?
{....}
Yes. One is what other people on the project are doing in
similar cases. The other is not.
It seems not, by some experimenting, but I thought it was
worth checking since j++ and ++j usually mean different things
in the language ++c .
The difference is in the returned value. In this case, the
returned value is being ignored.

There have been claims (and still are, by people who prefer
speculation to measurement) that the prefix version will be
faster for user defined types, like iterators. For this reason,
on a green fields project, it is probably simpler to choose
prefix notation, and avoid the arguments. If there is any
existing code, however, just use whatever it uses, and don't
worry about it.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 25 '08 #7
On Fri, 25 Jan 2008 04:58:18 -0800, keith wrote:
On 25 Jan, 11:05, Lionel B <m...@privacy.netwrote:
>On Fri, 25 Jan 2008 02:40:43 -0800, keith wrote:
Generally speaking (and the FAQ book from Cline et al concurs:
Q23.08), one should use the prefix increment if the previous value of
the operand is discarded, as in the example above. The rationale is
that the postfix operator needs to make a copy of the operand,
increment the operand, then return the value of the copy; the prefix
operator merely needs to increment the operand and return its value.

Out of interest, does the standard mandate that the postfix version is
*required* to make a copy? Obviously it's not going to make any
difference for an int (and I'd expect an optimising compiler to
generate identical code here), but when incrementing an object with a
non-trivial copy constructor it potentially could make a difference.

I doubt the standard has anything to say on the matter, but how else
would you increment the operand (whatever that might mean for the object
concerned), _and_then_ return the previous value?
Sorry, I obviously didn't express myself very well... the situation I had
in mind is where the return value of postfix increment is *discarded*.
Then, is the compiler allowed *not* to make a copy of the object?

--
Lionel B
Jan 25 '08 #8
On 2008-01-25 17:30, Lionel B wrote:
On Fri, 25 Jan 2008 04:58:18 -0800, keith wrote:
>On 25 Jan, 11:05, Lionel B <m...@privacy.netwrote:
>>On Fri, 25 Jan 2008 02:40:43 -0800, keith wrote:

Generally speaking (and the FAQ book from Cline et al concurs:
Q23.08), one should use the prefix increment if the previous value of
the operand is discarded, as in the example above. The rationale is
that the postfix operator needs to make a copy of the operand,
increment the operand, then return the value of the copy; the prefix
operator merely needs to increment the operand and return its value.

Out of interest, does the standard mandate that the postfix version is
*required* to make a copy? Obviously it's not going to make any
difference for an int (and I'd expect an optimising compiler to
generate identical code here), but when incrementing an object with a
non-trivial copy constructor it potentially could make a difference.

I doubt the standard has anything to say on the matter, but how else
would you increment the operand (whatever that might mean for the object
concerned), _and_then_ return the previous value?

Sorry, I obviously didn't express myself very well... the situation I had
in mind is where the return value of postfix increment is *discarded*.
Then, is the compiler allowed *not* to make a copy of the object?
There is something called the "as if" rule (section 1.9 of the standard)
which basically says that as long as the observable behaviour of the
program during execution is the same as if things were done as described
in the standard then the program is conforming. So if you do not try to
make any use of the returned value then the compiler should be free to
do nothing except increment the value.

For an integer this should be simple but for user defined types the
compiler might have some trouble to decide how much of the code is
needed an what it can safely skip, which is why it is generally
recommended to use the prefix version.

--
Erik Wikström
Jan 25 '08 #9
Joe Greer wrote:
Lionel B <me@privacy.netwrote in news:fn**********@south.jnrs.ja.net:
>>
Out of interest, does the standard mandate that the postfix version is
*required* to make a copy? Obviously it's not going to make any
difference for an int (and I'd expect an optimising compiler to generate
identical code here), but when incrementing an object with a non-trivial
copy constructor it potentially could make a difference.

The way I look at it is that ++i and i++ take the same number of
keystrokes to type and ++i better indicates what I mean (increment i and
use that value).
You don't use the value (at least not in the example the OP posted). You
discard it. So in that regard, both variants are the same. With ++i, you
discard the new value, with i++, you discard the old one.

Jan 26 '08 #10
James Kanze wrote:
On Jan 25, 6:24 am, pauldepst...@att.net wrote:
>Is there any difference between
>for(int i = 0; some condition; i++)
{....}
>and
>for(int i = 0; some condition; ++i) ?
{....}

Yes. One is what other people on the project are doing in
similar cases. The other is not.
>It seems not, by some experimenting, but I thought it was
worth checking since j++ and ++j usually mean different things
in the language ++c .

The difference is in the returned value. In this case, the
returned value is being ignored.

There have been claims (and still are, by people who prefer
speculation to measurement) that the prefix version will be
faster for user defined types, like iterators. For this reason,
on a green fields project, it is probably simpler to choose
prefix notation, and avoid the arguments. If there is any
existing code, however, just use whatever it uses, and don't
worry about it.
Considering that - except for the potential difference in execution time -
there is no difference, why use the postfix version? If I definitely don't
gain anything from one solution, but might (even if chances are slim) gain
something from the other, then I use of course that one.
Jan 26 '08 #11
On Jan 26, 6:15 pm, Rolf Magnus <ramag...@t-online.dewrote:
James Kanze wrote:
[...]
There have been claims (and still are, by people who prefer
speculation to measurement) that the prefix version will be
faster for user defined types, like iterators. For this reason,
on a green fields project, it is probably simpler to choose
prefix notation, and avoid the arguments. If there is any
existing code, however, just use whatever it uses, and don't
worry about it.
Considering that - except for the potential difference in
execution time - there is no difference, why use the postfix
version?
Typically, because it is the established coding convention where
you're working.
If I definitely don't gain anything from one solution, but
might (even if chances are slim) gain something from the
other, then I use of course that one.
Supposing all other things equal. They usually aren't. If your
shop has the established tradition of using the postfix op
(frequent, if for no other reason that that's what K&R did),
that's a very strong reason for using postfix---it requires (or
should require) significant motivation to change. And that
significant motivation isn't there.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jan 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rhino | last post by:
Given a loop like this: for (int ix=0; ix<myArray.length; ix++) { System.out.println(ix); } or especially this: for (int ix=0; ix<Math.sqrt(shoeSize)*hairlength/(IQ); ix++) {...
14
by: deko | last post by:
For some reason this does not seem to be working... Am I missing something basic? Dim rst As DAO.Recordset Dim db As DAO.Database Set db = CurrentDb Set rst = db.OpenRecordset("qryEmailS") '...
56
by: Dave Vandervies | last post by:
I just fixed a bug that some of the correctness pedants around here may find useful as ammunition. The problem was that some code would, very occasionally, die with a segmentation violation...
6
by: Tico Tech | last post by:
Hi: I have a question about the Microsoft Communications Control Version 6.0 from visual studio .net. I have this code: Dim Buffer As String Do Buffer = AxMSComm1.Input() //Error, it does...
1
by: Q | last post by:
Hello you all, I have to create an application which writes data to a serial port, waits for an answer and read this answer from the same serial port. I just don't know where to start. Any...
21
by: Roland | last post by:
The following issue is puzzling me: There are 2 ways of writing the code below: .... Dim fnt as Font = New Font(...) DrawString(myText, fnt,...) fnt.dispose(). or DrawString(myText, New...
22
by: Cesar G. Miguel | last post by:
I've been studying python for 2 weeks now and got stucked in the following problem: for j in range(10): print j if(True): j=j+2 print 'interno',j What happens is that "j=j+2" inside IF...
7
by: MikeB | last post by:
Hello All, I am new to ajax and wanted to start by trying something simple. I have a web form with an updatepanel and then inside the update panel I have a listbox. Then outside of the updatepanel...
13
by: pauldepstein | last post by:
I have code that looks like this: for (int counter = 0; counter < 100; ++ counter) { { static int blah = 0; if (blah == 0) // do something; blah ++; }
3
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.