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

Loop Question

Started by bigbaddevil6, 18 July 2014 - 03:50 AM
bigbaddevil6 #1
Posted 18 July 2014 - 05:50 AM
I know that this is somewhat basic but I can not remember what kind of loop it is or how to set it up. The point of the loop would be to do the loop and if a condition is met skip the rest and jump back to the top. Here so sudo code.

so something like

while true do
  if X == true then
	doStep1()
	doStep2()
  end
doStep3()
end

So in this case if "X" became true do step 1 and 2 and go back to the top else do step 3. The point of this is something to do with gps.locate() if It runs into an obstacle it would do step 1 and 2 then go back to the top to check its position else if it didn't run into an obstacle just keep going
johnnic #2
Posted 18 July 2014 - 05:59 AM
If/else?


while true do
if X == true then
  doStep1()
  doStep2()
else
  doStep3()
end
end
bigbaddevil6 #3
Posted 18 July 2014 - 07:30 AM
could of sworn there was a loop that did something like that though like the repeat until setup
theoriginalbit #4
Posted 18 July 2014 - 07:52 AM
In other programming languages there is the keyword continue which allows you to skip from the current instruction back to the loop condition, unfortunately however that is not available in Lua.

The available loop types in Lua are as follows;

while

while condition do
  --# code
  --# will only run when the condition resolves to true, this loop will run 0 to infinite times
end

repeat

repeat
  --# code will run at least once
  --# will run at least once until the condition resolves to true, this loop will run 1 to infinite times
until condition

numeric for

for var = 1, max, step do
  --# code
  --# this loop will run while `var` is less than or equal to `max` incrementing `var` by the value of `step` each time
  --# `step` is optional and when omitted defaults to 1
  --# if `var` starts higher than `max` this loop will not run, otherwise this loop will run a minimum of once until the break condition is met
end

generic for (ipairs)

for index, value in ipairs( someTable ) do
  --# this loop is primarily designed to iterate over tables, though it could have other uses, it will only iterate over values that are stored under a positive numerical index
  --# the general consensus is to not use this, a numerical for loop is more efficient and has less drawbacks than an iterator
end

generic for (pairs)

for key, value in pairs( someTable ) do
  --# this loop is primarily designed to iterate over tables, though it could have other uses, it will only iterate over values that are stored in the table, whether they're numerically indexed or otherwise
  --# note: any key that is not a positive numerical index will not be returned in any order such as alphabetical
end
Edited on 18 July 2014 - 05:52 AM
Bomb Bloke #5
Posted 18 July 2014 - 11:13 AM
In other programming languages there is the keyword continue which allows you to skip from the current instruction back to the loop condition, unfortunately however that is not available in Lua.

I suppose if you specifically wanted that behaviour, you could stick a while loop inside another while loop with the same condition. This'd allow you to use "break" for that purpose.

However, I can't think of an situation where it'd be needed, or even worthwhile.