The Numeric for Loop
Part of the Fundamentals section of Coddy's Lua journey — lesson 73 of 90.
When you need to repeat code a specific number of times, the numeric for loop is your best choice. Unlike while and repeat-until loops that depend on changing conditions, the numeric for loop automatically handles the counting for you.
The basic syntax looks like this:
for variable = start, finish do
-- code to repeat
endHere's a simple example that prints the numbers 1, 2, and 3:
for i = 1, 3 do
print(i)
endThe loop creates a variable i that starts at 1 and automatically increases by 1 each time through the loop. When i reaches 3, the loop runs one final time and then stops.
You don't need to manually update the counter variable - Lua handles this automatically.
This makes numeric for loops perfect for situations where you know exactly how many times you want something to happen, like printing a message multiple times, processing each item in a list, or counting through a range of numbers.
Challenge
EasyCreate a training session tracker that uses a numeric for loop to simulate multiple workout repetitions. Write a for loop that runs from 1 to 5, representing 5 training rounds.
Inside the loop, print the message "Training round: " followed by the current round number. This will help you practice the basic numeric for loop syntax and understand how the loop variable automatically increments.
Cheat sheet
The numeric for loop repeats code a specific number of times with automatic counting:
for variable = start, finish do
-- code to repeat
endExample that prints numbers 1, 2, and 3:
for i = 1, 3 do
print(i)
endThe loop variable automatically starts at the start value and increases by 1 each iteration until it reaches the finish value.
Try it yourself
-- TODO: Write your code here
-- Use a for loop to iterate from 1 to 5
-- Print "Training round: " followed by the round number for each iterationThis 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