# Java: Which Term determines this is a Sum?

• Java
Gold Member
Hi all,
I have the code below with a 'for' loop. I see the output is 17, the sum of all elements of the (variable?)
result. I am a bit confused: I cannot tell which part of the code is used to determine that the values
{1,2,10,4,5} must be added to each other?
1)We define an integer-valued variable called result.
2)'result' is initialized to be 0
3) We define a loop from 0 to 5 on the values of 'result'
4) 'result' takes the values result(i)=1 for i ##\neq ## 3; result(3):=10
5) I don't see where we are declaring that the terms are added together.
Thanks.

EDIT:

------------------------------------------------------------------------------------------------
Java:
int result = 0;
for (int i = 0; i < 5; i++) {
if (i == 3) {
result += 10;
} else {
result += i;
}
}
System.out.println(result);

Last edited by a moderator:

Staff Emeritus
2021 Award
I don't see where we are declaring that the terms are added together.

What does "+=" mean?

• WWGD
Gold Member
What does "+=" mean?
Thanks. Still, a bit confused: result+ =10 for 3 and result+(i)=i otherwise and this does not see to be indicating a sum. Is my understanding of the rest correct?

Staff Emeritus
2021 Award
What does "+=" mean?

Again, what does += mean?

• WWGD
StoneTemplePython
Gold Member
Good news is

+=

is used in quite a few other languages and the high level meaning is the same accros them. (Lower level there are some differences about in place addition vs 'regular' addition how that's treated, but it isn't so important.)
- - - -

So Java...?

I hope you didn't take my humorous random thought comment about being overly verbose to ward off an 'all knowing' co-worker too seriously!

• WWGD
Gold Member
Good news is

+=

is used in quite a few other languages and the high level meaning is the same accros them. (Lower level there are some differences about in place addition vs 'regular' addition how that's treated, but it isn't so important.)
- - - -

So Java...?

I hope you didn't take my humorous random thought comment about being overly verbose to ward off an 'all knowing' co-worker too seriously!
Thanks, for some reason I was expecting something like 'Sum' . Yes, Java sort of came up re job requirements too often, so I chose it. See RT for more.

I cannot tell which part of the code is used to determine that the values
{1,2,10,4,5} must be added to each other?

The code you wrote is summing {0, 1, 2, 10, 4}, not {1, 2, 10, 4, 5}.

• WWGD
Gold Member
The code you wrote is summing {0, 1, 2, 10, 4}, not {1, 2, 10, 4, 5}.
Good point, thanks, to remember counting starts at 0 is Java(maybe others) . .

Mark44
Mentor
result += 10;
The += operator here is one of many compound assign operators that are used in C, C++, C#, Java, Python, and probably other languages.

The statement you wrote is equivalent to result = result + 10; -- at least as far as the effect on result is concerned. Namely, it increments result by 10.
Other compound assignment operators include -=, *=, /=, %=, &=, |=, and a few others.

• WWGD
rbelli1
Gold Member
counting starts at 0

Actually counting starts at whatever you tell it to. In this case you specified zero.

You will frequently find this starting at zero because zero is often the first element in the data being processed and you want all of it.

BoB

• WWGD
Gold Member
Actually counting starts at whatever you tell it to. In this case you specified zero.

You will frequently find this starting at zero because zero is often the first element in the data being processed and you want all of it.

BoB
Right, sorry, I meant that this is the case when defining the k-th element of an array. If we refer to , say, Result(0) , for the 1st element of result.

• rbelli1
Mark44
Mentor
int result = 0;
for (int i = 0; i < 5; i++) {
if (i == 3) {
result += 10;
} else {
result += i;
}
}
System.out.println(result);
@WWGD, as a Science Advisor, you should be aware of code tags -- there's a sticky on this at the top of this forum section.
With code tags (for Java) and indentation and a slight rearrangement (by me), your code looks like this:
Java:
int result = 0;
for (int i = 0; i < 5; i++) {
if (i == 3) {
result += 10;
}
else {
result += i;
}
}
Posting code with no indentation makes it much harder to comprehend, especially in larger blocks of code.

• WWGD