The 'break' Statement
Part of the Fundamentals section of Coddy's Lua journey — lesson 75 of 90.
Sometimes you need to exit a loop before it naturally finishes. The break statement gives you this power - it immediately stops the current loop and jumps to the code that comes after the loop's end.
Here's how break works in a simple example:
for i = 1, 10 do
if i == 5 then
break
end
print(i)
end
print("Loop finished!")This loop would normally print numbers 1 through 10, but the break statement stops it when i equals 5. So it only prints 1, 2, 3, 4, then jumps straight to “Loop finished!”
Challenge
EasyCreate a treasure hunting system that uses a for loop with a break statement to search through a series of treasure chests. Start by creating a variable named targetTreasure and assign it the value 7.
Write a for loop that searches through treasure chests numbered from 1 to 10. Inside the loop, print the message "Searching chest: " followed by the current chest number. Then check if the current chest number equals the targetTreasure value. If it matches, print "Treasure found!" and use the break statement to exit the loop immediately.
After the loop ends (either naturally or due to the break), print "Search complete" to indicate the treasure hunting session has finished.
Cheat sheet
The break statement immediately stops the current loop and jumps to the code after the loop's end:
for i = 1, 10 do
if i == 5 then
break
end
print(i)
end
print("Loop finished!")This prints 1, 2, 3, 4, then jumps to "Loop finished!" when i equals 5.
Try it yourself
-- Set the target treasure
local targetTreasure = 7
-- TODO: Write your code here
-- Create a for loop that searches through chests 1 to 10
-- Print "Searching chest: " followed by the chest number
-- Check if the chest number equals targetTreasure
-- If found, print "Treasure found!" and break
-- After the loop, print "Search complete"This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator12Loops Basics
The while LoopThe repeat-until LoopThe Numeric for Loopfor Loop with a StepThe 'break' StatementRecap - Countdown