A loop lets you repeat instructions multiple times without writing the instructions out multiple times.
From:
create a variable
call a function
create a variable
call a function
create a variable
call a function....
To:
Repeat this 15 times:
create a variable
call a function
for loop, or a count-controlled loop
- executes a set of instructions for a specified number of times
- used if you know the number of times you need to repeat the instructions
iteration – one pass through the instructions
while loop, or condition-controlled loop
- repeats a set of instructions while that condition is true.
While there are pink tiles available:
placeTile('pink')
placeTile('orange')
placeTile('mint')
for each loops
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)
The larger the sample size and the more diverse your dataset is, the more confident you’ll be in your results.
Leave a Reply