Opensource.com Opensource.com -

Lua loops: how to use while and repeat until

Lua loops: how to use while and repeat until sethkenlon Tue, 02/14/2023 - 03:00 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 repeat until 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!") endRun 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 tomatoThat'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 Programming and development Red Hat Developers Blog Programming cheat sheets Try for free: Red Hat Learning Subscription eBook: An introduction to programming with Bash Bash shell scripting cheat sheet eBook: Modernizing Enterprise Java An open source developer's guide to building applications 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 endThe 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. Learn how and when to use while and repeat until loops in Lua. Image by: WOCinTech Chat. Modified by Opensource.com. CC BY-SA 4.0 Programming What to read next This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License. Register or Login to post a comment.

控制结构是编程语言的重要特性,因为它们可以根据程序运行时经常建立的条件来控制程序的流程。Lua提供了while循环、for循环和repeat until循环,本文介绍了while和repeat until循环。Lua中的关系运算符有:等于、不等于、小于、大于、小于等于、大于等于,以及逻辑运算符:与、或。while循环可以在满足某个条件时执行指令,repeat until循环则是一个带有“捕获”语句的while循环。无限循环是指条件无法满足,因此会无限循环,可能是由于程序逻辑错误或意外状态造成的。Lua中的循环都是为了实现同一目标,可以根据需要选择合适的循环。

lua

相关推荐 去reddit讨论