Loops, Loops and more Loops! Pt.1

David Kirsch
Jan 4 · 4 min read

Working a lot with JavaScript recently I noticed when writing my programs I have an array of loops to choose from….see what I did there?😉. While they all practically do the same thing (repeat a block of code), there are many reasons why you would choose one over another, mainly because it will make your life easier.

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

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.

Infinite while loop

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.

99% of the time your code will be similar to this, example 1
Heed with caution, example 2

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.

Trying a new restaurant: Do while loop snippet

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!

Nerd For Tech

From Confusion to Clarification

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store