• Skip to main content
  • Skip to after header navigation
  • Skip to site footer

Free-Mail

  • Events
  • Images
    • Instagram Goodness
  • Music
  • News
  • Software

Lua loops: how to use while and repeat until

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!") 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.

Woman programming
Image by:

WOCinTech Chat. Modified by Opensource.com. CC BY-SA 4.0

Programming

What to read next
Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.
Register or Login to post a comment.

: Opensource.com sethkenlon

Supporting Open Source.

Have you tried: Sailing in Africa ?

Share this:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Pinterest (Opens in new window) Pinterest
  • Email a link to a friend (Opens in new window) Email
  • More
  • Share on Tumblr (Opens in new window) Tumblr
  • Print (Opens in new window) Print
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit
Published on 14 February 2023 by Alan Category: SoftwareTag: Loops, Repeat, Until

DOVE DEEPENS COMMITMENT TO MUSIC CULTURE WITH THE RETURN OF FAN-FIRST HOT SEATS PLATFORM AND ANNOUNCES OFFICIAL RECORDING ACADEMY® PARTNERSHIP FOR THE 2026 GRAMMY AWARDS®

Dove Whole Body Deo brings all-over confidence to music’s hottest moments with opportunities …

Read moreDOVE DEEPENS COMMITMENT TO MUSIC CULTURE WITH THE RETURN OF FAN-FIRST HOT SEATS PLATFORM AND ANNOUNCES OFFICIAL RECORDING ACADEMY® PARTNERSHIP FOR THE 2026 GRAMMY AWARDS®

Magical Mystery Tour: a rare Beatles flop – but it paved the way for Monty Python

Magical Mystery Tour: a rare Beatles flop – but it paved the way for Monty Python: …

Read moreMagical Mystery Tour: a rare Beatles flop – but it paved the way for Monty Python

How Tutoring Supports Academic Success Across Various Subjects

Tutoring can make a big difference for students by providing personalised guidance in a range of …

Read moreHow Tutoring Supports Academic Success Across Various Subjects

About Alan

Previous Post:Create a modern user interface with the Tkinter Python library
Next Post:Trending Now: Nota

Reach Trust

Reach Trust is a holding trust for a number of companies within the Stratlec Group.

Copyright © 2026 · Free-Mail · All Rights Reserved · Powered by Straton Electrical

Back to top