Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can someone answer this question about while loops please?

Asked by 9 years ago
while true do
    --code
end

While what is true? What does it need to be true in order to do?

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Answers explaining while true do:


To specifically answer your question - while what is true?


The form of a while loop is

while (condition) do

Where condition is some expression that is checked to pass or fail ( just like an if statement).

A simple value which passes as the condition is true.

true is a constant (just like 5 is a constant or "cat" is a constant) which passes conditions of control structures and the boolean operators.

When the condition is true, then, since it is (a) constant it unconditionally passes -- it will always pass since the condition is always true (true).

Ad
Log in to vote
-1
Answered by 9 years ago

while true do is an infinite loop, as true is always true. This means it will infinitely repeat the code inside it. Using it without a wait() will cause a crash. The way to break out of it is to use

break

break will break out of any loop. Even if it is under an if statement's scope.

Answer this question