Lesson 3.3: Counting Loops and Loop Variables
A counting loop changes a loop variable through a planned range. The loop variable tells which repeat is currently running.
A loop from 1 to 4 by 1 uses the values 1, 2, 3, and 4. That is four repeats. A loop from 0 to 4 that includes both ends uses five values. Confusing those counts causes an off-by-one error.
The step value controls how the loop variable changes. A positive step counts upward. A negative step counts downward when the start and end values are arranged for it.
Guided practice
Create a counting loop with i from 1 to 4 by 1. Print i inside the loop. Predict all four output values.
Now move forward by 1 each time and print the final scene position after the loop. Test a loop from 0 to 3 and compare its repeat count. Both examples repeat four times even though their first and last loop-variable values differ.
Use clear loop variable names when they have a meaning, such as row or itemIndex.
Loading the block workspace...
Ready to move on?
Only mark complete if you genuinely understand the material. Your progress will be saved in this browser.
Stuck on this lesson?
Ask about anything on this page. It can see which lesson you have open and which part you are reading.