Java Java: Which Term determines this is a Sum?

Click For Summary
The discussion revolves around understanding how the Java code calculates the sum of specific values. The code initializes an integer variable 'result' to 0 and uses a for loop to iterate through values from 0 to 4, adding either the loop index or 10 to 'result' when the index is 3. Participants clarify that the '+=' operator is a shorthand for addition, effectively incrementing 'result' by the specified value. The confusion arises from the expectation of a direct declaration of a sum, while the code implicitly sums the values through the loop's logic. Overall, the code sums the values {0, 1, 2, 10, 4}, not the initially expected {1, 2, 10, 4, 5}.
WWGD
Science Advisor
Homework Helper
Messages
7,772
Reaction score
13,006
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?
Please comment/correct:
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:
Technology news on Phys.org
WWGD said:
I don't see where we are declaring that the terms are added together.

What does "+=" mean?
 
  • Like
Likes WWGD
Vanadium 50 said:
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?
 
Vanadium 50 said:
What does "+=" mean?

Again, what does += mean?
 
  • Like
Likes 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!
 
  • Like
Likes 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}.
 
  • Like
Likes 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.
 
  • Like
Likes WWGD
  • #10
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
 
  • Like
Likes WWGD
  • #11
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.
 
  • Like
Likes rbelli1
  • #12
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.
 
  • Like
Likes WWGD

Similar threads

Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 14 ·
Replies
14
Views
1K
  • · Replies 0 ·
Replies
0
Views
640
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 0 ·
Replies
0
Views
670
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 2 ·
Replies
2
Views
13K