# While & Do/While Loops

## While Loop

Probably the simplest loop structure, this code block will run if and only if the condition is true. Once it becomes false, the loop stops. You can also break out of the loop with a `break;` statement.\
\
Keep in mind that the while loop may not run at all, depending on the condition.

### Syntax

The syntax of a While loop is some of the simplest possible:

![](https://1200419583-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LKbpNQDFNJap9OTDSt6%2F-LQKfUa_-vF3sgK9kedq%2F-LQKfqubMoK2K5tC9HWH%2FWhile%20Loop.png?alt=media\&token=ca862841-2e2b-46e1-8cfc-16464c40d342)

```javascript
while (someCondition) {
    // Do anything in here
}
```

### Example 1 - Infinite Loop

```javascript
// This will loop forever (infinite loop) 
// unless you call 'break;' somewhere inside the loop block
// Infinite loops are VERY bad as you have no ability to properly end it
while (true) {
    console.log("INFINITE LOOP!");
}
```

### Example 2 - Until a Value

{% embed url="<https://tech.io/snippet/yiROuEy>" %}
Clicking the blue "Powered By" button will open this example in a new tab
{% endembed %}

## Do While Loop

The Do While is a variation on the While. It is ***guaranteed to run at least once*** but it will only loop back to the top if the condition is true.

### Syntax

The syntax is *similar* to the While loop but notice the differences - especially the semicolon ending the statement.

![](https://1200419583-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LKbpNQDFNJap9OTDSt6%2F-LQKfUa_-vF3sgK9kedq%2F-LQKfxXCK3iMCbkysFS5%2FDo%20While%20Loop.png?alt=media\&token=2bf1a8bc-0f4a-4767-a72d-388b55fde24d)

```javascript
do {
    // Some code that is guaranteed to run once
} while (someCondition);

/*** Note that there is a semicolon at the end of the statement.  ***/
```

### Example 3 - Not Zero

```javascript
// Add numbers the user enters until they enter zero

// Assuming good input from the user
let userNumber, sum = 0;

// loop body is executed at least once
do {
    userNumber = Number(prompt("Please enter a number:"));
    sum += userNumber;
} while (userNumber !== 0);

console.log("Total sum was %i", sum);

// You might not recognize the console.log on line 12. That is a different
// way of including a variable into a string (it only works in console.log).
```

### Example 4 - Print a Box

{% embed url="<https://tech.io/snippet/uowOxmT>" %}
Clicking the blue "Powered By" button will open this example in a new tab
{% endembed %}


---

# Agent Instructions: 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:

```
GET https://cs.brash.ca/unit-3/loops/while-loop.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
