Skip to Content
Lua Quick Start Guide
book

Lua Quick Start Guide

by Gabor Szauer
July 2018
Beginner
202 pages
5h 42m
English
Packt Publishing
Content preview from Lua Quick Start Guide

Breaking a loop

A loop might need to exit mid-execution, before the loop condition could evaluate to false. Or, perhaps there is a certain branch of logic that should exit a loop in case of an error. At any point during the loop's execution, the break keyword stops the execution of a loop immediately, as the following code snippet demonstrates:

x = 0while x < 10 do -- Execute 10 times!    print ("x is " .. x)    if x == 5 then        -- This stops the loop execution at 5        break    end    x = x + 1end

This code should loop from 0 to 9 and print out the value of x at each step. However, when x is 5, the loop breaks. Because of this break, this code only outputs 0 through 5.

If you are coming to Lua from another language, you might expect to see a continue statement. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Lua Programming

Beginning Lua Programming

Kurt Jung, Aaron Brown
Vim Masterclass

Vim Masterclass

Jason Cannon

Publisher Resources

ISBN: 9781789343229Supplemental Content