Apr 30, 2017 at 10:28pm UTC
for loop:
j = 0
a[0] = 2 * 0 - 3
a[0] = -3
j = 1
a[1] = 2 * 1 - 3
a[1] = -1
j = 2
a[2] = 2 * 2 - 3
a[2] = 1
j = 3
a[3] = 2 * 3 - 3
a[3] = 3
j = 4
a[4] = 2 * 4 - 3
a[4] = 5
a = {-3, -1, 1, 3, 5}
while loop:
sum = 0
i = 0
sum = sum + a[0]
sum = 0 + (-3)
sum = -3
is -3 more than 0?
No, don't print anything.
i = i + 1
i = 0 + 1
i = 1
sum = sum + a[1]
sum = -3 + (-1)
sum = -4
is -1 more than 0?
No, don't print anything.
i = i + 1
i = 1 + 1
i = 2
sum = sum + a[2]
sum = -4 + 1
sum = -3
is 1 more than 0?
Yes
Print i (2)
Print 2 * a[2] (2)
i = i + 1
i = 2 + 1
i = 3
sum = sum + a[3]
sum = -3 + 3
sum = 0
is 3 more than 0?
Yes
Print i (3)
Print 2 * a[3] (6)
i = i + 1
i = 3 + 1
i = 4
while loop end
print sum (0)
Things that we're printed: 2, 2, 3, 6, 0
Last edited on Apr 30, 2017 at 10:28pm UTC
Apr 30, 2017 at 10:40pm UTC
Thank you very much that helps immensely! It's a lot easier to visualize now.