I know when I started out programming years ago, I was confused by when I should be using a for loop or a while loop or even a do while loop. I thought now would be a good time to break down each type of loop for anyone struggling with control flow statements. In this part 1 of 2 parts blog, I will discuss the difference between while and do-while loops. Part 2 will cover three different types of for loops.
While loops are pretty straight-forward when it comes to figuring out when to use it and when you should look for another loop. While loops are usually the first type of control flow statements we learn when starting to learn how to repeat blocks of code. We start with a basic while loop when we begin programming because, well, we use while loops everyday as humans. For example: While it’s raining, I will stay inside. While it’s hot, turn on the AC. While it’s the weekday, I work. While loops need a boolean (true/false) statement to work, without such, your loops wont execute.
Here, you see a simple while loop. We have a boolean statement that checks to see if the condition is true and then we have the block of code that will repeat while the condition is true. We have one big error though. Do you see it? If we were to run this code, we would cause our program to crash. Why? Although we have a condition that meets true, there is nothing in place to change this boolean to false, causing it to run an infinite amount of times.
Most of the time you will pass your condition as the argument of the while statement, instead of typing in true. If you have a valid reason to use true as your condition, you MUST make sure you have a forced exit somewhere in your code.
The two snippets above show different ways you can write your while loop. I suggest if you are just starting out stick with the example 1. Both snippets will print out the same output, however in example 2, I am forcing a break out of the while loop if the condition is met in the if statement.
So, while loops are easy to understand. We use them when we want to repeat a block of code that will run as along as the condition evaluates to true and exit when the condition evaluates to false.
Some of you maybe wondering….what if you want to run a statement first before checking a condition? That is when a Do While loop is useful. Let me give you another real life example of how we use do while’s throughout our life. Let’s say a new restaurant opens up near by your house. You wanna try it out since you haven’t been there before, but you only want to continue going there if the food is good. This is exactly how do while loops work. You say I want to run this block of code AT LEAST ONCE, then check to see if the condition is met. Do while’s are similar to a standard while loop, except do while’s guarantee an execution of the code block.
As you can see from the above code snippet, the do section is executed immediately when the program runs. the code block being run is a function called eatAtRestaurant(). This will be run one time automatically and then after it completes all the steps in the code, it will then check if the condition is met. If the condition evaluates to true, the do code block will run again.
That’s about it for while and do-while loops. I hope this helps you see there are reasons why you would choose one over the other. In my next blog (part 2) I will explain for loops and the different types. I look forward to seeing you back next week!