445,819 Members | 1,173 Online
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

# basic question on *=

 P: 84 I'm now since a couple of weeks debugging a program and am now at the point where I don't know anymore what could be wrong (actually it compiles but does not do what I want)... Anyway, if i want to write a=b*(c+d) and write the following code: Expand|Select|Wrap|Line Numbers a=b; a*=c+d; does the program what I want or does it see a=b*c+d? I really hope it is the latter possibility, because that risks to solve my problem... thanks in advance either way Jun 11 '07 #1
9 Replies

 Expert 100+ P: 1,764 I'm now since a couple of weeks debugging a program and am now at the point where I don't know anymore what could be wrong (actually it compiles but does not do what I want)... Anyway, if i want to write a=b*(c+d) and write the following code: Expand|Select|Wrap|Line Numbers a=b; a*=c+d; does the program what I want or does it see a=b*c+d? I really hope it is the latter possibility, because that risks to solve my problem... thanks in advance either way It's same as a=a*(c+d) and becaue of a=b it is same as a=b*(c+d); Savage Jun 11 '07 #2

 100+ P: 208 I'm now since a couple of weeks debugging a program and am now at the point where I don't know anymore what could be wrong (actually it compiles but does not do what I want)... Anyway, if i want to write a=b*(c+d) and write the following code: Expand|Select|Wrap|Line Numbers a=b; a*=c+d; does the program what I want or does it see a=b*c+d? I really hope it is the latter possibility, because that risks to solve my problem... thanks in advance either way Expand|Select|Wrap|Line Numbers a=b; a*=c+d; Does what you want it a = b * (c + d) If you want it to be more readable you could just assign c+d to another variable and then do a *= var; That would produce the same results but maybe be a little more easy to read. PS Savage beat me to it. Jun 11 '07 #3

 P: 84 It's same as a=a*(c+d) and becaue of a=b it is same as a=b*(c+d); Savage So a*=c+d is equivalent to a*=(c+d) ? EDIT: and you beat me to my answer to savage :) Thanks very much both of you. Jun 11 '07 #4

 Expert Mod 5K+ P: 9,197 This code: a=b; a*=c+d; is equivalent to: Expand|Select|Wrap|Line Numbers a = b*(c+d);   This is a precedence issue. The *= operator has a precedence of 16 whereas addition has a precedence of 6. That means the addition is done before the *=. Personally, I like your original method: Expand|Select|Wrap|Line Numbers a = b*(c+d);   because it's easier to read. Jun 11 '07 #5

 P: 23 Just thought that this precedence list http://www.cppreference.com/operator_precedence.html might help to justify why a*=c+d is equivalent to a=a*(b+c) --Sorower Jun 11 '07 #6

 P: 84 T Personally, I like your original method: Expand|Select|Wrap|Line Numbers a = b*(c+d);   because it's easier to read. I agree that it easier to read, but if the b,c,d terms are very long (about one line) and the program is huge (260 MB of code) and you haven't written the program just modified it, which means that you might not know all the variables' names defined elsewhere and inherited to where you are, makes it difficult and risky to define for every b,c,d term a variables. So it leaves you no choice. I would though never do it if it would be easily avoidable... Also thanks about the explication of why via the precedences. Jun 11 '07 #7

 P: 84 Just thought that this precedence list http://www.cppreference.com/operator_precedence.html might help to justify why a*=c+d is equivalent to a=a*(b+c) --Sorower Thanks, such a list clears up also some other things, bookm arked it right away. Jun 11 '07 #8

 Expert 100+ P: 1,764 Expand|Select|Wrap|Line Numbers a=b; a*=c+d; Does what you want it a = b * (c + d) If you want it to be more readable you could just assign c+d to another variable and then do a *= var; That would produce the same results but maybe be a little more easy to read. PS Savage beat me to it. Muhahahahahah Savage Jun 11 '07 #9

 Expert 100+ P: 1,764 So a*=c+d is equivalent to a*=(c+d) ? EDIT: and you beat me to my answer to savage :) Thanks very much both of you. We are more than happy to help u. Savage Jun 11 '07 #10