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

Asked

Viewed 1,551 times

11

Is there any difference as to the usability of repeat and while on the moon?

In my opinion, the two appear to have the same purpose.

Not taking syntax into account, there is some difference between them, even if it is minimal?

2 answers

13


Basically the repeat is equivalent to do ... while of other languages.

In the while, you are giving the condition to enter the loop. If the condition is false, the operations performed within the structure will not be executed once.

local i = 1
while a[i] do
  print(a[i])
  i = i + 1
end

In the repeat, you give condition on exit, using until. At least once, the instructions will be executed within the loop.

repeat
  line = os.read()
until line ~= ""
print(line)

Interesting to observe this conceptual difference:

  • in the while, the condition true causes you to remain within the loop.

  • In the until the condition true causes you to skirt from within the loop.


The Portuguese translation of "while" is "while", and that of "repeat ... until" is "repeat ... until".

  • It’s true. Interesting. Although do be a key word, it is not complementary to the while as in the case of php

0

Think of it as if while was "while" and repeat until it was "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 run the loop while var==true,and will do so until you stop the game, repeat,which is executed only once. I hope that helped ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.