Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Ivor Horton's Beginning Visual C++ 2010
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2010 by Ivor Horton; ISBN: 9780470500880
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2010 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 12th, 2013, 01:10 PM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2013
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Smile Increment operators

Two questions:

First :

k = 0;
k += (++k) + (++k);
cout<<k; // Why K equal to 6?

Second :
k= 0;
k *= (++k) + (++k);
cout<<k; //Why k equal to 8?

Thanks in advance

Ceresyuyi
  #2 (permalink)  
Old January 13th, 2013, 12:25 AM
Authorized User
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2012
Posts: 66
Thanks: 0
Thanked 4 Times in 4 Posts
Default

ceresyuyi,

You posed excellent questions. I had to play computer for a while to figure it out. Let me explain the concept of playing computer first. It is a technique that you will find handy in any programming language to determine what the correct results of your code should produce. That is not to say that your code is correct. It just proves what the code should produce. This practice is used to confirm that the code is executing properly.

To play computer you break the instructions down to the simplest form, which is performing one operation at a time and tracking the values of all variables in each operation. Compound operations have to be broken down into their individual components and observe the proper precedence. This is easiest to do on paper.

Now for the explanations. You will find it helpful to refer to the table on page 77 of the book. The order of operations is from the top to the bottom: meaning the closer to the top the operator is listed, the more priority it has. The associativity in the far right column is just as important as the order.

For example:
  • The * and \ operators are left associative and have the same priority according to the table. This means that 3/2*5 is evaluated from left to right. Using parentheses here is the equality 3/2*5=(3/2)*5. The 3/2 is performed first.
  • The assignment operator (=) is right associative , and both = have the same priority according to the table. This means that x=y=5 will be evaluated from right to left. The value 5 will be assigned to y, and then the value of y will be assigned to x.

In all computations, anything in parentheses is evaluated first.

Now let's play computer with both of the statements you questioned.

k += (++k) + (++k) with k initialized to a value of 0
Referring to the table, we find that
  • + is on the sixth level and is left associative
  • += is on the sixteenth level and is right associative
Using the precedence rules we have to evaluate the expression in parentheses first, then the + operator, followed by the += operator in each of the individual steps of evaluating the entire line of code. It is important to notice that each time the value of k changes, every instance of k in the expression changes!

Ready? Here we go. Try following along on paper. It will make more sense.
  1. The initial value of k is 0.
  2. Observing the right associativity of += has us start at the last (farthest to the right) expression (++k). The value of k is incremented by one; therefore, all instances of k now have a value of 1.
  3. Now we evaluate the first expression (++k). Remember that k now has a value of 1. The value of k is incremented by one again, giving it a value of 2. Now all instances of k in the expression have a value of 2.
  4. The expression now looks like this. k += 2 + 2 Because the expressions in parentheses have been evaluated, the table tells us that the + operator is the next in line. The result is k += 4. Once again the value of k has changed. The new value is 4.
  5. The expression k += 4 is Identical to k = k + 4. Because the new value of k is 4, we have the equivalent of k = 4 + 4. This results in k = 8.

k *= (++k) + (++k) with k initialized to 0
I am going to abbreviate this soluton a little to avoid typographical errors. You can review the above solution if necessary for clarification.

Referring to the table again we find that the
  • + operator is on the sixth level and is left associative
  • *= operator is on the sixteenth level and is right associative
Our order of operations is parentheses, + operator, then the *= operator.

Here are the steps to evaluate:
  1. initial value of k is 0
  2. far right (++k) is evaluated first, giving k a value of 1 --> k = 1 everywhere
  3. left (++k) is evaluated next, giving k a value of 2 --> k = 2 everywhere
  4. now k *= 2 + 2 --> k *= 4, k = 2 * 4 --> k = 8

Does that make sense? If not tell me where it does not make sense and the value of k you have at that point. I'll try to walk you through it.

regards,
drpepper
The Following User Says Thank You to drpepper For This Useful Post:
ceresyuyi (January 14th, 2013)
  #3 (permalink)  
Old January 15th, 2013, 02:07 AM
Authorized User
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2012
Posts: 66
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Thanks for clicking the thanks button for me.
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Logical operators ksskumar XSLT 3 November 11th, 2011 06:09 AM
overloading the increment and decrement operators(pg 540) esppeace BOOK: Ivor Horton's Beginning Visual C++ 2010 0 May 7th, 2011 10:24 AM
Problem with Increment Operators alexdelamuerte BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1 2 December 3rd, 2008 11:51 AM
Math operators nguyendh BOOK: Ivor Horton's Beginning Visual C++ 2005 1 June 25th, 2006 02:35 AM
Matrix operators Ravel99 Pro VB 6 0 April 28th, 2006 04:45 AM



All times are GMT -4. The time now is 01:09 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.