Add the values in a list
Add every item once. Repeated values still count each time they occur.
for
Simple example
Add one, two and three.
Start1 · 2 · 3
Result6
def main() raises:
var values: List[Int] = [1, 2, 3]
var total = 0
for value in values:
total += value
print(total)
6
Advertisements
Everyday examples
Add the three amounts on a receipt.
Start5 · 5 · 8
Result18
def main() raises:
var values: List[Int] = [5, 5, 8]
var total = 0
for value in values:
total += value
print(total)
18
How many passengers boarded at the three stops?
Start2 · 7 · 1
Result10
def main() raises:
var values: List[Int] = [2, 7, 1]
var total = 0
for value in values:
total += value
print(total)
10
How many exercise books arrived in three boxes?
Start10 · 8 · 9
Result27
def main() raises:
var values: List[Int] = [10, 8, 9]
var total = 0
for value in values:
total += value
print(total)
27