From 4d07192400f763eba166cb658c4d766f3b4e364f Mon Sep 17 00:00:00 2001 From: Xingyu Wang Date: Sat, 25 Feb 2023 15:29:25 +0800 Subject: [PATCH 1/2] RP @geekpi https://linux.cn/article-15575-1.html --- ...Lua loops how to use while and repeat until.md | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) rename {translated/tech => published}/20230214.1 ⭐️ Lua loops how to use while and repeat until.md (79%) diff --git a/translated/tech/20230214.1 ⭐️ Lua loops how to use while and repeat until.md b/published/20230214.1 ⭐️ Lua loops how to use while and repeat until.md similarity index 79% rename from translated/tech/20230214.1 ⭐️ Lua loops how to use while and repeat until.md rename to published/20230214.1 ⭐️ Lua loops how to use while and repeat until.md index 765014feb4..e5bf4bf6f5 100644 --- a/translated/tech/20230214.1 ⭐️ Lua loops how to use while and repeat until.md +++ b/published/20230214.1 ⭐️ Lua loops how to use while and repeat until.md @@ -3,14 +3,16 @@ [#]: author: "Seth Kenlon https://opensource.com/users/seth" [#]: collector: "lkxed" [#]: translator: "geekpi" -[#]: reviewer: " " -[#]: publisher: " " -[#]: url: " " +[#]: reviewer: "wxy" +[#]: publisher: "wxy" +[#]: url: "https://linux.cn/article-15575-1.html" Lua 循环:如何使用 while 和 repeat until ====== -控制结构是编程语言的一个重要特征,因为它们使你能够根据通常在程序运行时动态建立的条件来指导程序的流程。不同的语言提供了不同的控制,在 Lua 中,有 while 循环、for 循环和 repeat until 循环。这篇文章涵盖了 while 和 repeat until 循环。由于它们的灵活性,我在一篇[单独的文章][1]中介绍 for 循环。 +> 学习如何以及何时在 Lua 中使用 while 和 repeat until 循环。 + +控制结构是编程语言的一个重要特征,因为它们使你能够根据通常在程序运行时动态建立的条件来指导程序的流程。不同的语言提供了不同的控制,在 Lua 中,有 `while` 循环、`for` 循环和 `repeat` `until` 循环。这篇文章涵盖了 `while` 和 `repeat until` 循环。由于它们的灵活性,我在一篇 [单独的文章][1] 中介绍 `for` 循环。 条件是由一个使用运算符的表达式来定义的,运算符是你在数学课上可能认识的符号的一个花哨的术语。Lua 中有效的运算符有: @@ -31,8 +33,7 @@ Lua 循环:如何使用 while 和 repeat until - `foo > 3`:变量 `foo` 是否大于 3?`foo` 必须是 4 或更大才能满足这个条件。 - `foo >= 3`:`foo` 是否大于或等于 3?`foo` 必须是 3 或更大才能满足这个条件。 - `foo > 3 and bar < 1`:`foo` 是否大于 3 而 `bar` 小于 1?要满足这个条件,`foo` 变量必须在 `bar` 为 0 的同时为 4 或更大。 -- `foo> 3 or bar < 1`:`foo` 是否大于 3?或者说,`bar` 是否小于 1?如果 `foo` 是 4 或更大,或者 `bar` 是 0,那么这个条件就是真的。如果 `foo` 是 4 或更大,而 `bar` 是 0,会怎样?答案出现在本文的后面。 - +- `foo> 3 or bar < 1`:`foo` 是否大于 3?或者,`bar` 是否小于 1?如果 `foo` 是 4 或更大,或者 `bar` 是 0,那么这个条件就是真的。如果 `foo` 是 4 或更大,而 `bar` 是 0,会怎样?答案出现在本文的后面。 ### While 循环 @@ -64,9 +65,9 @@ $ lua ./while.lua No more zombie apocalypse! ``` -### Until 循环 +### until 循环 -Lua 还有一个 repeat until 循环结构,本质上是一个带有 “catch” 语句的 while 循环。假设你在从事园艺工作,你想追踪还剩下什么可以收获的东西: +Lua 还有一个 `repeat` `until` 循环结构,本质上是一个带有 `catch` 语句的 `while` 循环。假设你在从事园艺工作,你想追踪还剩下什么可以收获的东西: ``` mytable = { "tomato", "lettuce", "brains" } @@ -110,7 +111,7 @@ while ( foo > 3 or bar < 1 ) do end ``` -你可以安全地运行这段代码,但它确实模仿了一个意外的无限循环。有缺陷的逻辑是 `or` 运算符,它允许这个循环在 `foo` 大于 3 和 `bar` 小于 1 的情况下继续进行。`and`运算符有不同的效果,但我让你去探索。 +你可以安全地运行这段代码,但它确实模仿了一个意外的无限循环。有缺陷的逻辑是 `or` 运算符,它允许这个循环在 `foo` 大于 3 和 `bar` 小于 1 的情况下继续进行。`and` 运算符有不同的效果,但我让你去探索。 无限循环实际上有其用途。图形应用使用技术上的无限循环来保持应用程序窗口的开放。我们没有办法知道用户打算使用这个程序多久,所以程序无限地运行,直到用户选择**退出**。在这些情况下使用的简单条件显然是一个总是被满足的条件。下面是一个无限循环的例子,为了方便起见,还是内置了一个安全陷阱: @@ -131,7 +132,7 @@ end ### Lua 中的循环 -从示例代码中可以看出,尽管有不同的实现方式,但循环基本上都是朝着同一个目标工作。选择一个对你来说有意义的,并且在你需要执行的处理过程中效果最好的。以防万一你需要它:终止失控循环的键盘快捷键是 **Ctrl+C**。 +从示例代码中可以看出,尽管有不同的实现方式,但循环基本上都是朝着同一个目标工作。选择一个对你来说有意义的,并且在你需要执行的处理过程中效果最好的。以防万一你需要它:终止失控循环的键盘快捷键是 `Ctrl+C`。 -------------------------------------------------------------------------------- @@ -140,10 +141,11 @@ via: https://opensource.com/article/23/2/lua-loops-while-repeat-until 作者:[Seth Kenlon][a] 选题:[lkxed][b] 译者:[geekpi](https://github.com/geekpi) -校对:[校对者ID](https://github.com/校对者ID) +校对:[wxy](https://github.com/wxy) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 [a]: https://opensource.com/users/seth [b]: https://github.com/lkxed/ [1]: https://opensource.com/article/22/11/lua-for-loops +[0]: https://img.linux.net.cn/data/attachment/album/202302/25/152802hoi4khxzm3izpejh.jpg \ No newline at end of file From 22e7427b16b9c0363eae9e07c68e27b4bac3cef0 Mon Sep 17 00:00:00 2001 From: Xingyu Wang Date: Sat, 25 Feb 2023 15:32:39 +0800 Subject: [PATCH 2/2] R --- ...20230214.1 ⭐️ Lua loops how to use while and repeat until.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/published/20230214.1 ⭐️ Lua loops how to use while and repeat until.md b/published/20230214.1 ⭐️ Lua loops how to use while and repeat until.md index e5bf4bf6f5..6a49ad26a2 100644 --- a/published/20230214.1 ⭐️ Lua loops how to use while and repeat until.md +++ b/published/20230214.1 ⭐️ Lua loops how to use while and repeat until.md @@ -35,7 +35,7 @@ Lua 循环:如何使用 while 和 repeat until - `foo > 3 and bar < 1`:`foo` 是否大于 3 而 `bar` 小于 1?要满足这个条件,`foo` 变量必须在 `bar` 为 0 的同时为 4 或更大。 - `foo> 3 or bar < 1`:`foo` 是否大于 3?或者,`bar` 是否小于 1?如果 `foo` 是 4 或更大,或者 `bar` 是 0,那么这个条件就是真的。如果 `foo` 是 4 或更大,而 `bar` 是 0,会怎样?答案出现在本文的后面。 -### While 循环 +### while 循环 只要满足某个条件,while 循环就会执行指令。例如,假设你正在开发一个应用来监测正在进行的僵尸末日。当没有剩余的僵尸时,就不再有僵尸末日了: