Repeating StepsΒΆ
Learning Objectives:
- Use a
for
loop to repeat code. - Use
range
to create a list of numbers
We often have to repeat steps in a process. If you are making a cake the recipe might say add an ingredient and then stir it 50 times or until the batter is smooth. You can probably handle stirring the batter 50 times, but what if you had to stir it 1,000 times? You might want a machine to do that.
A computer never gets tired. It can do the same thing over-and-over without slipping up. It can execute a program for as long as it has electricity. So, there must be a way to tell a computer to do things over-and-over. Computers know how to repeat steps in a program and the way they do this is often called a loop or iteration. Have you ever had an earworm? This is when a song gets stuck in your head and keeps repeating as if the song was set to loop (repeat after playing).