TranslateProject/sources/tech/20230214.1 ⭐️ Lua loops how to use while and repeat until.md

5.4 KiB

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.

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 选题:lkxed 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出