What is the difference between repeat and while on the moon?

11

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?

    
asked by anonymous 18.12.2015 / 14:48

2 answers

13

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 ".     

18.12.2015 / 14:53
0

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;)

    
01.01.2017 / 06:48