> For the complete documentation index, see [llms.txt](https://docs.gpmautomate.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gpmautomate.com/automate-en/action-guide/blocks/while.md).

# While

While is a command block used to repeatedly execute a group of actions continuously until a condition is met.

Unlike the For loop (which iterates a fixed number of times), the While loop is typically used when you do not know in advance how many times the scenario will need to repeat, as it completely depends on the actual state at runtime.

🎥 Watch the tutorial video: [Here](https://youtu.be/4O4dPC-CNHM).

#### How it works:

* Before starting each iteration, the system will check the condition that you configured.
* If the condition is not met, the actions inside the While block will continue to be executed.
* As soon as the condition is met, the system will stop the loop and move on to the subsequent actions below the While block.

#### Real-world example: Waiting to get the OTP code from Mail/Telegram

Suppose you are creating a script to register an account and need to wait for the system to send the OTP code to your Email. Since the time it takes for the Mail to arrive can vary, you cannot know in advance how long to wait:

* You place the Read mail code action inside the While block.
* The loop's exit condition: When the variable `$otpCode` receives a value (is no longer empty).
* The system will continuously check and read the mailbox. As soon as it successfully retrieves the OTP code, the condition is met, and the loop stops so the script can proceed to the next step of entering the code.

<figure><img src="https://2961903581-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpIRrmOLZkhHBXdAiidIw%2Fuploads%2Fgit-blob-0053acfc883a1a11f3e3b7e1a1cacaf44380ee5b%2Fcb-02.png?alt=media" alt=""><figcaption></figcaption></figure>

> ⚠️ Important note: When using the While loop, you need to ensure that the actions inside the command block will change the state of the exit condition. If the condition is configured incorrectly or the Mail never arrives, the script will enter an infinite loop (hanging the script).
>
> To optimize, you should also combine it with a counter variable for the number of checks. If there are more than 10 attempts without receiving Mail, use the Exit loop action to exit the loop, or the Stop action to completely halt the program and move on to the next profile.


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.gpmautomate.com/automate-en/action-guide/blocks/while.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
