What is the difference between repeat and while in lua?
Is there any difference as to the usability of repeat
and while
in Lua?
In my opnion, the two seem to have the same purpose.
Not taking into account the syntax, is there any difference between them, even if it is minimal?
2 answers
Basically repeat
equals do ... while
of other languages.
No 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 all.
local i = 1
while a[i] do
print(a[i])
i = i + 1
end
No repeat
, you give the condition on the output, using until
. At least once, the instructions inside the loop will be executed.
repeat
line = os.read()
until line ~= ""
print(line)
Interesting to observe this difference conceptual:
No
while
, the true condition causes you to remain inside the loop.No
until
the true condition causes you to Exit from inside the loop.
the Portuguese translation of "while" is "while", and that of "repeat ... until " is " repeat ... up that " .
Think as if while IS " while "and repeat until is"repeat until". For example
repeat
wait()
num=num+1
until var==true
This loop will repeat until var==true
Already
while var==true do
wait()
num=num+1
end
Will loop while var==true
,and will do so until you stop the game,unlike repeat
,which only runs once.
I hope this helped ;)