mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
commit
40c9837d74
@ -1,148 +0,0 @@
|
||||
[#]: subject: "Lua loops: how to use while and repeat until"
|
||||
[#]: via: "https://opensource.com/article/23/2/lua-loops-while-repeat-until"
|
||||
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Lua loops: how to use while and repeat until
|
||||
======
|
||||
|
||||
Control structures are an important feature of programming languages because they enable you to direct the flow of the program based on conditions that are often established dynamically as the program is running. Different languages provide different controls, and in Lua there's the while loop, for loop, and repeatuntil loop. This article covers the while and repeat until loops. Because of their flexibility, I cover for loops in a [separate article][1].
|
||||
|
||||
A condition is defined by an expression using an operator, which is a fancy term for symbols you may recognize from math classes. Valid operators in Lua are:
|
||||
|
||||
- `==` equal to
|
||||
- `~=` not equal to
|
||||
- `<` less than
|
||||
- `>` greater than
|
||||
- `⇐` less than or equal to
|
||||
- `>=` greater than or equal to
|
||||
|
||||
Those are known as relational operators because they prompt an investigation of how two values relate to one another. There are also logical operators, which mean the same as they mean in English and can be incorporated into conditions to further describe the state you want to check for:
|
||||
|
||||
- `and`
|
||||
- `or`
|
||||
|
||||
Here are some example conditions:
|
||||
|
||||
- `foo > 3`: Is the variable `foo` is greater than 3? The `foo` must be 4 or more to satisfy this condition.
|
||||
- `foo >= 3`: Is `foo` greater than or equal to 3? The `foo` must be 3 or more to satisfy this condition.
|
||||
- `foo > 3 and bar < 1`: Is `foo` greater than 3 while `bar` is less than 1? For this condition to be true, the `foo` variable must be 4 or more at the same moment that `bar` is 0.
|
||||
- `foo > 3 or bar < 1`: Is `foo` greater than 3? Alternately, is `bar` less than 1? If `foo` is 4 or more, or `bar` is 0, then this condition is true. What happens if `foo` is 4 or more while `bar` is 0? The answer appears later in this article.
|
||||
|
||||
### While loop
|
||||
|
||||
A while loop executes instructions for as long as _some condition_ is satisfied. For example, suppose you're developing an application to monitor an ongoing zombie apocalypse. When there are no zombies remaining, then there is no more zombie apocalypse:
|
||||
|
||||
```
|
||||
zombie = 1024
|
||||
|
||||
while (zombie > 0) do
|
||||
print(zombie)
|
||||
zombie = zombie-1
|
||||
end
|
||||
|
||||
if zombie == 0 then
|
||||
print("No more zombie apocalypse!")
|
||||
end
|
||||
```
|
||||
|
||||
Run the code to watch the zombies vanish:
|
||||
|
||||
```
|
||||
$ lua ./while.lua
|
||||
1024
|
||||
1023
|
||||
[...]
|
||||
3
|
||||
2
|
||||
1
|
||||
No more zombie apocalypse!
|
||||
```
|
||||
|
||||
### Until loop
|
||||
|
||||
Lua also has a repeat until loop construct that's essentially a while loop with a "catch" statement. Suppose you've taken up gardening and you want to track what's left to harvest:
|
||||
|
||||
```
|
||||
mytable = { "tomato", "lettuce", "brains" }
|
||||
bc = 3
|
||||
|
||||
repeat
|
||||
print(mytable[bc])
|
||||
bc = bc - 1
|
||||
until( bc == 0 )
|
||||
```
|
||||
|
||||
Run the code:
|
||||
|
||||
```
|
||||
$ lua ./until.lua
|
||||
brains
|
||||
lettuce
|
||||
tomato
|
||||
```
|
||||
|
||||
That's helpful!
|
||||
|
||||
### Infinite loops
|
||||
|
||||
An infinite loop has a condition that can never be satisfied, so it runs infinitely. This is often a bug caused by bad logic or an unexpected state in your program. For instance, at the start of this article, I posed a logic puzzle. If a loop is set to run until `foo > 3 or bar < 1`, then what happens when `foo` is 4 or more while `bar` is 0?
|
||||
|
||||
Here's the code to solve this puzzle, with a safety catch using the `break` statement just in case:
|
||||
|
||||
```
|
||||
foo = 9
|
||||
bar = 0
|
||||
|
||||
while ( foo > 3 or bar < 1 ) do
|
||||
print(foo)
|
||||
foo = foo-1
|
||||
|
||||
-- safety catch
|
||||
if foo < -800000 then
|
||||
break
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
You can safely run this code, but it does mimic an accidental infinite loop. The flawed logic is the `or` operator, which permits this loop to continue both when `foo` is greater than 3 and when `bar` is less than 1. The `and` operator has a different effect, but I leave that to you to explore.
|
||||
|
||||
Infinite loops actually do have their uses. Graphical applications use what are technically infinite loops to keep the application window open. There's no way of knowing how long your user intends to use the application, so the program runs infinitely until the user selects **Quit**. The simple condition used in these cases is one that is obviously always satisfied. Here's an example infinite loop, again with a safety catch built in for convenience:
|
||||
|
||||
```
|
||||
n = 0
|
||||
|
||||
while true do
|
||||
print(n)
|
||||
n = n+1
|
||||
|
||||
if n > 100 then
|
||||
break
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
The condition `while true` is always satisfied because `true` is always true. It's the terse way of writing `while 1 == 1` or something similarly eternally true.
|
||||
|
||||
### Loops in Lua
|
||||
|
||||
As you can tell from the sample code, although there are different implementations, loops all basically work toward the same goal. Choose the one that makes sense to you and that works best with the processing you need to perform. And just in case you need it: The keyboard shortcut to terminate a runaway loop is **Ctrl+C**.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/23/2/lua-loops-while-repeat-until
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [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,0 +1,149 @@
|
||||
[#]: subject: "Lua loops: how to use while and repeat until"
|
||||
[#]: via: "https://opensource.com/article/23/2/lua-loops-while-repeat-until"
|
||||
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Lua 循环:如何使用 while 和 repeat until
|
||||
======
|
||||
|
||||
控制结构是编程语言的一个重要特征,因为它们使你能够根据通常在程序运行时动态建立的条件来指导程序的流程。不同的语言提供了不同的控制,在 Lua 中,有 while 循环、for 循环和 repeat until 循环。这篇文章涵盖了 while 和 repeat until 循环。由于它们的灵活性,我在一篇[单独的文章][1]中介绍 for 循环。
|
||||
|
||||
条件是由一个使用运算符的表达式来定义的,运算符是你在数学课上可能认识的符号的一个花哨的术语。Lua 中有效的运算符有:
|
||||
|
||||
- `==` 等于
|
||||
- `~=`不等于
|
||||
- `<` 小于
|
||||
- `>` 大于
|
||||
- `⇐`小于或等于
|
||||
- `>=` 大于或等于
|
||||
|
||||
这些被称为关系运算符,因为它们比较两个值之间的关联。还有一些逻辑运算符,其含义与英语中的含义相同,可以纳入条件中,进一步描述你想检查的状态:
|
||||
|
||||
- `and`
|
||||
- `or`
|
||||
|
||||
下面是一些条件的例子:
|
||||
|
||||
- `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,会怎样?答案出现在本文的后面。
|
||||
|
||||
|
||||
### While 循环
|
||||
|
||||
只要满足某个条件,while 循环就会执行指令。例如,假设你正在开发一个应用来监测正在进行的僵尸末日。当没有剩余的僵尸时,就不再有僵尸末日了:
|
||||
|
||||
```
|
||||
zombie = 1024
|
||||
|
||||
while (zombie > 0) do
|
||||
print(zombie)
|
||||
zombie = zombie-1
|
||||
end
|
||||
|
||||
if zombie == 0 then
|
||||
print("No more zombie apocalypse!")
|
||||
end
|
||||
```
|
||||
|
||||
运行代码,看僵尸消失:
|
||||
|
||||
```
|
||||
$ lua ./while.lua
|
||||
1024
|
||||
1023
|
||||
[...]
|
||||
3
|
||||
2
|
||||
1
|
||||
No more zombie apocalypse!
|
||||
```
|
||||
|
||||
### Until 循环
|
||||
|
||||
Lua 还有一个 repeat until 循环结构,本质上是一个带有 “catch” 语句的 while 循环。假设你在从事园艺工作,你想追踪还剩下什么可以收获的东西:
|
||||
|
||||
```
|
||||
mytable = { "tomato", "lettuce", "brains" }
|
||||
bc = 3
|
||||
|
||||
repeat
|
||||
print(mytable[bc])
|
||||
bc = bc - 1
|
||||
until( bc == 0 )
|
||||
```
|
||||
|
||||
运行代码:
|
||||
|
||||
```
|
||||
$ lua ./until.lua
|
||||
brains
|
||||
lettuce
|
||||
tomato
|
||||
```
|
||||
|
||||
这很有帮助!
|
||||
|
||||
### 无限循环
|
||||
|
||||
一个无限循环有一个永远无法满足的条件,所以它无限地运行。这通常是一个由错误逻辑或你的程序中的意外状态引起的错误。例如,在本文的开头,我提出了一个逻辑难题。如果一个循环被设定为 `foo > 3 or bar < 1` 运行 ,那么当 `foo` 为 4 或更大而 `bar` 为 0 时,会发生什么?
|
||||
|
||||
下面是解决这个问题的代码,为了以防万一,还使用了 `break` 语句安全捕获:
|
||||
|
||||
```
|
||||
foo = 9
|
||||
bar = 0
|
||||
|
||||
while ( foo > 3 or bar < 1 ) do
|
||||
print(foo)
|
||||
foo = foo-1
|
||||
|
||||
-- safety catch
|
||||
if foo < -800000 then
|
||||
break
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
你可以安全地运行这段代码,但它确实模仿了一个意外的无限循环。有缺陷的逻辑是 `or` 运算符,它允许这个循环在 `foo` 大于 3 和 `bar` 小于 1 的情况下继续进行。`and`运算符有不同的效果,但我让你去探索。
|
||||
|
||||
无限循环实际上有其用途。图形应用使用技术上的无限循环来保持应用程序窗口的开放。我们没有办法知道用户打算使用这个程序多久,所以程序无限地运行,直到用户选择**退出**。在这些情况下使用的简单条件显然是一个总是被满足的条件。下面是一个无限循环的例子,为了方便起见,还是内置了一个安全陷阱:
|
||||
|
||||
```
|
||||
n = 0
|
||||
|
||||
while true do
|
||||
print(n)
|
||||
n = n+1
|
||||
|
||||
if n > 100 then
|
||||
break
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
条件 `while true` 总是被满足,因为 `true` 总是为真。这是比写 `while 1 == 1` 或类似的永远为真的简洁方式。
|
||||
|
||||
### Lua 中的循环
|
||||
|
||||
从示例代码中可以看出,尽管有不同的实现方式,但循环基本上都是朝着同一个目标工作。选择一个对你来说有意义的,并且在你需要执行的处理过程中效果最好的。以防万一你需要它:终止失控循环的键盘快捷键是 **Ctrl+C**。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
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)
|
||||
|
||||
本文由 [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
|
Loading…
Reference in New Issue
Block a user