Learn

There’s one more way to give looping instructions to a computer. We define a sequence of desired tile colors (a list), and tell the computer to repeat the instructions for each item in the sequence. Here’s an example list:

festiveList = ['pink', 'green', 'red', 'pink', 'green', 'red' ]

and the loop would look like:

For each color in festiveList: placeTile(color)

Why does this program look shorter than the ones before it? We only need to write placeTile() once because the pink-green-red repetition is already described in the list.

color is a variable whose value changes each iteration. It represents each item in the list. On the first iteration, its value would be 'pink', then 'green', then 'red', and so on.

The sequence we used was a list, but we can use other similar data types. The umbrella term for those is collection, so we may also call for each loops collection loops.

Use this loop when you need to perform a task for every item in a list, or when the order of things must be maintained. In this case, both were important. A tile must be placed for each item in the list and the order of them is essential to the pattern.

Instructions

Place the same 30 tiles as before, but now with a for each loop!

  • Select the collection (list) to use
  • Run it

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?