逐项累加
从零开始,每读到一个数就把它加到总数里。
简单示例
初始状态2 + 4
结果6
function main()
total = 0
total += 2
total += 4
println(total)
end
main()
6
广告
生活中的例子
三个班分别捐了 5 本、2 本和 4 本书。图书馆一共能把多少本书放上书架?
初始状态5 + 2 + 4
结果11
function main()
books = [5, 2, 4]
total = 0
i = 0
while i < 3
total += books[1 + i]
i += 1
end
println(total)
end
main()
11