Amazon.com Widgets page 84 ---- n++ <> ++n
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 30, 2014, 11:52:59 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Errata
| | |-+ 5th printing
| | | |-+ page 84 ---- n++ <> ++n
Pages: [1] Go Down
Print
Author Topic: page 84 ---- n++ <> ++n (Read 1293 times)
designdb
Newbie
*
Posts: 3


Email




on: July 11, 2009, 08:44:21 PM

On page 84 you state "Some programmers prefer to put the ++ or -- after the variable name, as in n++ or bean_counter--.  This is acceptable and is a matter of personal preference."

This is incorrect.  I'm not quite sure what the technical explanation is, but effectively ++n has a higher order of precedence than n++.

Sample Code:

   int x;
   
   x=0;
   x= ++x * 2;
   NSLog(@"pre-increment - %i",x);

   x=0;
   x= x++ * 2;
   NSLog(@"post-increment - %i",x);

Result:
   pre-increment - 2
   post-increment - 1

The computer sees "x= ++x * 2" as "x= (x+1) * 2",
but sees "x= x++ * 2" as "x= x * 2; x = x + 1"

x++ does the increment at the very end of the entire statement, even after the assigments or tests for equivalence.  ++x does the increment before everything else, for all intents and purposes at the highest level of precendence.

Wonderful.  Now that I've typed all of this, I find that you *do* cover this topic, all the way back on page 296+.

I'm going to argue that page 84 is still wrong.  It is *not* a matter of personal preference.  There is a difference; it's just not important at this point in the lesson.  In the case of other similar topics, you have suggested that the details are covered in another chapter.  That would be vastly preferable to actually giving the reader incorrect information.  Assume that they will only read this one chapter.  You can give them incomplete information, but actually incorrect information is not good...

Thank you,
James Ramsey
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: July 12, 2009, 12:04:27 AM

James,

Thanks for your post.  I'll be sure to make the statement clearer in a future printing. The context here was just using the operator as the last expression in the for statement, in which whether is pre- or post- operation is in fact irrelevant.

Incidentally, this topics has been previously addressed:  http://classroomm.com/objective-c/index.php?topic=273.0

Cheers,

Steve Kochan
Logged
designdb
Newbie
*
Posts: 3


Email




Reply #2 on: July 12, 2009, 08:41:53 AM

I scanned the entire 2e current errata section, but didn't think to look in the rest of the board...
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.