# Need help : Endless loop

I need to do a loop in the setup and it needs to be endless. Never mind why… I just need it… I need to put a loop around this code:

``````level= 1

function setup()

if(level < 5 ) then
level = level + 1
else

if (level > 10 ) then
level = level + 1

else
if (level > 15) then
level = level + 1
end
end
end
``````

Since this is going to be in the setup, I need to loop it so it can do the level + 1 part, because at the beginning of the program level is assigned to 1. Since I need level + 1 to happen, I need to loop this endlessly. This is just me testing something out for my actual game.

@JessicGriffin If this is going in setup and it’s endless, you’ll never exit setup(). So here it is.
The only way out of this is to kill Codea.

``````
function setup()
level=0
while true do  -- endless loop
if(level < 5 ) then
level = level + 1
elseif (level > 10 ) then
level = level + 1
elseif (level > 15) then
level = level + 1
end
end
end

``````

@dave1707 Thanks Dave, that was really helpful… Except my ipad crashed and is frozen beyond belief from so many calculations or whatever going on. Is there a way to limit the number of calculations per second? Is there a way to say do this once every second, instead of it trying to get like a million random numbers ? A kid in my class told me to make a variable called start time, and then I’ll just write if start time = whatever then do whatever… But I don’t know if that’s the most efficient way to do it.

You should execute the code in the `draw()` function. Then if you want it to execute every second, put it in the following if-statement:

``````t = t + 1
if t=>60 then
-- your code without the while part
t = 0
end
``````

And ofcourse, you need to initialize this in the `setup`:

``````t = 0
level = 0
``````

However, this timer is based on the frame rate. You might want to use a system time based timer instead.

Okay. I copied my code out to that. But my answer has nil value x.x Why? I put my code in draw, but answer has nil value. Sighhhhhhh. Why?

``````function draw()
t=t+1
background(206, 33, 42, 255)
fontSize(50)
fill(216, 11, 11, 255)
font("Arial-BoldMT")
if (t>60)then
if (level < 5) then
level= level + 1
else
if (level > 10) then
level = level + 1
else
if (level > 15) then
level= level + 1
end
end
end
end
end

``````

Ignore the font stuff, I was going to write it on the screen but I decided to just print it in the outbox instead

NEVERMIND. I figured it out. I put print in the setup when it didn’t have a value. Sigh this is so confusing. Now I’ve put it in draw, like this:

``````function draw()
t=t+1
background(206, 33, 42, 255)
fontSize(50)
fill(216, 11, 11, 255)
font("Arial-BoldMT")
if (t>60)then
if (level < 5) then
level= level + 1
else
if (level > 10) then
level = level + 1
else
if (level > 15) then
level= level + 1
end
end
end
end
end
``````

And it started printing different numbers originally, then it just kept on printing the same answer. 7. It went 7,8,9,3,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7… etc. WHY.

It also just waits one second then does all the math at once. How do I tell it to wait a second, and then do it, and then wait, and then do it?

I tried the program again, and it keeps on selecting four random numbers then selecting one random number and printing it over and over and over again. Why?

I could tell the computer to count to a number, like tell it to count to 60. So it would wait a second. But I don’t know the syntax for that. What is it?

NEVERMIND My teacher helped me ill post the code in a second.

``````--# Main
-- levelApp
-- Use this function to perform your initial setup

level = 0
t=0

function setup()

supportedOrientations(LANDSCAPE_ANY)
--displayMode(FULLSCREEN)

end

-- This function gets called once every frame
function draw()
t=t+1
background(206, 33, 42, 255)
fontSize(50)
fill(216, 11, 11, 255)
font("Arial-BoldMT")
if (math.fmod (t,60)== 0) then
if (level >  15) then
level= level + 1
else if (level > 10) then