0

Can anyone explain me why is the output is 11? sorry noob question, Im just a beginner.

ForLoop https://code.sololearn.com/c275alWz7UFT/?ref=app

15th Jan 2021, 9:39 AM
Arnold Berendez
Arnold Berendez - avatar
6 Antworten
+ 3
Your iteration starts from 5 and ends with 15 In other words, the loop iterates 11 times (15 - 5 + 1 == 11) And every iteration, the program adds +1 to sum variable Repeat the process or add 1 to sum 11 times, and therefore sum will be equal to 11. Try to change the " sum += 1 " to " sum += 2 " and it will add 2 to sum variable every loop, in this case, the program performs 11 loops. Add 2 to sum each loop ( 11 * 2 ) And we will get 22 instead.
15th Jan 2021, 9:48 AM
noteve
noteve - avatar
+ 1
Diko masyadong ma gets kuya niko hehe
15th Jan 2021, 9:56 AM
Arnold Berendez
Arnold Berendez - avatar
+ 1
Ok. Wag muna natin isama yung mga syntax para masmadaling maintindihan // LOOP from 5 to 15 == from 1 to 11 Since pareho lang namang 10 yung differences nila. Huwag ka masyado magfocus sa variable na "i" since di naman natin ginamit yung "i", ginamit lang natin sya for iteration. So here, from 1 to 11 means 1 is our start value and 11 is our end value, and incremention is i++ or +1. So simulate natin sya. Loop start 1 2 3 4 5 6 7 8 9 10 11 Loop breaks These numbers represents values of i, every incremention. Pag nareach nya na yung 11, the loop breaks. Ok now, sama naman natin yung sum += 1 sum = 0 1 ---- +1 sum = 1 2 ---- +1 sum = 2 3 ---- +1 sum = 3 4 ---- +1 sum = 4 5 ---- +1 sum = 5 6 ---- +1 sum = 6 7 ---- +1 sum = 7 8 ---- +1 sum = 8 9 ---- +1 sum = 9 10 --- +1 sum = 10 11 ---- +1 sum = 11 *Loop Breaks* Final Value: 11 Output: sum = 11
15th Jan 2021, 10:14 AM
noteve
noteve - avatar
+ 1
So parang ganito po kuya? 5 --- 1 6 --- 2 7 --- 3 8 --- 4 9 --- 5 10 --- 6 11 --- 7 12 --- 8 13 --- 9 14 --- 10 15 --- 11
15th Jan 2021, 10:25 AM
Arnold Berendez
Arnold Berendez - avatar
+ 1
Yes po.
15th Jan 2021, 10:47 AM
noteve
noteve - avatar
+ 1
Salamat kuya niko nagets konapo hehe
15th Jan 2021, 10:48 AM
Arnold Berendez
Arnold Berendez - avatar