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

What does the while do statement do?

Asked by 10 years ago

Example:

Hi = true
while Hi do
print("Everything is awesome!")
end

2 answers

Log in to vote
1
Answered by 10 years ago

A while (condition) do loop executes the code it a continuous loop as long as condition = true.

Hi = true

while Hi do
    print("Everything is awesome!")
    wait()
end

This can be substituted with more concise code.

while wait() do
    print("Everything is awesome!")
end

If you see while true do, that intends to be executed forever, and if there is no wait, could crash a computer. Better practice for an endless while loop is while wait() do. That accomplishes the same thing without crashing a computer as well as being more concise and readable. It should also be noted that if a script hits a while true do loop that's outside of a coroutine it will not execute any code below it. For example,

while wait() do
    print("Everything is awesome!")
end

while wait() do
    print('Coding is awesome.')
end

--[[
endless output: Everything is awesome!
'Coding is awesome' is never printed
--]]

It should also be noted that anything that doesn't explicitly results in false returns true. For instance,

Hi = 2+2

while Hi do
    print(Hi)
    wait()
end

--endless output: 4
Hi = 'Everything is awesome!'

while Hi do
    print(Hi)
    wait()
end

--endless output: Everything is awesome!

As opposed to,

Hi = 4 < 3

while Hi do
    print(Hi)
    wait()
end

--no output
Ad
Log in to vote
1
Answered by 10 years ago

it endlessly repeats

print('Everything is awesome!')

to stop this you want to do

Hi = true
while Hi do
print("Everything is awesome!")
wait(1)
end
0
This is good but edit this and explan more than the other one and i'll accept yours! iluvmaths1123 198 — 10y

Answer this question