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

Probably just a basic question

Started by NMetallic123, 16 February 2014 - 08:02 PM
NMetallic123 #1
Posted 16 February 2014 - 09:02 PM
This is probably something simple that I am overlooking, but my question is is there a way for when a user inputs in a certain thing, it goes back up to the top and re executes the program? I know that I will need an if loop but I don't know how to do it.
Tjakka5 #2
Posted 17 February 2014 - 02:37 AM

local flag = false
while not flag do
  input = read()
  if input ~= "Ponies" then
    flag = true
  end
end
--Other code

I believe that should work.
Jim #3
Posted 17 February 2014 - 07:40 AM
Or put the code you wanna loop into a function and setup a while loop that executes that function, based on the user input (Tjakka5 posted how to do this above).
Edited on 17 February 2014 - 06:41 AM
surferpup #4
Posted 17 February 2014 - 01:18 PM
There are two basic approaches – an exit condition in the while statement itself, or a break condition within the while loop's execution block. [member='Tjakka5'] gave you an exit condition within the while statement. It relied on a variable external to the scope of the while loop. Let's try this out using key press events. We will exit if the user presses the "q" key.


local exitConditionVariable = false
while not exitConditionVariable do
  print ("Waiting for a key press...")
  local _,key = os.pullEvent("key")
  if key == keys.q then
	exitConditionVariable = true
  end
  print ("Hello from inside of the while loop")
end
print ("Done")

The second way is to break out of a while loop when a condition occurs. This does not rely on variables external to the while loop. The break statement will move the program execution counter to one statement past the end statement of the while loop.


while true do -- do this forever
  print ("Waiting for a key press...")
  local _,key = os.pullEvent("key")
  if key == keys.q then
	break -- will exit the while loop
  end
  print ("Hello from inside of the while loop")
end
print ("Done")

A break statement is immediate. If you try these two versions out, you will find that the print statement print ("Hello from inside of the while loop") will execute one more time in the first example because the while loop will continue to finish execution of all the statements in the while loop before the while condition is tested again. In the break example, when the condition is satisfied, the while loop terminates immediately before printing "Hello from inside of the while loop".

Both are legitimate programming techniques which are used depending on what you as the programmer wish to accomplish.

A Note on the use of NOTIn the first example, we wrote the exit condition as


while not exitConditionVariable do

Why didn't we just write while exitConditionVariable == false do ??

A variable can be logically false if it is nil or if it is assigned the value of false. However, a variable will equate (==) to false only if it is assigned the value of false. We cover the largest number of possibilities with the first example – not exitConditionVariable will evaluate as true if exitConditionVariable is nil or if it holds a value of false. Because of this, we did not need to declare the variable and set its value before the while loop – although it is better practice to do so.
Edited on 17 February 2014 - 12:41 PM