Jump to content




Lua Basics -- Using "break" to Exit Loops

lua

6 replies to this topic

#1 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 20 January 2014 - 12:19 AM

Tutorial: Lua Basics -- Using "break" to Exit Loops

This tutorial is offered as a topic in the Ask A Pro Renewal Project tutorials requested by Lyqyd.

Pre-Requisites: Before reading this tutorial, you should be familiar with all of the control structure concepts covered in Lua Basics -- Variable Scope, Code Blocks and Control Structures

There are three basic looping control structures in Lua: the while loop, the repeat loop and the for loop. Each loop has exit conditions built into the conditions of the control structure. However, it is sometimes useful to exit the loop early, should a special condition arise. That is where the break keyword comes in handy. The break keyword only works inside of the code block of a looping control structure. It will not work with a stand-alone if control structure (that is an if control structure that is not already inside of a loop). Essentially, when Lua encounters a break keyword from within a loop, execution of the loop ceases immediately, and program execution jumps to the next statement following the ending keyword of the loop.

In the case of for and while loops, break will cause program execution to jump to the statement immediately following the end keyword. In the case of repeat loops, break will cause program execution to jump to the statement immediately following the until keyword and its associated condition.

Examples

A Better User Menu

Other Thoughts

If you are within a function and thinking of exiting a loop in the function, the return keyword will immediately end execution of a loop much like a break will with the added bonus of returning from the function. This may be useful if your purpose is to immediately exit the loop and exit from the function. However, if your function returns values, you will need to handle that within the return statement. If you choose to use return for the purpose of ending loops, make sure that you truly intended to exit the function as well.

Furthermore, it is good to remember that whether you use break or you return, local variables fall out of scope when exiting the code block with which they are associated.

Editing Credits

Edited by surferpup, 21 January 2014 - 02:02 PM.


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2014 - 12:27 AM

 surferpup, on 20 January 2014 - 12:19 AM, said:

From within functions (and only from within functions), the return keyword will immediately end execution of a loop much like a break will with the added bonus of returning from the function.
Incorrect return works on the main program body as well, run the following code on a computer
if not turtle then
  return print("I'm not a Turtle")
end
print("I'm only printed on a Turtle")

It should also be noted that if a return is used in a do block it will also exit the program
do
  print("I will print")
  return
end
print("I will never print")

Edited by theoriginalbit, 20 January 2014 - 12:27 AM.


#3 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 20 January 2014 - 12:35 AM

You are correct. I was attempting to make the point that return will exit from a loop as well, with the benefit of not only ending the loop but exiting the function.

Changed to"From within functions (and only from within functions)If you are within a function and thinking of exiting a loop in the function…"

I am trying to put together a few Lua basics tutorials for new programmers and those new to Lua. As one who recently took up Lua, these topics would have been helpful for me. Thank you for the correction.

Edited by surferpup, 20 January 2014 - 12:39 AM.


#4 Symmetryc

  • Members
  • 434 posts

Posted 20 January 2014 - 10:23 AM

 theoriginalbit, on 20 January 2014 - 12:27 AM, said:

 surferpup, on 20 January 2014 - 12:19 AM, said:

From within functions (and only from within functions), the return keyword will immediately end execution of a loop much like a break will with the added bonus of returning from the function.
Incorrect return works on the main program body as well, run the following code on a computer
if not turtle then
  return print("I'm not a Turtle")
end
print("I'm only printed on a Turtle")

It should also be noted that if a return is used in a do block it will also exit the program
do
  print("I will print")
  return
end
print("I will never print")
This works because the main program is essentially a function. You can pass arguments to it and you will get returns much like a normal function. This behavior can be seen when using the loadfile function.

Edit: Furthermore, like a normal function, local variables fall out of scope at the end of its use and arguments are accessed using "...".

Edited by Symmetryc, 20 January 2014 - 10:25 AM.


#5 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 21 January 2014 - 01:53 PM

I'm not sure what to do with your suggested edit. I am not discussing function arguments in this tutorial … at least I don't think I am. The return discussion is more of an aside.

I added: Furthermore, it is good to remember that whether you use break or you return, local variables fall out of scope when exiting the code block with which they are associated.

#6 Symmetryc

  • Members
  • 434 posts

Posted 23 January 2014 - 04:05 PM

Sorry, when I wrote "Edit:", I meant that I added onto my original post (I edited it), not to edit your post :P

#7 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 24 January 2014 - 12:46 AM

That's okay, I still used one of the nuances you mentioned. :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users