Java: Which Term determines this is a Sum?

Click For Summary

Discussion Overview

The discussion revolves around understanding the Java code snippet that calculates a sum using a for loop. Participants are exploring the mechanics of the code, particularly focusing on how the addition of values is determined and the meaning of the "+=" operator.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about which part of the code indicates that values should be summed, specifically questioning the addition of the values {1,2,10,4,5}.
  • Multiple participants inquire about the meaning of the "+=" operator, with one noting that it is used in several programming languages with similar high-level meanings.
  • Another participant points out that the code is actually summing the values {0, 1, 2, 10, 4}, not {1, 2, 10, 4, 5}, highlighting a misunderstanding about zero-based indexing in Java.
  • There is a discussion about the implications of counting starting at zero and its relevance to array indexing in programming.
  • A participant suggests that the code could be made clearer with proper indentation and formatting, emphasizing the importance of readability in code.

Areas of Agreement / Disagreement

Participants are generally exploring the same topic but express differing views on the interpretation of the code and the behavior of the "+=" operator. There is no consensus on the understanding of how the summation is determined.

Contextual Notes

Some participants are uncertain about the implications of the "+=" operator and how it relates to the summation process. There is also a lack of clarity regarding the initial values being summed due to the zero-based indexing in Java.

WWGD
Science Advisor
Homework Helper
Messages
7,802
Reaction score
13,106
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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: WWGD

Similar threads

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