Jump to content




do ... end


  • You cannot reply to this topic
8 replies to this topic

#1 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 17 July 2014 - 04:01 PM

Does do ... end count as a loop in lua? I want to easily exit from many points in a certain block in my script, and want to use break to do so. I know I could set up a single run loop, but I decided I might as well check if this will work.
do
  --#stuff
  if <condition> then
    break
  end
  --#more stuff
  --#repeat
end
--#do stuff when done

As backup, I can easily do
local go = true
while go do
  --#stuff
  if condition then
    break
  end
  --#more stuff
  --#repeat
  go = false
end


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 July 2014 - 04:04 PM

nope. unfortunately do ... end cannot be broken nor returned from. It is simply a way to limit scope. For example:

local foo
do
  local bar = "Hello World!"
  function foo()
    print(bar)
  end
end

foo() --# works
print( bar ) --# will print nothing, there's no variable bar that is in this or any parent scope

using break you'd get an error along the lines of 'not inside loop' and using return will exit whatever code block it is in, whether that's a function or the program.

Edited by theoriginalbit, 17 July 2014 - 04:05 PM.
added example


#3 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 17 July 2014 - 04:05 PM

Dang... Guess I'll go with single use while loops.

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 July 2014 - 04:08 PM

What's your use case? there may be another alternative for you.

btw using a single for loop might be nicer to read; in the event there's no alternative
for i = 1, 1 do
  --# stuff
  if condition then
    break
  end
  --# stuff
end


#5 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 17 July 2014 - 04:15 PM

Inside the ore functions of this script I've been working on
Spoiler


#6 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 17 July 2014 - 04:16 PM

You could also shorten that with a repeat until [condition] loop.
repeat
stuffToDo()
until condition

Edited by Cranium, 17 July 2014 - 04:17 PM.


#7 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 17 July 2014 - 04:19 PM

Lol single use loop:
repeat
stuff()
until false

Thanks for the help.

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 July 2014 - 04:24 PM

You have another problem, due to the fact that you define the local variable 'ok' after you use it in the 'ore' functions

#9 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 17 July 2014 - 08:09 PM

I actually had several more problems. I think I fixed most of them, so I'm testing it now. http://pastebin.com/ZpKSLTgW





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users