> 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-guides/blocks/while.md).

# While

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 entirely depends on the actual state at runtime.

#### How it works:

* Before starting each iteration, the system will check the condition that you have 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 for OTP code via Mail/Telegram

Suppose you are scripting an account registration and need to wait for the system to send the OTP code to your Email. Since the time for the Mail to arrive can be fast or slow, you cannot know in advance how long to wait:

* You place the action Read mail code inside the While block.
* The stopping condition for the loop: 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="/files/GKFVESBEApSfTQ08jHAG" 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 stopping condition. If the condition is configured incorrectly or the Mail never arrives, the script will fall into an infinite loop (hanging the script).
>
> To optimize, you should also combine a counter variable for the number of checks. If there are more than 10 checks 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 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://docs.gpmautomate.com/automate-en/action-guides/blocks/while.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.
