{"id":71203,"date":"2023-02-14T09:01:55","date_gmt":"2023-02-14T09:01:55","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=71203"},"modified":"2023-02-14T09:01:55","modified_gmt":"2023-02-14T09:01:55","slug":"lua-loops-how-to-use-while-and-repeat-until","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=71203","title":{"rendered":"Lua loops: how to use while and repeat until"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">Lua loops: how to use while and repeat until<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/seth\" class=\"username\">sethkenlon<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Tue, 02\/14\/2023 &#8211; 03:00<\/span><\/p>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p>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&#8217;s the while loop, for loop, and repeat<em> <\/em>until loop. This article covers the while and repeat until loops. Because of their flexibility, I cover for loops in a <a href=\"https:\/\/opensource.com\/article\/22\/11\/lua-for-loops\" target=\"_blank\" rel=\"noopener\">separate article<\/a>.<\/p>\n<p>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:<\/p>\n<ul>\n<li>\n<p><code>==<\/code> equal to<\/p>\n<\/li>\n<li>\n<p><code>~=<\/code> not equal to<\/p>\n<\/li>\n<li>\n<p><code> less than<\/code><\/p>\n<\/li>\n<li>\n<p><code>&gt;<\/code> greater than<\/p>\n<\/li>\n<li>\n<p><code>\u21d0<\/code> less than or equal to<\/p>\n<\/li>\n<li>\n<p><code>&gt;=<\/code> greater than or equal to<\/p>\n<\/li>\n<\/ul>\n<p>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:<\/p>\n<ul>\n<li>\n<p><code>and<\/code><\/p>\n<\/li>\n<li>\n<p><code>or<\/code><\/p>\n<\/li>\n<\/ul>\n<p>Here are some example conditions:<\/p>\n<ul>\n<li>\n<p><code>foo &gt; 3<\/code>: Is the variable <code>foo<\/code> is greater than 3? The <code>foo<\/code> must be 4 or more to satisfy this condition.<\/p>\n<\/li>\n<li>\n<p><code>foo &gt;= 3<\/code>: Is <code>foo<\/code> greater than or equal to 3? The <code>foo<\/code> must be 3 or more to satisfy this condition.<\/p>\n<\/li>\n<li>\n<p><code>foo &gt; 3 and bar : Is <code>foo<\/code> greater than 3 while <code>bar<\/code> is less than 1? For this condition to be true, the <code>foo<\/code> variable must be 4 or more at the same moment that <code>bar<\/code> is 0.<\/code><\/p>\n<\/li>\n<li>\n<p><code>foo &gt; 3 or bar : Is <code>foo<\/code> greater than 3? Alternately, is <code>bar<\/code> less than 1? If <code>foo<\/code> is 4 or more, or <code>bar<\/code> is 0, then this condition is true. What happens if <code>foo<\/code> is 4 or more while <code>bar<\/code> is 0? The answer appears later in this article.<\/code><\/p>\n<\/li>\n<\/ul>\n<h2>While loop<\/h2>\n<p>A while loop executes instructions for as long as <em>some condition<\/em> is satisfied. For example, suppose you&#8217;re developing an application to monitor an ongoing zombie apocalypse. When there are no zombies remaining, then there is no more zombie apocalypse:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-c\" data-lang=\"bash\">zombie = 1024\n\nwhile (zombie &gt; 0) do\n  print(zombie)\n  zombie = zombie-1\nend\n\nif zombie == 0 then\n  print(\"No more zombie apocalypse!\")\nend<\/code><\/pre>\n<p>Run the code to watch the zombies vanish:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-c\" data-lang=\"bash\">$ lua .\/while.lua\n1024\n1023\n[...]\n3\n2\n1\nNo more zombie apocalypse!<\/code><\/pre>\n<h2>Until loop<\/h2>\n<p>Lua also has a repeat until loop construct that&#8217;s essentially a while loop with a &#8220;catch&#8221; statement. Suppose you&#8217;ve taken up gardening and you want to track what&#8217;s left to harvest:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-c\">mytable = { \"tomato\", \"lettuce\", \"brains\" }\nbc = 3\n\nrepeat\n   print(mytable[bc])\n   bc = bc - 1\nuntil( bc == 0 )<\/code><\/pre>\n<p>Run the code:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-c\">$ lua .\/until.lua\nbrains\nlettuce\ntomato<\/code><\/pre>\n<p>That&#8217;s helpful!<\/p>\n<h2>Infinite loops<\/h2>\n<p>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 <code>foo &gt; 3 or bar , then what happens when <code>foo<\/code> is 4 or more while <code>bar<\/code> is 0?<\/code><\/p>\n<p>Here&#8217;s the code to solve this puzzle, with a safety catch using the <code>break<\/code> statement just in case:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-c\">foo = 9\nbar = 0\n\nwhile ( foo &gt; 3 or bar <\/code><\/pre>\n<\/p>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">Programming and development<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/?intcmp=7016000000127cYAAQ\">Red Hat Developers Blog<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/cheat-sheets?intcmp=7016000000127cYAAQ\">Programming cheat sheets<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/services\/training\/learning-subscription?intcmp=7016000000127cYAAQ\">Try for free: Red Hat Learning Subscription<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/bash-programming-guide?intcmp=7016000000127cYAAQ\">eBook: An introduction to programming with Bash<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/cheat-sheets\/bash-shell-cheat-sheet?intcmp=7016000000127cYAAQ\">Bash shell scripting cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/e-books\/modernizing-enterprise-java?intcmp=7016000000127cYAAQ\">eBook: Modernizing Enterprise Java<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/building-applications?intcmp=7016000000127cYAAQ\">An open source developer&#8217;s guide to building applications<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>You can safely run this code, but it does mimic an accidental infinite loop. The flawed logic is the <code>or<\/code> operator, which permits this loop to continue both when <code>foo<\/code> is greater than 3 and when <code>bar<\/code> is less than 1. The <code>and<\/code> operator has a different effect, but I leave that to you to explore.<\/p>\n<p>Infinite loops actually do have their uses. Graphical applications use what are technically infinite loops to keep the application window open. There&#8217;s no way of knowing how long your user intends to use the application, so the program runs infinitely until the user selects <strong>Quit<\/strong>. The simple condition used in these cases is one that is obviously always satisfied. Here&#8217;s an example infinite loop, again with a safety catch built in for convenience:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-c\">n = 0\n\nwhile true do\n  print(n)\n  n = n+1\n\n  if n &gt; 100 then\n  break\n  end\nend<\/code><\/pre>\n<p>The condition <code>while true<\/code> is always satisfied because <code>true<\/code> is always true. It&#8217;s the terse way of writing <code>while 1 == 1<\/code> or something similarly eternally true.<\/p>\n<h2>Loops in Lua<\/h2>\n<p>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 <strong>Ctrl+C<\/strong>.<\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>Learn how and when to use while and repeat until loops in Lua.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2023\/02\/programming-code-keyboard-laptop-music-headphones.png\" width=\"520\" height=\"292\" alt=\"Woman programming\" title=\"Woman programming\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>WOCinTech Chat. Modified by Opensource.com. CC BY-SA 4.0<\/p>\n<\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-tags field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/programming\" hreflang=\"en\">Programming<\/a><\/div>\n<\/p><\/div>\n<div class=\"hidden field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2023\/02\/cc-by-sa--20.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?destination=\/feed&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lua loops: how to use while and repeat until sethkenlon Tue, 02\/14\/2023 &#8211; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":71204,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-71203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/71203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=71203"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/71203\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/71204"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=71203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=71203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=71203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}