> For the complete documentation index, see [llms.txt](https://eleven-fifty-academy.gitbook.io/javascript-101-fundamentals/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eleven-fifty-academy.gitbook.io/javascript-101-fundamentals/1-javascript-fundamentals/1-js-fundamentals/3-loops/while-loops.md).

# While Loops

## While Statement Loops

It is possible for a `while` loop to stand without the `do`. Including the `do` guarantees that the statement inside the `while` will process once. It is wise to take care with stand-alone `while` loops, as they have a natural tendency to go infinite.

The `while` loop executes its statements if the designated condition (i.e. `n < 3`) reads as TRUE. If the condition reads FALSE, the statement within the loop stops executing and control passes on.\
&#x20;The condition is checked before the statement (i.e. `n++; x += n;`) is executed, at which point, the condition is checked again.\
&#x20;If the condition returns FALSE, execution stops and control passes on before the statement is executed.\
&#x20;To execute multiple statements, use a block statement (`{...}`) to group.

```javascript
//While Loops

//Create a variable
var score = 0;
        //Set a condition in parens
while(score < 10){
    //Set an increment operation
    score++;
    //Print to the console
    console.log("Score: ", score);
}

//Another example
var age = 0;
while(age < 100){
    age+=10;
    console.log("Age:", age);
}

if (age === 100){
    console.log("I made it!");
}

//A challenge -create a while loop that prints 10-100 by 10s. AT 50
// print "Halfway there!"

var counter = 0;
while(counter < 100){
    counter+=10;
    if(counter === 50){
        console.log("There's halfway");
    } else {
        console.log(counter);
    }
}
```

## Difference Between For and While Loops

```javascript
// for loop

var cars = ['BMW', 'Volvo', 'Saab', 'Ford'];
var i = 0;
var text = '';

for (;cars[i];) {
  text += cars[i] + '<br>';
  i++;
}

// while loop

var cars = ['BMW', 'Volvo', 'Saab', 'Ford'];
var i = 0;
var text = '';

while (cars[i]) {
  text += cars[i] + '<br>';
  i++;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://eleven-fifty-academy.gitbook.io/javascript-101-fundamentals/1-javascript-fundamentals/1-js-fundamentals/3-loops/while-loops.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
