Open-source News

Lua loops: how to use while and repeat until

opensource.com - Tue, 02/14/2023 - 16:00
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!") 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

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 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.

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.

libvpx VP8/VP9 1.13 Released With Yet More AVX2 & Arm Neon Optimizations

Phoronix - Tue, 02/14/2023 - 13:00
Google engineers last week released libvpx 1.13 as the newest feature release to this library that serves as the reference software implementation for the VP8 and VP9 codecs. While Google engineers have already spent years tuning libvpx for maximum performance with Advanced Vector Extensions (AVX), the game is not over and v1.13 has yet more tuning there -- along with continued Arm Neon optimizations too...

Sway & wl-roots Land Support For Wayland Fractional Scaling

Phoronix - Tue, 02/14/2023 - 06:07
Back in November Wayland Protocols 1.31 released and was headlined by a new extension to handle fractional scaling. The latest Wayland compositor adding support for fractional scaling is now the popular i3-inspired Sway compositor as well as the wl-roots library used by it and other compositors...

Microsoft Officially Launches D3D12 GPU Video Acceleration For WSL Linux Use

Phoronix - Tue, 02/14/2023 - 02:00
For over a year I have been writing about how Microsoft has been working on Direct3D video acceleration for Mesa, getting VA-API mapped atop Direct3D 12 video APIs, video engine based effects, and other enablement around Direct3D 12 video support. Microsoft has today officially released the Direct3D 12 GPU video acceleration support now for Windows Subsystem for Linux (WSL) users...

The Current State Of GCC 13's Rust Language Front-End

Phoronix - Tue, 02/14/2023 - 01:30
Arthur Cohen with Embecosm presented at the FOSDEM developer conference earlier this month on gccrs, the ongoing effort to provide a Rust language front-end to the GCC compiler. While the GCC Rust front-end has been merged for the upcoming GCC 13 release, it's not yet in a state that will really be usable to most Rust developers yet as an alternative to Rust's official LLVM-based compiler...

libdisplay-info Marks Its First Release To Reduce EDID/DisplayID Parsing Fragmentation

Phoronix - Tue, 02/14/2023 - 00:30
The first tagged release of libdisplay-info (v0.1) is now available for this new library that aims to reduce code duplication and fragmentation among Wayland compositors and other software making use of monitor EDID and DisplayID parsing...

Mesa's Rusticl Lands Support For SPIR-V Programs

Phoronix - Mon, 02/13/2023 - 21:45
It's been a while since there has been any major additions to Mesa's Rusticl OpenCL implementation led by Red Hat's Karol Herbst while today he merged support for SPIR-V programs to this Rust-written driver. This SPIR-V support is necessary for eventually supporting SYCL and HIP...

Pages