Lesson 18 of 55
7 mins readPython Loop Termination with break
In Plain English
The `break` statement immediately terminates the innermost enclosing `for`, `while`, or `repeat` loop and transfers control to the statement following `end`.
Deep Dive: How It Works
Innermost Loop Exit: `break` only exits the immediate loop in which it appears.
Last Statement Rule: In Lua 5.1, `break` had to be the last statement in a block; in modern Lua, it can appear anywhere.
Searching Pattern: Common idiom for terminating early when a search target is located.
Core Rules to Remember

break Statement: Instantly breaks out of the loop.

Early Exit Pattern: Improves performance by terminating loops once the work is complete.
Live Interactive Example
Hit Run Code to see it liveFinding Target and Breaking
Python 3.12
1
2
3
4
5
6
7
8
9
Output Console
Click "Run Code" to view the rendered output.
How it works: break terminated the loop immediately after finding 42.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Break on Condition
Loop `for i = 1, 10 do`. If `i == 4`, `break`.
Print `"Value: " .. i`.
1
2
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.