This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
adencraft2000's profile picture

I need help with exiting a "while true do" loop

Started by adencraft2000, 02 August 2013 - 09:48 AM
adencraft2000 #1
Posted 02 August 2013 - 11:48 AM
Hi,
I have a system that essentially is this:

while true do
print("Example code")
wait(2)
exit

I want it so I can type in "stop" and it will exit that loop.
That or I press any key to exit the loop.

I am new to lua and Computercraft so help is much appreciated.
Bubba #2
Posted 02 August 2013 - 12:00 PM
Hello adencraft,

There are a few ways that you can exit from a loop. The first one is to have the loop check a condition every time it runs.
Example:

local shouldStop = false
while not shouldStop do
  print("Not stopping yet!")
  local event, keyCode = os.pullEvent("key")
  if keyCode == keys.space then
    shouldStop = true
  end
end
In the above example, the program will continue writing "Not stopping yet!" until you hit the correct key (in this case the spacebar).

Another way to exit a loop is to use the break statement.
Example:

local sleepTime = 0
while true do
  print("Not stopping yet!")
  sleep(1)
  sleepTime = sleepTime + 1
  if sleepTime > 5 then
    break
  end
end

If you want to get more familiar with how loops, I would suggest checking out the Programming in Lua online manual. It's very handy and every Lua programmer should read it to get the basics of Lua.
adencraft2000 #3
Posted 02 August 2013 - 12:07 PM
Thanks
adencraft2000 #4
Posted 02 August 2013 - 12:39 PM
Off topic:
This is being printed to my MINECRAFT console log NOT the computer

2013-08-03 02:38:01 [INFO] [STDOUT] ComputerCraft: Error running task.
2013-08-03 02:38:01 [INFO] [STDERR] java.lang.NoClassDefFoundError: dan200/computer/core/IMountedFileNormal
2013-08-03 02:38:01 [INFO] [STDERR]	 at dan200.computer.core.FileSystem.<init>(FileSystem.java:25)
2013-08-03 02:38:01 [INFO] [STDERR]	 at dan200.computer.core.Computer.initFileSystem(Computer.java:626)
2013-08-03 02:38:01 [INFO] [STDERR]	 at dan200.computer.core.Computer.access$1000(Computer.java:33)
2013-08-03 02:38:01 [INFO] [STDERR]	 at dan200.computer.core.Computer$1.execute(Computer.java:865)
2013-08-03 02:38:01 [INFO] [STDERR]	 at dan200.computer.core.ComputerThread$1$1.run(ComputerThread.java:117)
2013-08-03 02:38:01 [INFO] [STDERR]	 at java.lang.Thread.run(Thread.java:680)
2013-08-03 02:38:01 [INFO] [STDERR] Caused by: java.lang.ClassNotFoundException: dan200.computer.core.IMountedFileNormal
2013-08-03 02:38:01 [INFO] [STDERR]	 at cpw.mods.fml.relauncher.RelaunchClassLoader.findClass(RelaunchClassLoader.java:139)
2013-08-03 02:38:01 [INFO] [STDERR]	 at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
2013-08-03 02:38:01 [INFO] [STDERR]	 at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
2013-08-03 02:38:01 [INFO] [STDERR]	 ... 6 more

Any idea what the problem is (probably more for a java dev but idk.)


EDIT:
Fixed
Edited on 02 August 2013 - 11:16 AM
Geforce Fan #5
Posted 02 August 2013 - 12:56 PM
It would be best you make another topic for the above. Idk where it would go, just look around the forums.
adencraft2000 #6
Posted 02 August 2013 - 01:15 PM
Okay so I used example 1 and it would not loop, Code can be found here: http://pastebin.com/8CAkZYG9
Bubba #7
Posted 02 August 2013 - 02:05 PM
Okay so I used example 1 and it would not loop, Code can be found here: http://pastebin.com/8CAkZYG9

How exactly do you want this to behave? The code is valid and should have the turtle do the thing endlessly - however, you have to hit a key every time you want the turtle to do something. If you want to the turtle to just continuously do something until you hit a key, you will need to use the parallel API or coroutines. These are somewhat more advanced topics, but if you'd like I can go into more detail.

Another alternative is to have the turtle just do something a certain number of times. For this we would use a for loop.
Example:

for i=1,5 do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
end
adencraft2000 #8
Posted 02 August 2013 - 04:03 PM
Okay so I used example 1 and it would not loop, Code can be found here: http://pastebin.com/8CAkZYG9

How exactly do you want this to behave? The code is valid and should have the turtle do the thing endlessly - however, you have to hit a key every time you want the turtle to do something. If you want to the turtle to just continuously do something until you hit a key, you will need to use the parallel API or coroutines. These are somewhat more advanced topics, but if you'd like I can go into more detail.

Another alternative is to have the turtle just do something a certain number of times. For this we would use a for loop.
Example:

for i=1,5 do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
end

So would this work?

for i=1,5 do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
end
local event, keyCode = os.pullEvent("key")
if keyCode == keys.space then
  print("Stopping")
  sleep(2)
  exit()
end


EDIT:
So could you make the loop be entered like

mine 3
and it would mine 3 in that direction?

Could you please go in to detail with the parallel API for me :)/>
adencraft2000 #9
Posted 02 August 2013 - 05:07 PM
I got the turtle working another way:
http://pastebin.com/xwS2x1ab

Thanks for the help
adencraft2000 #10
Posted 03 August 2013 - 01:03 AM
Okay so I used example 1 and it would not loop, Code can be found here: http://pastebin.com/8CAkZYG9

How exactly do you want this to behave? The code is valid and should have the turtle do the thing endlessly - however, you have to hit a key every time you want the turtle to do something. If you want to the turtle to just continuously do something until you hit a key, you will need to use the parallel API or coroutines. These are somewhat more advanced topics, but if you'd like I can go into more detail.

Another alternative is to have the turtle just do something a certain number of times. For this we would use a for loop.
Example:

for i=1,5 do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
end

So… Could i put a "for loop" inside for loop?
HurricaneCoder #11
Posted 03 August 2013 - 02:49 AM
Okay so I used example 1 and it would not loop, Code can be found here: http://pastebin.com/8CAkZYG9

How exactly do you want this to behave? The code is valid and should have the turtle do the thing endlessly - however, you have to hit a key every time you want the turtle to do something. If you want to the turtle to just continuously do something until you hit a key, you will need to use the parallel API or coroutines. These are somewhat more advanced topics, but if you'd like I can go into more detail.

Another alternative is to have the turtle just do something a certain number of times. For this we would use a for loop.
Example:

for i=1,5 do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
end

So… Could i put a "for loop" inside for loop?

yes you can put a for loop in side a for loop.