0

I got my sample exam question in java. I saw this expression sum += sum + d in the for loop.
Here is the code:

double sum =0;

for (double d = 0; d<10; sum += sum + d) {
    d += 0.1;
}

I just dont understand that part.
I only know these:

x+=1
x=x + ++x;
x=x + x++;

Thanks in advance!

Hugues M.
  • 19,846
  • 6
  • 37
  • 65
hakimov
  • 19
  • 1
  • 1
  • what part is confusing? – SaggingRufus Jun 02 '17 at 14:21
  • `sum += sum + d` is same as `sum = sum + sum + d` is same as `sum = sum * 2 + d` – Andreas Jun 02 '17 at 14:21
  • @Andreas: Please put answers in the answers section. A short answer is not necessarily a poor one. Ditto Berger. – Bathsheba Jun 02 '17 at 14:21
  • 2
    You understand `x+=1` and `x=x + ++x` but have difficulties with `sum += sum + d`? You'd probably better dive into the topic again otherwise it might hurt your exam. – Thomas Jun 02 '17 at 14:23
  • 1
    Possible duplicate of [Java's +=, -=, \*=, /= compound assignment operators](https://stackoverflow.com/questions/8710619/javas-compound-assignment-operators) – Youcef LAIDANI Jun 02 '17 at 14:25
  • @Thomas I will Thomas, I will. Thank you. I have started Java 3 months ago along with 3 other subjects. So it was a bit diffucult to focus on all of them equally. So I am new to Java. Pulling my hair, when I stuck or don't understand something. However, I keep going. I wish I will be Soft.Engineer one day. CHeers !!!!!!!!!! – hakimov Jun 02 '17 at 15:31

5 Answers5

3

(I'm glad you understand the three obfuscated statements. But please don't use them in production. The final two are undefined in C and C++).

sum += a is shorthand for sum = sum + a for any a (neglecting any subtle differences due to implicit type conversions).

So sum += sum + d is sum = sum + sum + d; which simplifies to

sum = 2 * sum + d;

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

If to be more accurate, sum += a is not same to sum = sum + a, there is a type cast to type of sum. Let's consider the next example:

short x = 3;
x += 4.6;

It's the same to

short x = 3;
x = (short)(x + 4.6);

But not to x = x + 4.6 So, we have type cast to the type of sum. For more details read JLS:

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

1

From the operators precedence, you can see that + (additive) operator has higher precedence than += (assignment) operator, so we first evaluate the sum + d part, let's call this result sumplusd, we get :

sum += sumplusd

Then we evaluate +=, that is increment the variable on the left part of the expression by the value on the right part, we get :

sum = sum + sumplusd, which reads sum = sum + (sum + d), which reads sum = sum*2 + d .

Arnaud
  • 17,229
  • 3
  • 31
  • 44
0

Since, sum + = "any variable or constant" is equivalent to sum = sum + "variable or constant". Thus, sum+=sum+d would be same as sum = sum + ( sum + d ).

Yahya
  • 13,349
  • 6
  • 30
  • 42
Logical Retard
  • 113
  • 2
  • 11
0

To simplify the whole mess, including the do loop:

double sum = 0.0;
double d = 0.0;
while ( d < 10.0 )
{
    sum = 2.0*sum + d
    d = d + 0.1;
}
Jack
  • 5,801
  • 1
  • 15
  • 20