As you saw in the video, while loops are used when you know the final state you’d like to achieve, but you don’t know how many iterations (repeats) you’ll need to get there. How could this apply to the real world?
Remember that chocolate you bought in bulk? Well, imagine it’s a lazy Sunday afternoon, you’ve got your box of chocolate with you, maybe a few others in reserve. You start eating one piece of chocolate, then another and another...until it’s gone, and the box is empty. 🍫
Looks like you’ve gotten into a loop! You just keep going and going until you’ve reached the end condition: no more chocolate. You could write this as:
While the example sounds delicious, it could get vicious. If you don't have an endpoint, or the program can’t accomplish its goal, it’ll keep going and become an infinite loop. Initially, that seems awesome, right? Chocolate continues to magically appear, and you keep eating it, but your stomach can only handle so much. Your digestive system will inevitably “malfunction.” 🤢 The same goes for your computer. You could have a program meltdown, which would lead to a crash.💥 It’s kind of like a toddler on too much sugar. Or you, after you eat all that chocolate.
After you recover from your intense chocolate binge, you decide to train for a marathon. On the day of the race, all you need to have in mind is another while loop:
With this situation, there is a pretty clear end state to reach: crossing the finish line! No infinite loops here! Thank goodness.
Now let's check out the second type of loop: for loops! Let's stick with the chocolate scenario. There’s another way you can get rid of a huge stockpile of chocolate: making desserts with it!
You pick a recipe for a chocolate cake which requires about 4 cups of crushed chocolate. Since you only have one measuring cup in your kitchen cupboard, you’ll have to drop your chocolate in the cake batter one cup at a time.
This is the perfect situation for a for loop which is used when you have a specific amount of iterations, or loops, to do. In this case, you have four iterations - one for each cup of chocolate you pour into the batter. Once you’ve dropped all four in, you’re done!
Now that you’ve made your cake, you have the perfect companion for your lazy Sunday afternoon. That, and your new video streaming service. You’ve just found out that your favorite TV series has a new season, and you are ready to hunker down and binge watch all 10 episodes.
Well, wouldn’t you know it, but once again you’re living a for loop. You’ll continue watching exactly 10 episodes, one after the other, and then you’ll stop. See? You’ve been thinking like a computer all along.
Try it out for yourself!
You are writing a guessing game that will randomly decide a secret number between 1 and 100. It then begins the game by asking the user to try to guess the secret number. The user makes a guess, and the computer says whether this guess corresponds to the secret number.
First, write the instructions for the above computer. Next, to complexify the program you have two options once the person guesses:
Have the user guess until they get the right answer.
Have the user guess a specific number of times.
For which option would you use a for loop? For which one would you use a while loop?
While loops are used when you know the end state you want, but not the number of iterations.
For loops are used when you know the exact number of iterations you need.
An infinite loop is a while loop that gets stuck because it doesn’t have an end condition.