As you may be aware, a [b]while[/b] loop has the structure:
[code]
while < some condition is true > do
–various statements
end
[/code]
The logic of a while loop can be stated as:[list=1]
[*]Test if a condition is logically [b]true[/b]. If it is:
[list]
[*][b]do[/b] various statements until you encounter the end of this block (the [b]end[/b] statement of the [b]while … do[/b]).
[*]Jump back up to the beginning of this block (the [b]while[/b] statement)
[/list]
[*]If the condition is not logically [b]true[/b], jump to the end of the [b]while[/b] block and continue with the rest of the program.
[/list]
[color=#ff0000][i]A [b]while[/b] statement will not execute if the condition evaluates as false at the beginning of the loop.[/i][/color]
There are two basic approaches to exiting a while loop. – an exit condition in the while statement itself, or a break condition within the while loop's execution block. Let's try this out using key press events. We will exit if the user presses the "q" key.
Method One: Using an exitCondition within the while statement
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")
Method Two: Using an break statement inside of the while loop
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 NOT
In 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.
Using Return to Exit a While Loop
If you are within a function (or program) and thinking of exiting a loop in the function (or program), 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.
Additional tutorials that may be helpful if you had this question:
- For more on using break, see Lua Basics: Using break to Exit Loops
- For a good walkthrough of Lua basics, see Lua Basics -- Variable Scope, Code Blocks and Control Structures