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()
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/
2 comments
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.
w8 wot