Studyfin Studyfin ✨ Sign up free

Loops

Grade 6 · Coding · Free lesson

Hi there! I am Studyfin, your coding tutor. Today, we will learn how to make our code repeat tasks easily using loops!

Sometimes we need to do a task many times, like taking ten steps forward. Writing the same code ten times is slow and messy. We use a design process to find these repeating steps and make them simpler.

kids

A loop is a programming tool that repeats a block of code. Instead of writing 'step forward' five times, we write it once inside a loop. The loop tells the computer to repeat that action five times.

The Repeat Loop

We can use loops with conditionals, which are 'if-then' decisions. For example, a loop can repeat a task 'until' a path is blocked. This helps our programs solve everyday problems dynamically.

Loop Until BlockedRepeat: Move forward if path is clear!
✏️ Worked example

Imagine you are building a game where a character must water 5 plants in a row. Let us design a simple program to solve this everyday problem using a loop instead of writing the same code 5 times.

  1. Analyze the problem: Identify the actions that repeat. The character needs to 'water' and then 'move right' for each plant.
  2. Plan the sequence: Without a loop, the code would be: Water, Move, Water, Move, Water, Move, Water, Move, Water, Move.
  3. Create the loop: Group the repeating actions into a single block: 'Water' and then 'Move right'.
  4. Set the loop limit: Tell the loop to repeat exactly 5 times so the character stops after the last plant.
  5. Test the program: Run the loop. The character waters a plant, moves, and repeats this sequence until all 5 plants are watered.
math
Want the full interactive lesson — quiz, animations, and Finn cheering them on?

More Coding lessons