This program finds the sum of numbers: sum = 4 + 7 + 10 and prints it on the screen.
This amount is 21.
Initial values of variables:
c = 1
sum = 0
Then a loop is executed:
while (c <10):
c = c + 3
sum = sum + c
The body of the cycle is executed three times.
Two Operators:
c = c + 3
sum = sum + c
repeated three times:
1) c = c + 3 (c = 1 + 3, c = 4)
2) sum = sum + c (sum = 0 + 4), sum = 4)
3) c = c + 3 (c = 4 + 3, c = 7)
4) sum = sum + c (sum = 4 + 7), sum = 11)
5) c = c + 3 (c = 7 + 3, c = 10)
6) sum = sum + c (sum = 11 + 10), sum = 21)
At this point, the variable c becomes 10 (c = 10) and we exit the loop (while (c <10)).
The value of the sum sum (number 21) is printed as a result:
print (sum)
Comments
Leave a comment