break and continue statements are used in loops, while return can be used from either a loop body or function body. (assuming the loop is placed in a function body.)
break will break out of the loop immediately when executed.
continue will halt the current iteration of the loop, and will continue onto the next permitting the loop will not terminate on the next iteration.
return is for use with functions. A return statement will return some given value back to where the function was called from.
Examples of all three:
--//This causes the loop to end immediately after 'hello there' executes.
for i = 1, 5 do
print'hello there'
break
end
--//The continue statement causes the rest of the loop body to stop executing, so the loop moves onto its next iteration.
for i = 1, 10 do
if i % 2 == 0 then
continue
end
print'odd number found yessir'
end
local function getDiscount(age)
if age > 65 then
--//Senior discount is used
return .3
else
return 0 --//No discount is given
end
end
print(getDiscount(69)) --> prints .3 (or 30%)
A return statement can also just stop the function from executing all together, much like a break statement, causing no value to be returned to where it was called from. If return is used in a loop body, then again the same thing occurs and some value (or no value) is returned back to where the function was called from. This also causes the loop to break too obviously.