Learn

When we give instructions once and tell the computer how many times to repeat them, we are using a for loop, or a count-controlled loop. It executes a set of instructions for a specified number of times:

For 10 times: placeTile('pink') placeTile('orange') placeTile('mint')

The tile-placing instructions are the same from the previous exercise – Place a pink tile, Place a orange tile, Place a mint tile – written as calls to the placetile() function.

When a computer receives this program it sets a counter to 0 and executes the instructions in the body of the loop. After each iteration (one pass through the instructions), it advances the counter by 1. The process repeats until the counter is 10, meaning 10 iterations are completed.

The for loop is good to use when you know the number of times you’d like to perform a task before you begin, like printing 3 copies of a document or inserting 8 rows into a table.

Instructions

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

  • Insert the three instructions (pink, orange, mint)
  • Select the number of times the loop should run (hint: 30 / 3 = 10)
  • Run it

Much faster than the previous exercise, right? You got the same outcome in much less time! Additionally, using this method reduces your chance of making a mistake in the pattern.

Sign up to start coding

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?