The while Loop
Part of the Fundamentals section of Coddy's Lua journey — lesson 71 of 90.
The while loop is perfect for this - it keeps running the same code over and over as long as a condition remains true.
The basic syntax of a while loop looks like this:
while condition do
-- code to repeat
endHere's a simple example that prints numbers from 1 to 3:
local count = 1
while count <= 3 do
print(count)
count = count + 1
endThis loop works by first checking if count <= 3 is true. If it is, the code inside runs, printing the current number and increasing the counter. Then it checks the condition again, and the process repeats until the condition becomes false.
Challenge
EasyCreate a health monitoring system that uses a while loop to track a player's recovery process. Start by creating a variable named playerHealth and assign it the value 25. Then create another variable named maxHealth and assign it the value 100.
Write a while loop that continues as long as the player's health is less than their maximum health. Inside the loop, first print the current health value, then increase playerHealth by 15. Because the print happens before the increment, the last value printed will be 85 — the loop condition is checked again after adding 15 (making playerHealth equal to 100), and since 100 is not less than maxHealth, the loop stops without printing 100.
The expected output is:2540557085
Cheat sheet
The while loop repeats code as long as a condition remains true:
while condition do
-- code to repeat
endExample that prints numbers from 1 to 3:
local count = 1
while count <= 3 do
print(count)
count = count + 1
endThe loop checks the condition, runs the code if true, then checks again until the condition becomes false.
Try it yourself
-- Initialize player health and maximum health
local playerHealth = 25
local maxHealth = 100
-- TODO: Write your code here
-- Create a while loop that continues while playerHealth < maxHealth
-- Inside the loop: print current health, then increase playerHealth by 15This 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