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

Which 'loop' is best to use? [closed]

Asked by 9 years ago

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.

0
Along with DigitalVeer for using While, I believe in many cases, While is the way to go. You can do Infinite loops much easier and also can be flexible to end such loop. Although, it really does matter what you're using the loops for. alphawolvess 1784 — 9y
0
w8 wot TheeDeathCaster 2368 — 7y

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?

3 answers

Log in to vote
3
Answered by 9 years ago

Repeat and While

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.


Looping Forever

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

Do While

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

http://www.cplusplus.com/doc/tutorial/control/

0
So, it is best to use the 'while' loop in a lot of cases over an 'repeat' loop? TheeDeathCaster 2368 — 9y
Ad
Log in to vote
7
Answered by 9 years ago

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.

2
Lol, the 'repeat' loop is written correctly. :) Although, when it comes to numbers like that with a loop, I use the '>=' keyword, because I'm afraid that sometimes it may error in some-way and keep on looping over the number [Which has happened with me before]. :) TheeDeathCaster 2368 — 9y
0
However, the 'for' loop does explain a lot to me, but, I'm having troubling configuring the 'while' and 'repeat' loops; They both do the same function, however, they are programmed sort-of opposite of each-other, so I'm a bit confused right there. TheeDeathCaster 2368 — 9y
0
The while loop only runs the code block if the statement is true and the repeat runs the code block once and then will keep running it as long as the statement is false. FearMeIAmLag 1161 — 9y
Log in to vote
1
Answered by
Abaw7 5
9 years ago

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.