+ 1
Finding the value of a valuable in a for loop
so absolutely everything else I'm following and understanding. but when it comes to the challenges I just can't figure out the value of a variable in a for loop. example: sum=0 for (x=0; x<10;x++) sum+=x; This is a crude example lol then they'll need me to know the value of x to solve one of the questions. everyone once in a while I'll get it right then think I know it. then try again and completely wrong. Please any help would be greatly appreciated. Thank you
3 Respuestas
+ 5
sum+=x means sum=sum+x
So here, sum=0 & x increases from 0 to 9
When loop starts
Sum + x
Sum = 0 + 0 =0
Sum= 0 + 1=1
Sum= 1 + 2=3
Sum= 3 + 3=6
Sum= 6 + 4=10
Sum= 10 + 5=15
Sum= 15 + 6=21
Sum= 21 + 7=28
Sum= 28 + 8=36
Sum= 36 + 9=45
Loop ends
+ 2
Reproduce the challenge in the code playground to get the answer.
analyze the logic that step by step or loop by loop got you there.
sum+x=next sum
0+0=0 first loop x=0, loop will run carrying out the addition and then x will increment by one
0+1=1 second loop x=1 is still less than 10 so the loop will run carrying out the addition
1+2=3 ...same thing
3+3=6 ...same thing
6+4=10 ...same thing
10+5=15 ...same thing
15+6=21 ...same thing
21+7=28 ...same thing
28+8=36 ...same thing
36+9=45 ...same thing but after this loop x =10 and the for loop ends
the sum equals 45
the value of x equals 10
Hope this helps;)
0
ohhhhhh man oh man thank you. you both made since I understand now. started getting them all correct. I can't thank you both enough.
I'm enjoying c++ so this means a lot