Close
Close Window

Computational Thinking

Chapter 1 Iteration Slides and Exercises

Show Source |    | About   «  0.1. List Iteration   ::   Contents   ::   2.1. Glossary  »

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.

Created with Raphaël 2.1.2
  1. 4
  2. 13
  3. 6
  4. 9
  5. 11
for each item
price
do
Iteration
Variable
Proficient Saving... Error Saving
Server Error
Resubmit

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.

Created with Raphaël 2.1.2
  1. 4
  2. 13
  3. 6
  4. 9
  5. 11
for each item
price
do
print (price)
STATE
price
CONSOLE
Proficient Saving... Error Saving
Server Error
Resubmit

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

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.

Created with Raphaël 2.1.2
  1. 4
  2. 13
  3. 6
  4. 9
  5. 11
set
total
=
0
for each item
price
do
set
total
=
total + price
print (total)
STATE
price
total
CONSOLE
Proficient Saving... Error Saving
Server Error
Resubmit

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.

Created with Raphaël 2.1.2
  1. 4
  2. 13
  3. 6
  4. 9
  5. 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
Proficient Saving... Error Saving
Server Error
Resubmit

   «  0.1. List Iteration   ::   Contents   ::   2.1. Glossary  »

nsf
Close Window