Reason behind Question: For a while now, I have been using the repeat loop
for a lot of my codes and projects, however, I didn't start thinking Is the 'repeat' loop best to use in situations?
until recently, because, I barely use the while loop
for a project, and as the for the for loop
, I use that only sometimes as well, depending on the project I am working on.
To dumb it down a bit, I am asking, Which loop is best to use?
. If I need to be more in-depth, then I'll try and edit my Answer.
The big difference between the repeat loop and the while loop is that the first code segment is ran differently.
On a while loop, the conditional statement is ran first before running the code while repeat does the complete opposite [runs first, checks later]
In languages such as C++ and Java, there are also what we call do while
loops.
'do while' loops are extremely similar to repeat until because the code is run first before the conditional.
Although I won't explain a 'do while' loop because there are articles which you can find on it, I will give an example of a comparison with code:
local steve = true; while (steve == false) do print("Hey! Did I run? I'm in a while loop!") end repeat print("Hey! Did we beat them? We are in a repeat loop!") until steve == true
The following code stops both loops when their conditionals are met, which should actually be immediately.
Take a guess at what the output met.
Output:
Hey! Did we beat them? We are in a repeat loop!
As you see, although both code segments have met their condition for them to stop, the repeat loop still ran it's code first before checking anything.
Although I am not positively sure which one is faster when repeating, I would assume repeat would be faster in looping forever due to the fact that it runs the first segment right away before checking while while does a loop right away. However, both can easily be used to loop forever.
while true do wait() -- ALWAYS HAVE A WAIT TIME WITH INFINITE LOOPS print("Hey!") end repeat wait() print("Ohai!") until false
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
Your question is a fairly common one but can be answered simply: It depends what you need to do. If you need to run a piece of code a certain amount of times or iterate over a table, use a For loop. If you need to run a piece of code and then check to see if something is true in order to end the loop, us a Repeat loop. A While loop is used when you want to see if something is true in order to begin and continue the loop. I have provided a few examples below:
table1 = {1,2,3,4,5,6,7,8} for i, v in pairs(table1) do print(table1[i]) wait() end
for i = 1, 8 do print(i) wait() end
i = 1 repeat print(i) i = i + 1 wait() until i == 8
i = 1 while (i < 9) do print(i) i = i + 1 wait() end
All of these loops do the same thing(print out the numbers 1-8), but each loop is used in a different way. Each loop is used in different situations and you can use whichever you prefer. There is no 'correct' loop.
I apologize if the repeat loop isn't written correctly, I rarely use them because I don't have the need to.
In addition to the Answers above, it is important to remember that you can break out of any loop at any time using the Break Keyword. This keyword allows you to modify the functionality of any loop, say you needed for some unknown reason use a While loop, but break out of it after some condition has been met a certain number of times:
while true do wait() if Condition1 == Condition2 then break; end end
While you could change a variable, it would continue on through the loop until the end of the loop.
Locked by TheeDeathCaster
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?