- #1
- 7,339
- 11,276
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:
------------------------------------------------------------------------------------------------
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: