> For the complete documentation index, see [llms.txt](https://gitbook.hbcomputersecurity.co.uk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.hbcomputersecurity.co.uk/core-technical-skills/core-skills/linux/shell-scripting/loops/while-loops.md).

# while Loops

while loops are also fairly common, and execute code while an expression (or condition) is true. while loops have a simple format and, like `if` statements, use the square brackets (`[]`) to define the conditional test

From a high-level perspective, this is what we're looking at:

{% code title="Psuedo code - while loop" %}

```bash
while [ <some conditional test> ]
do
    <perform some action>
done
```

{% endcode %}

The easiest way to understand the difference between the types of loops is to write a script that performs the same job. So, we will create the same outcome as our for loop, and examine the difference:

```bash
#!/bin/bash

counter=1

while [ $counter -le 10 ]
do
  echo "192.168.1.$counter"
  ((counter++))
done
```

Aside from the obvious difference that this is no longer a "one-liner", we can see immediately that there is no defined list. Instead, a variable is declared with a fixed value (`counter=1`), and the condition states that this loop will run **while** the value of counter is **less that or equal to 10**.&#x20;

At the latter stages of the script, we see `counter++` - this simply means take the current value or `counter`, and add it to itself, which in our case means add 1, as per the original declaration. So, with each loop, counter goes up in value by 1, until it reaches 10 and the loop ends:

![Example of a while loop](/files/-MkgoXExR_5ORfQ8ujyI)

As you can see, although there is a clear difference in the logic being used, the outcome remains the same.


---

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

```
GET https://gitbook.hbcomputersecurity.co.uk/core-technical-skills/core-skills/linux/shell-scripting/loops/while-loops.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.
