- 2
I dont understand the question...
The one where it says what is the output of this code... and the answer is 17
6 ответов
+ 1
Ohhh ok thank you sooooo much
0
I assume you mean the question in the course about the continue; method. The answer is 16.
Firstly,
sum = 0
During the first iteration of the loop, i = 4,
sum += i
sum = sum + i
sum = 0 + 4
sum = 4
(I'm not writing code, I'm showing the math.)
Second iteration, incremented to i = 5,
sum += i
sum = sum + i
sum = 4 + 5
sum = 9
Third iteration, i = 6,
Since (i == 6) is true, the if gets triggered.
This iteration of the loop ia skipped by continue;.
Fourth iteration, i = 7,
sum += i
sum = sum + i
sum = 9 + 7
sum = 16
Fifth iteration, i = 8,
(8 < 8) is false.
Since the condition is false, the loop ends.
The sum is 16.
0
I confused because of why is there two same name term? "sum and sum". Could you please explain to me?
Therefore I couldn't do the math operation.
0
You mean where I write "sum" twice on the same line? The += operator means add to what is already there. For example, sum += 2, is saying add 2 to what sum already is, thus sum = sum + 2.
0
hi James,
I did not understand how this was done.
How does "i" affect sum when "var sum = 0"?
Is both of them same value of "sum"? or different value?
your first explanation is ok;
Firstly,
sum = 0
During the first iteration of the loop, i = 4,
sum += i
sum = sum + i
sum = 0 + 4
sum = 4
but I confuse that Second iteration;
Second iteration, incremented to i = 5,
sum += i
sum = sum + i
sum = 4 + 5
sum = 9
why have you accepted the "sum = 4" at this point?
Does not this "var sum=0" always have to be 0?
I think I didn't ask correct question? so sorry, My grammar is not good.
0
4 is what sum was after the first iteration.