Loops: repeating steps — Computing, 7–10
A loop tells a computer to repeat an action or group of actions. Instead of writing the same instruction many times, you describe the repeat rule and let the computer count or check it.
Repeat with a rule
A loop puts steps inside a repeat instruction. It can repeat a fixed number of times, such as 10 jumps, or continue while a condition is true, such as moving while the path is clear. The rule controls when the repetition stops.
Why use loops?
Without loops, a program would need a separate line for every repeated action. That makes it long and easy to change incorrectly. Loops solve the problem of describing one pattern once, then applying it many times, while keeping the instructions easier to read.
Example: a square
To draw a square, a turtle needs four identical sides and four turns. Put “move forward 50 steps, turn right 90 degrees” inside a loop that repeats 4 times. The turtle draws one side, turns, and repeats until all four sides are complete.
The trap: the endless loop
A common mistake is making a loop whose stopping condition can never become true, or choosing the wrong repeat count. This happens because the repeated steps look correct on their own. Check what changes each time and ask exactly when the loop must stop.
Where does it appear?
Loops are useful when a traffic light cycles through colours, a game keeps checking for a collision, or a music app plays a beat repeatedly. They are also used to process each item in a list. Repetition is not accidental; it is part of the design.
Keep exploring
Other languages
Loading MyLeoNes™…