A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://builtin.com/software-engineering-perspectives/for-loop-vs-while-loop below:

How to Pick Between a For Loop and While Loop

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

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.

A tutorial on JavaScript loops. | Video: Programming with Mosh 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.

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:

When should I use a for loop instead of a while loop?

When coding, use a for loop when you know the number of iterations in advance, and use a while loop when the number of iterations is uncertain and depends on a condition.

What are the key differences between for and while loops?

In JavaScript, a for loop allows for initialization, expression and increment statements when defined, giving it a more rigid structure. A while loop only requires an expression statement when defined.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4