This site requires JavaScript, please enable it in your browser!
Greenfoot back
ipeefreely
ipeefreely wrote ...

2015/5/18

Please explain how to work this

ipeefreely ipeefreely

2015/5/18

#
sum = 0; x = 2; while ( x < 6 ) { sum = sum + x; x = x + 1; } I thought the calculation was to replace each value of x or sum with each assigned number but apparently I'm wrong. Can someone please explain how I am work this please? Thank you
azazeel azazeel

2015/5/18

#
you can read the basic programming and it's simple when you learn and run the code
ipeefreely ipeefreely

2015/5/18

#
azazeel wrote...
you can read the basic programming and it's simple when you learn and run the code
can you please elaborate on your reply. I'm trying to understand how to work this but still winding up confused. I tried to work the problem again and came up with 7 using the chart you provided. 7 is 1 of the options as the answer but I am not sure if I am working it correctly. I took 2+2; 2+1=7?
ipeefreely ipeefreely

2015/5/18

#
never mind. someone else explained it but thanks for your reply. I got sum is initialized to be 0. x = 2; x < 6 (true), sum = 0 + 2 = 2 x = 3; x < 6 (true) sum = 2 + 3 = 5 x = 4; x < 6 (true) sum = 5 + 4 = 9 x = 5; x < 6 (true) sum = 9 + 5 = 14 x = 6; x < 6 (false) The program terminated The last value of sum is 14 I didnt understand how to distribute the handy chart you provided but now I understand
You need to login to post a reply.