How to Pick Between a For Loop and While Loop

Both methods can help you iterate through your code. Here’s what you need to know to figure out the best tool for the job.

Written by Jonathan Hsu
Published on Aug. 25, 2022
How to Pick Between a For Loop and While Loop
Image: Shutterstock / Built In
Brand Studio Logo

As a newcomer to coding, the while and for loops are the first two iteration structures you will learn. They’re critical to understand, as they open up a world of possibilities when paired with decision structures like if/else statements.

If you’re learning on your own or your instructor was not particularly opinionated, there’s a good chance you’re asking yourself, “How do I pick between the two?”

Difference Between a For Loop and While Loop

  • For Loop: A for loop is an iteration method that is best used when you know the number of iterations ahead of time. It’s always followed by the initialization, expression and increment statements.
  • While Loop: A while loop is an iteration method that is best used when you don't know the number of iterations ahead of time. The contents of the loop are executed as long as the expression evaluates to true.

We’re here to answer that question. While I’ll be using JavaScript for all the code examples, the concepts and opinions really extend to any language that supports both of these.

Let’s dive in. We’ll begin with a brief recap of the syntax involved for both — just in case.

 

What Is a For Loop?

A for loop is more structured than the while loop. The keyword for is used, followed by three statements:

  1. Initialization: Executed before the loop begins.
  2. Expression: Evaluated before each iteration, exits the loop when false.
  3. Increment: Executed at the end of each iteration.
for(count=1; count < 10; count++) {
   console.log(count);
}

The increment does not have to be ++, but you’ll see that the most often.

So, in summary, the while loop has a looser syntax, and the for loop has a more rigid syntax. A while loop expects some sort of modification to the variable in the body, whereas everything is in the for loop’s definition.

A tutorial on JavaScript loops. | Video: Programming with Mosh

More on Software DevelopmentHow to Undo the Last Commit Using Git Reset Command

 

What Is a While Loop?

A while loop has lower overhead between the two iteration structures. The loop consists of the keyword while followed by an expression and curly braces. The contents of the loop — what is between the curly braces — are executed as long as the expression evaluates to true.

while(count < 10) {
   console.log(count);
}

Now, the example above is incomplete because a while loop needs a way to eventually exit the loop. Normally, this is done by modifying the variable in the expression.

let count = 1;

while(count < 10) {
   console.log(count);
   count++;
}

More on JavaScript5 Ways to Check If an Object Is Empty in JavaScript

 

For Loops vs. While Loops

Deciding which loop to use is ultimately a judgment call. Don’t let my opinion sway you away from what’s comfortable for your own eyes, especially if you’re a beginner.

There are other more advanced iteration structures that you can eventually grow to learn, so don’t get hung up on deciding between these two.

With that said, my universal rule for choosing between a while loop and for loop is that I use while loops when I do not know the number of iterations ahead of time and for loops when I do know.

Let’s go through a few examples of each:

  • Use a for loop to iterate over an array.
  • Use a for loop when you know the loop should execute n times.
  • Use a while loop for reading a file into a variable.
  • Use a while loop when asking for user input.
  • Use a while loop when the increment value is nonstandard.
Hiring Now
Bringg
Cloud • Enterprise Web • Logistics • Software
SHARE