Can anyone explain me why is the output is 11? sorry noob question, Im just a beginner. | Sololearn: Learn to code for FREE!

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

1/15/2021 9:39:50 AM

Arnold Berendez

6 Answers

New Answer

+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.

+1

Diko masyadong ma gets kuya niko hehe

+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

+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

+1

Yes po.

+1

Salamat kuya niko nagets konapo hehe