> 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-zh/cao-zuo-zhi-nan/blocks/while.md).

# While

与 For 循环（以固定次数循环）不同，While 循环通常在您不知道将要重复多少次的情况下使用，而完全依赖于运行时的实际状态。

#### 工作原理：

* 在每次循环开始之前，系统将检查您配置的条件（Condition）。
* 如果条件尚未满足，While 块中的操作将继续执行。
* 一旦条件满足，系统将停止循环并转向 While 块下方的其他操作。

#### 实际示例：等待获取 OTP 邮件/Telegram

假设您正在编写注册账户的脚本，需要等待系统将 OTP 代码发送到电子邮件。由于邮件到达的时间可能快或慢，您无法提前知道需要等待多久：

* 您将读取邮件代码的操作放入 While 块中。
* 循环的停止条件：当变量 `$otpCode` 接收到值（不再为空）。
* 系统将持续检查并读取邮箱。一旦成功获取 OTP 代码，条件满足，循环停止以继续执行填写代码的步骤。

> ⚠️ 重要注意事项：使用 While 循环时，您需要确保块内的操作会改变停止条件的状态。如果条件配置错误或邮件永远不会到达，脚本将陷入无限循环（脚本挂起）。
>
> 为了优化，您应该结合一个计数变量。如果超过 10 次没有收到邮件，请使用 Exit loop 操作退出循环，或使用 Stop 操作完全停止程序并转到下一个配置文件。


---

# 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-zh/cao-zuo-zhi-nan/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.
