# Java: Which Term determines this is a Sum?

• Java
• WWGD
In summary,You have written a code fragment that sums the values {0, 1, 2, 10, 4, 5} using an integer-valued variable called result.

#### WWGD

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:
WWGD said:
I don't see where we are declaring that the terms are added together.

What does "+=" mean?

WWGD
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?

What does "+=" mean?

Again, what does += mean?

WWGD
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
StoneTemplePython said:
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.

WWGD said:
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
wle said:
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) . .

WWGD said:
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
WWGD said:
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
rbelli1 said:
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
WWGD said:
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

## 1. What is the term that determines if an equation is a sum in Java?

The term that determines if an equation is a sum in Java is the "+" operator. This operator is used to add two or more values together and return the sum.

## 2. How is the "+" operator used to determine a sum in Java?

The "+" operator is used to add two or more values together and return the sum. It can be used with both numeric and string values to perform addition.

## 3. Can the "+" operator be used for other operations besides addition?

Yes, the "+" operator can also be used for other operations such as string concatenation. When used with strings, the operator will combine the two values into one string.

## 4. Are there any other terms that can determine a sum in Java?

Besides the "+" operator, the term "sum" can also refer to the total of a group of values. In Java, the "sum" can be determined using a for loop or an enhanced for loop to iterate through the values and add them together.

## 5. How can I determine the sum of a specific set of values in Java?

To determine the sum of a specific set of values in Java, you can use a for loop to iterate through the values and add them together, or use the Arrays.stream() method to add up all the values in an array.