1.1. List Iteration¶
1.1.1. List Iteration¶
1 / 11
Settings
<<<>>>
This animation includes a list of numbers: 4, 13, 6, 9, 11. The iteration will process all the numbers in this list, one number at a time. While this example has five numbers, an iteration will generally work for any length of list.
- 4
- 13
- 6
- 9
- 11
for each item
price
do
Iteration
Variable
Variable
1 / 16
Settings
<<<>>>
This animation shows that the iteration variable, price, is part of the state. Remember that the state shows only the current value a variable.
- 4
- 13
- 6
- 9
- 11
for each item
price
do
print (price)
STATE
price
CONSOLE
1 / 23
Settings
<<<>>>
This animation shows the steps of an algorithm to add together and print the values in a list. This animation is an example of the accumulate pattern. The values in the list to be the price of items being purchased.
- 4
- 13
- 6
- 9
- 11
set
total
=
0
for each item
price
do
set
total
=
total + price
print (total)
STATE
price
total
CONSOLE
1 / 24
Settings
<<<>>>
This animation shows the steps of an algorithm to calculate and print the average of the values in a list. This animation is an example of the accumulate pattern where two values are accumulated within the same iteration. In this problem we take the values in the list to be the price of items being purchased.
- 4
- 13
- 6
- 9
- 11
set
count
=
0
set
total
=
0
for each item
price
do
set
count
=
count + 1
set
total
=
total + price
set
average
=
total / count
print (average)
STATE
count
total
price
average
CONSOLE