- 2

I dont understand the question...

The one where it says what is the output of this code... and the answer is 17

18th Jul 2017, 8:59 PM
SuperCoder13
SuperCoder13 - avatar
6 ответов
+ 1
Ohhh ok thank you sooooo much
18th Jul 2017, 10:12 PM
SuperCoder13
SuperCoder13 - avatar
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.
18th Jul 2017, 9:26 PM
James
James - avatar
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.
19th Jul 2017, 10:28 AM
cem
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.
19th Jul 2017, 11:23 AM
James
James - avatar
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.
19th Jul 2017, 11:57 AM
cem
0
4 is what sum was after the first iteration.
19th Jul 2017, 12:02 PM
James
James - avatar