Is there any difference in the usability of repeat
and while
on the moon?
In my opinion, the two seem to have the same purpose.
Not taking into account the syntax, is there any difference between them, however minimal?
Is there any difference in the usability of repeat
and while
on the moon?
In my opinion, the two seem to have the same purpose.
Not taking into account the syntax, is there any difference between them, however minimal?
Basically, repeat
is equivalent to do ... while
of other languages.
In while
, you are giving the condition to enter the loop . If the condition is false, operations performed within the framework will not be performed at any time.
local i = 1
while a[i] do
print(a[i])
i = i + 1
end
In repeat
, you give the condition on output, using until
. At least once, the statements will be executed inside the loop .
repeat
line = os.read()
until line ~= ""
print(line)
Interesting to note this conceptual difference:
while
, the true condition does that you remain within the loop .
In until
the true condition makes that you exit from within the loop .
. until " is " repeat ... until ".
Think as if while was "while" and repeat until was "repeat until". For example
repeat
wait()
num=num+1
until var==true
This loop will repeat until var==true
now
while var==true do
wait()
num=num+1
end
will loop as var==true
, and will do this until you stop the game, other than repeat
, which runs only once.
I hope this has helped;)