Module 5 · Repeating work with loops
Numeric for loops
Repeat a known number of times with a changing loop variable.
What you will learn
- Identify numeric loop start, end, and loop variable
- Count upward and downward
- iteration
- One pass through a loop body.
- loop variable
- The value that changes for each iteration.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Coin Quest may count down seconds or create a known row of markers. Repeating one clear body is safer than copying it many times.
The mental model
A numeric for loop gives the loop variable each number from its start to its end. The body runs once per value.
An optional third number is the step; a negative step counts downward.
New words and symbols
--!strict asks Studio to check your code for type mistakes. It is an instruction for the editor and produces no Output message.
Matching quotes such as "hello" surround a string: a piece of text. The quotation marks tell Luau where the text starts and ends.
print(...) calls the built-in print function. Put the values to show inside its parentheses; separate multiple values with commas. Read the result in Studio's Output panel.
end is read as “end”: it closes a block such as a decision, loop, or function.
for is read as “for”: it starts a controlled repetition with a changing loop value.
= is read as “is assigned”: it evaluates the right side and stores that value in the name on the left.
A tiny example
--!strictfor count = 1, 3 do print("Count", count)endLine by line
- 3
count begins at 1, ends at 3, and do starts the repeated body.
- 4
Each iteration prints the current loop variable.
- 5
end closes the loop.
Read the code aloud
Read one statement at a time: name the value being read, what the operation does, and where its result goes. Keep identifiers exactly as written so you can find them again in the code.
Pause at calls, conditions, and loops. Explain when the body runs before moving to the next statement; use the line notes above when a symbol is unfamiliar.
Execution trace
Begin at the first executable statement. Comments and blank lines do not run; a branch chooses which block runs, and a loop may revisit its body.
Line 3: count begins at 1, ends at 3, and do starts the repeated body.
Line 4: Each iteration prints the current loop variable.
Line 5: end closes the loop.
State before and after
Before running, identify the starting values and any existing Studio objects this example needs. List what you expect to change.
After your practice change, check this result: Output follows the exact inclusive range in the expected direction.
Reading or printing a value leaves it unchanged. Look for assignments, calls that change state, and property updates to explain each change you observe.
Predict before running
How many times does the body run?
Try it
Mark each step after you have tried it.
Common mistake
for count = 1, 3 print(count)endNumeric for syntax needs do before the body. Add it after the range.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Numeric for loops repeat across an inclusive range.
- The loop variable changes each iteration.
- A negative step counts down.
Next: Next, you will repeat while a changing condition remains true.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.