Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Important and useful uses for "repeat loops"?

Asked by 7 years ago

I've been using Lua for quite a few months now, but I've never understood the uses for repeat loops. I've used them before for small things, but never anything that couldn't be replaced with a different function or loop just as easily.

I'm just looking for a few examples of when using a repeat could be useful.

Thanks

2 answers

Log in to vote
4
Answered by 7 years ago
Edited 6 years ago

Condition placement


The repeat loop is essentially an inverted while loop. Yes it may seem weird explained like that, but it's not far off by any means. To understand this, first let's take a look at how the while loop works:

The while loop

The while loop will execute it's body of code only if it's condition is true, and only while it's condition remains true. Knowing this, we could take a look at this while loop example, and know that it won't do anything:

-- Condition is false, so it will not execute the loop.
while false do
    print("Running")
end

You should also notice that the while loop checks its condition before it runs it's code. This is another important difference between repeat and while. So now let's compare the two...

The repeat loop

As mentioned before, repeat is pretty much the opposite of while (though they're still both loops, and they will still both continue to run until their condition tells them otherwise). The only differences are outlined in their condition evaluation, and execution order.

Execution order


The order in which the while loop runs, is as follows: if condition == true then RunCode() Just like repeating an if statement, to check if it's condition is true before running it's code.

However, the order in which the repeat loop runs differs like such: RunCode() if condition == false then break They always run their code first, then check to see if the condition is false to break the loop.

Because of this inverted order, the syntax of the repeat loop differs as well. Instead of needing an "end" statement after the loop is created, it must be followed by "until". This is where the repeat loop's condition is evaluated after it's code has ran. The condition after it, simply tells the loop whether it should keep running or not.

Condition evaluation


Unlike how the while loop's condition must be true in order to keep running, the repeat loop's condition must be false or nil to continue running. Here's an example of an infinite repeat loop:

repeat
    print("Hello world")
until false -- We're starting with a false or nil condition, and repeating the code only until it's true.

The syntax of the loop above, also shows the execution order a bit more in detail. You can see the code is written first, because it's executed first. The condition is written last, because it's evaluated last. Here's an example:

-- This will repeat 'print("Hello world")' until it's condition is true
-- However even though we have "until true", the code runs before the condition is evaluated.
repeat
    print("Hello world")
until true

Creating a while loop with repeat?

So now knowing how repeat loops work, you could even imitate a while loop by using a repeat loop. Note back to where I said "Much like repeating an if statement, to check if it's condition is true before running it's code.". Which is exactly how we'd go about doing this. Simply have an if statement to check the condition before hand, and break the loop if it's evaluated as false or nil.

-- Condition
local Condition = false

-- Exactly the same as a while loop
repeat
    if Condition then -- If the condition is true, the execute it's code
        print("Hello world")
    else -- But if it's not, then...
        break -- Stop the loop immediately.
    end
until false -- This part wouldn't even be acknowledged in this example, but if it was, it would run forever until the loop was broken.

So, hope this helped. If you have any questions, just let me know and I'll get back to you as soon as possible.

0
Thank you. Honestly, thank you very much. User#11440 120 — 7y
0
Lovely. OldPalHappy 1477 — 7y
Ad
Log in to vote
-1
Answered by 7 years ago

They don't really do anything special. They are just a slightly shorter version of a while loop.


Here are some examples:

Asset Loading

repeat wait() until game.ContentProvider.RequestQueueSize == 0 -- if the game's assets are loaded

while game.ContentProvider.RequestQueueSize > 0 and wait() do -- same thing as above
end

Sound Playing

locals sounds = soundFolder:GetChildren() -- an array with sound objects
local curSound = 0

function playSound()
    curSound = curSound + 1
    local s = sounds[curSound]
    s:Play()
    repeat wait() until s.TimePosition >= s.TimeLength -- probably won't work because of server issues, but this is the basic theory of how it should work (waits until the sound is finished, basically)
    s:Stop()
end

repeat playSound() until curSound == #sounds -- repeats the function until the songs have been finished
print("Queue concluded")

Hope I helped!

~TDP

Answer this question