Wrox Programmer Forums
|
BOOK: Beginning Mac OS X Programming
This is the forum to discuss the Wrox book Beginning Mac OS X Programming by Michael Trent, Drew McCormack; ISBN: 9780764573996
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Mac OS X Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 1st, 2005, 11:39 AM
Authorized User
 
Join Date: Aug 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default i++ and ++i in a "for" loop

in Chapter 6, with Tiger and Xcode 2.1, I noticed that an "i++" or a "++i" in a "for" loop produce the same result.
Is it usual ?
I believed before that "++i" produced incrementation before execution of instructions between braces, and "i++" produced incrementation after instructions between braces.
Was I wrong ?
Is it a preprocessor specificity ?

FJ
__________________
FJ
 
Old October 3rd, 2005, 05:38 PM
Authorized User
 
Join Date: Aug 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am ashamed of my question, and I prefer be answering myself.
No importance "i++" or "++i". I was wrong.
The block of instructions during the loop is executed before the incrementation of "i", so pre-incrementation or post-incrementation has no importance.

Please, excuse me.

FJ
 
Old December 3rd, 2005, 12:54 PM
Wrox Author
 
Join Date: Nov 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Logically in a for-loop it doesn't matter. The expression is evaluated after the conditional code is run.

Some programmers claim using "++i" instead of "i++" reduces in smaller object code (i.e., number of machine instructions generated by the compiler) and is often more accurately what you meant to say. I don't know if that's true or not, and if it were true, the few bytes saved doesn't seem all that significant to me. But that's a matter of opinion.

 
Old May 18th, 2009, 07:37 AM
Registered User
 
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The difference in efficiency probably won't be noticeable with variables like integers, but if you are incrementing an instance of a (custom) class it may be. The difference will be that post increment will make a copy (thus calling the possibly rather expensive copy constructor) of itself before executing the increment and then return that copy. With pre increment this copy instruction isn't needed.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with for-each loop athanatos XSLT 0 April 10th, 2006 07:20 PM
Do Until loop with IF crmpicco Classic ASP Databases 2 June 15th, 2005 05:35 PM
For....Loop kliu9 Excel VBA 5 February 10th, 2005 06:43 AM
Do Loop junemo Beginning PHP 8 July 28th, 2004 02:58 AM
nested while loop doesn't loop hosefo81 PHP Databases 5 November 12th, 2003 08:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.