The need to have programs perform the same task multiple times over is common, and it would be tedious to copy and paste the same code for as many times as you need to run it. That’s where repetition structures come in.
whileloop is a condition-controlled loop. A condition controlled loop causes a statement, or set of statements to repeat as long as the condition is
true. Since this is a prestest loop, it first tests the condition before the loop is run. In the example, it has to test whether or not the variable
numberexists, and then determine whether the condition is
true. Just be careful of infinite loops
forloop is a count-controlled loop. You'll choose to use this type of loop when you have to do something a specific amount of times. The
for clauseassigns a variable that will take on the value depending on the statement we give it within the loop. First,
numis assigned to the number 35, then on the second loop it is assigned to number 66 and so on until it executes each iteration of the list of numbers in the loop. The
rangemethod is an option to use with a
for clauseif you'd like to be specicic about your iterations.
You can pass it a single argument:
This will print out 1 through 5.
In this case, it'll print out numbers 1-4 since we're telling it to start at 1 and go up-to but not including 5.
Or three arguments:
The first argument is 1, so we'll start at 1. The second argument is 10, so we'll go up to but not include 10. The third argument is 2 which is the step value. This means that the number 2 will be added to each successive number in the sequence.
It will print out the numbers 1, 3, 5, 7, 9.
Calculating a running total to show how an
accumulatorworks. Introducing: Augmented Assignment Operators! We've seen variables on the left, and values on the right, like so:
x = 10
But when we need to increment a variable, we have to put that variable on the right side of the equation as well:
x = x + 10
When we incorperate this into a loop, each iteration of the loop increments the value of
xby 10. This code will continue to add the numbers 1-5 to total, which is set to 0 initially and eventually print out the single number 15.
Input validation loopsA quick ditty on catching "bad" data before it enters the program. This will allow us to display an error message to the user if the program recieves incorrect or bad data. Here we're not checking to make sure the user doesn't enter an unreasonable number of hours worked per week, 400, for example. Since there are only 168 hours in a week, 400 hours worked in a week would be "bad" data.
Nested loopsYou can put loops inside of loops inside of loops inside of loops... Let's build a clock to showcase this one: Run that code to see how it operates! First, the inner loop goes through all of its iterations. That's why we see the
secondsvariable counting up to 60 first. Then minutes runs through, and then hours.
Got Questions❓, Comments 🗣 or Edits ✍
Use the Twitter thread below and hashtag #E4Everything to get in touch with me regarding this blog post:
Repetition structures, for loops, while loops, loops inside loops, repeating things, lots of repetition and learn not to repeat yourself in this blog post:— Emily Anne Moses (@emilyannemoses) June 21, 2019