Java: Which Term determines this is a Sum?

  • #1
WWGD
Science Advisor
Gold Member
2019 Award
5,180
2,495

Main Question or Discussion Point

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:

Answers and Replies

  • #2
Vanadium 50
Staff Emeritus
Science Advisor
Education Advisor
2019 Award
24,034
6,610
I don't see where we are declaring that the terms are added together.
What does "+=" mean?
 
  • #3
WWGD
Science Advisor
Gold Member
2019 Award
5,180
2,495
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?
 
  • #4
Vanadium 50
Staff Emeritus
Science Advisor
Education Advisor
2019 Award
24,034
6,610
  • #5
StoneTemplePython
Science Advisor
Gold Member
2019 Award
1,145
546
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!
 
  • #6
WWGD
Science Advisor
Gold Member
2019 Award
5,180
2,495
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.
 
  • #7
wle
307
136
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}.
 
  • #8
WWGD
Science Advisor
Gold Member
2019 Award
5,180
2,495
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) . .
 
  • #9
33,271
4,976
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.
 
  • #10
rbelli1
Gold Member
920
339
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
 
  • #11
WWGD
Science Advisor
Gold Member
2019 Award
5,180
2,495
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.
 
  • #12
33,271
4,976
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.
 

Related Threads on Java: Which Term determines this is a Sum?

Replies
4
Views
2K
Replies
2
Views
781
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
2
Views
5K
Replies
8
Views
1K
  • Last Post
Replies
11
Views
86K
Replies
2
Views
2K
Replies
2
Views
10K
Replies
19
Views
2K
Top