Wrox Programmer Forums
|
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 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 February 19th, 2004, 07:27 PM
Registered User
 
Join Date: Feb 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to elevate
Default ++ suffix/prefix

Going through the book, currently on Chapter 4. On page 78, an example of a do while loop uses ++ as a suffix (i++) to increment, while on page 79, another example uses ++ as a prefix (++totalYears). Is there any rhyme or reason for this? It would appear that it doesn't matter as there's no other operation in the expression.

 
Old February 19th, 2004, 08:06 PM
Authorized User
 
Join Date: Jan 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

On page 67 there is a chart explaining the order of priority of the arithmetic operators. ++ used as a prefix is the highest priority. ++ used as a suffix is the lowest priority.

Carl Olsen
www.carl-olsen.com
 
Old February 19th, 2004, 08:10 PM
Registered User
 
Join Date: Feb 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to elevate
Default

I'm aware of the priorities, it's just that in these two examples, there's nothing else to prioritize, so I was wondering if there were some convention on which to use.

 
Old February 19th, 2004, 08:48 PM
Authorized User
 
Join Date: Jan 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It says "i++" increments "i" after it's value has been output to the screen. If it were "++i" then it would be incremented before it was output to the screen. Run the example both ways and see what happens.


Carl Olsen
www.carl-olsen.com
 
Old February 19th, 2004, 11:30 PM
Registered User
 
Join Date: Feb 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The only reason is to show readers we can use two methods to do the same thing.There are differeces between them,but in the two places,they are the same.Which one will be used depends your convention.
 
Old February 20th, 2004, 08:56 AM
Authorized User
 
Join Date: Jan 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

They are not the same. Run the example both ways and you'll see that "i++" results in a list of numbers 1-10, and "++i" results in a list of numbers 2-11.

Carl Olsen
www.carl-olsen.com
 
Old February 20th, 2004, 03:46 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default


It all comes down to convention, usually. People use what they know and are familiar with.

Typically, if you're writing an expression where the location of the increment operator makes a difference in the result, then I suspect that line of code is more likely to cause confusion and/or problems down the road. These lines should probably be split.

When many students first begin learning C or C++, they often feel that empty for loops are an "elegant" and/or "witty" solution to certain problems. In the real world, however, such cleverness is usually looked down upon in favor of readability.

For example:

for (int i = 0; i < 10; std::cout << i++);

should really be:

for (int i = 0; i < 10; ++i)
{
   std::cout << i;
}

The second version, while it does take a lot more room, is much easier to understand at first glance. While it's possible to perform computation in the third section of a for expression, this place is really supposed to hold some sort of operation that will bring your iteration control variable closer to the stop condition.

Also, you'll notice that in the second code snippet, I use ++i as the incrementation variable. This is because it's *slightly* more efficient (unless you have a smart compiler). Because i++ is a post-increment operator, the compiler allocates space on the stack to hold the original value of i for use in the expression, and increments i in the original location.

Since there is no "rest of the expression", this extra allocation is not used for anything and therefore is just wasted space.

Again, many compilers can determine whether a post-incrementation operation is the entire expression and suppress the extra allocation in this case. But it's still handy to be explicit.



Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Prefix Data bundersuk Access VBA 2 December 26th, 2006 06:10 PM
How a additional prefix in a namespace comes? diang BOOK: ASP.NET Website Programming Problem-Design-Solution 0 July 13th, 2006 10:40 PM
Detect image suffix? mat41 Javascript How-To 5 July 28th, 2005 06:43 PM
Prefix zero conditionally: A newbie question soorya Access VBA 2 October 18th, 2004 12:17 PM
attribute namespace prefix tsmith XSLT 1 August 12th, 2004 06:01 AM





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