 |
BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4
 | This is the forum to discuss the Wrox book Beginning Microsoft Visual C# 2008 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, Eric White; ISBN: 9780470191354 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 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
|
|
|
|

December 19th, 2008, 05:02 AM
|
|
Registered User
|
|
Join Date: Dec 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Order of Presedence
The Answers.doc file from the Book-Downloads (Exercise4 - Beginning Microsoft Visual C# 2008) gives the solution below for computing the expression ...
I believe the answer here is wrong ... % and * and / are on the same order-of precedence level. So the answer, in my opinion should be:
Code:
resultVar += (((var1 * var2) + ((var3 % var4) / var5)));
(The comment to the answer says that % is on the same level as + which is a contradiction to the list on page 53 of the book ...)
Can anyone confirm, comment explain?

Exercise 4
Q. By considering operator precedence, list the steps involved in the computation of the following expression:
Code:
resultVar += var1 * var2 + var3 % var4 / var5;
A. The * and / operators have the highest precedence here, followed by +, %, and finally +=. The precedence in the exercise can be illustrated using parentheses as follows:
Code:
resultVar += (((var1 * var2) + var3) % (var4 / var5));
|
|

December 19th, 2008, 10:21 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
You are most clearly correct.
One wonders about the quality of the writing and editing that allowed something as fundamental as this to be so very very wrong.
Your answer *is* a little wrong, only in the sense that you have one extra useless set of parentheses.
This is most easily seen by using indentation:
Code:
resultVar += (
(
(var1 * var2)
+
(
(var3 % var4)
/
var5
)
)
)
;
You can see that the outermost TWO pairs of parentheses enclose the same expressions. Your version isn't wrong, of course. Just mildly more complex than needed.
|
|

December 22nd, 2008, 03:30 AM
|
|
Registered User
|
|
Join Date: Dec 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for your confirmation. I just took the original answer and fixed it ... you are certailnly correct with the extra parentheses. Will this be fixed in the downloads for this book? It's just a Word document "they" need to change ...
|
|

December 22nd, 2008, 05:50 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
> Will this be fixed in the downloads for this book?
What do you get when you cross and elephant and a rhino?
Let's hope that somebody at WROX sees this thread. If you don't get a response, try emailing one of the WROX people who post here.
Elephino.
|
|

December 22nd, 2008, 11:18 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
|
|
Precedence
As usual, Old Pendant nails it.
Keep in mind that the modulus operator is on the same level as multiply and divide, not the addition operator. Also, the math operators are left-associative, which means that, in the case of "ties", the operators are evaluated left-to-right in the expression.
Personally, I hate complex expressions like this for the very reason we're having this discussion. Given that 80% of software development time is spent debugging code, I'd break such complex expressions into two separate statements. This allows you to see the intermediate values without having to resort to a Watch window. An optimizing compiler is probably going to collapse the final code anyway, so such segmenting rarely has a noticeable impact on execution speed, especially if it's not in a loop. If you're worried about performance, or the expression is not being optimized, debug it first and then move it back to its complex form after testing.
Dr. Purdum
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
|
|

May 17th, 2009, 04:41 AM
|
|
Registered User
|
|
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Dag Nabbit!
I've been fussin' with this for way too long, trying to figure out how I was messing up. I even downloaded the errata, which was no help! Thanks for the confimation, y'all. I found this thread after I had already thrown together this here proof using sample values:
Code:
namespace Ch03Exercise04
{
classProgram
{
staticvoid Main(string[] args)
{
double var1 = 1, var2 = 2, var3 = 3, var4 = 4, var5 = 5;
double resultVar = 5;
resultVar += var1 * var2 + var3 % var4 / var5;
Console.WriteLine("question = {0}", resultVar);
resultVar = 5;
resultVar += (((var1 * var2) + var3) % (var4 / var5));
Console.WriteLine("answer key = {0}", resultVar);
resultVar = 5;
resultVar = resultVar + ((var1 * var2) + ((var3 % var4) / var5));
Console.WriteLine("table p53 and ever' dang maths teacher ever = {0}", resultVar);
Console.ReadKey();
}
}
}
The answer ain't any different than other people came up with earlier in the thread, but it's presented here to be run simply for the thrills that are errata-free order of operashuns in C#.
<interjection>Woo hoo!</interjection>
|
|

November 12th, 2009, 09:05 AM
|
|
Authorized User
|
|
Join Date: Nov 2009
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
|
|
If the order in a computational statement is from left to right(I doubt!!) the priority of operators in this code can be analyzed like this:
* -> / -> % -> + -> +=
This can be simply understood by the table given in book.
Otherwise,If the order of precedence in a computational statement is from right to left(I doubt again!!) we have:
/ -> % -> * -> + ->+=
Can anybody tell me what is the real order of precedence in a computational statement like:
var1 * var2 + var3 % var4 / var5
I mean which side of the operator + (with least priority in this statement) is being computed first?left or right?
Thanks in advance...
Erfan
|
|

November 12th, 2009, 10:13 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
|
|
Precedence of Operators
The Order of Precedence is presented in Table 6-6 (p. 149) in my book. If there are multiple operators in a complex expression, the rules of Associativity break the ties. Most math operators are left-associative, which means they are evaluated by the parser in a left-to-right fashion.
In your post, you asked the order of evaluation for the expression:
var1 * var2 + var3 % var4 / var5
Of the four operators in this complex expression, three have the same precedence level (i.e., *, %, and /) while the addition operator (+) is lowest in the food chain. Given that, the order of evaluation because of left associativity for these math operators, becomes:
First: var1 and var2 (multiply)
Second: var3 and var4 (modulus)
Third: The intermediate result of Step 2 divided by var5 (division)
Fourth: The result of Step 3 added to the result of Step 1 (addition)
Of course, you can change the order of evaluation of complex expressions by the use of parentheses.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
|
|

November 12th, 2009, 11:33 AM
|
|
Authorized User
|
|
Join Date: Nov 2009
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
|
|
1 more thing
I have one question in mind.
suppose that we have a main expression composed of two other expressions:
leftExp + rightExp
One of these Exps contain * operator with 2 operands and the other contain / operator with to operands in themselves (suppose that we don't know which expression,leftExp or rightExp,contain which operator * or /).
As we know + has the least precedence in comparrison to * and /.
Now if the main expression is evaluated,we are sure that one of two other Exps should be evaluated first.
My question is:which one is the first to be evaluated?leftExp or rightExp???
Does the order of precedence of * and / effect the evaluation step of leftExp and rightExp???
Do operators which are at the same level of precedence (like " *, / , % " and " + , - ") have another order of precedence among themselves (like this order: * -> % -> / or this one: / -> * -> % or...)?
I hope you got what I say?
Also these complicated things sound silly,they are extremely necessary for a programmer to know!!!!
I need your answer...
|
|

November 12th, 2009, 11:33 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
|
|
I'm not sure I understand your question. It would help if you would use a more concrete example in your question. Also, the statement:
(suppose that we don't know which expression,leftExp or rightExp,contain which operator * or /)
make no sense to me, since the code would know the operators and their positions in the expressions.
First, the compiler parses the line of code containing the expressions and checks for syntax errors. By the end of the parse, the compiler knows if the syntax is correct, what the operators are, and their positions in the statement. It would, therefore, know where to start processing the expressions.
Your statement:
My question is:which one is the first to be evaluated?leftExp or rightExp???
Does the order of precedence of * and / effect the evaluation step of leftExp and rightExp???
suggests that you might be confusing evaluation with processing. Also, when operators do have the same level of precedence, it is at that point that the Rules of Associativity take over. Think of associativity as tie breakers. The math operators are left-associative, which means that two expressions that have operators with the same precedence level use associativity rules to decide what to evaluate first. (See my earlier post.) Therefore, if LeftExp and RightExp have operators of the same precedence level, the left associative rules dictate that LeftExp is evaluated first.
With this info, reread pages 149-150 on the associativity rules and their impact on expression evaluation.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
|
|
The Following User Says Thank You to DrPurdum For This Useful Post:
|
|
|
 |