for Loop with a Step
Part of the Fundamentals section of Coddy's Lua journey — lesson 74 of 90.
The numeric for loop becomes even more powerful when you add a third argument called the step. The step controls how much the loop variable changes each time through the loop.
The expanded syntax looks like this:
for variable = start, finish, step do
-- code to repeat
endHere's an example that counts by 2s instead of 1s:
for i = 2, 10, 2 do
print(i)
endThis loop starts at 2, goes up to 10, and increases by 2 each time, printing: 2, 4, 6, 8, 10.
You can also use negative steps to count backwards:
for i = 5, 1, -1 do
print(i)
endThis counts down from 5 to 1, printing: 5, 4, 3, 2, 1.
The step gives you precise control over how your loop progresses, making it perfect for tasks like processing every other item in a list, creating countdowns, or working with specific number patterns.
Challenge
EasyCreate a multiplication table generator that uses a numeric for loop with a step to display multiples of 3. Write a for loop that starts at 3, goes up to 15, and uses a step of 3 to print only the multiples of 3.
Inside the loop, print each multiple value on a separate line. This will demonstrate how the step parameter controls the loop's progression by skipping values.
Cheat sheet
The numeric for loop can include a third argument called the step to control how much the loop variable changes each iteration:
for variable = start, finish, step do
-- code to repeat
endCount by 2s:
for i = 2, 10, 2 do
print(i) -- prints: 2, 4, 6, 8, 10
endCount backwards with negative step:
for i = 5, 1, -1 do
print(i) -- prints: 5, 4, 3, 2, 1
endTry it yourself
-- TODO: Write your code here
-- Create a for loop that starts at 3, goes up to 15, with a step of 3
-- Print each multiple on a separate lineThis 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