Studyfin Studyfin ✨ Sign up free

Loops

Grade 8 · Coding · Free lesson

Hey there! Ready to learn how coders save time? Let's explore loops, the ultimate coding superpower!

Imagine you are building an app to help people drink enough water. To track eight glasses of water, you do not want to write the same code eight times. Instead, we use a design process to create a program with loops to solve this everyday problem.

ai generated

A loop is a programming tool that repeats a set of instructions. In block-based languages like Scratch, a loop block wraps around other blocks. In text-based coding, we use words like 'for' or 'while' to repeat actions automatically.

REPEATING LOOP

Programs also need conditionals, which are 'if-then' decisions. We can use a conditional inside our loop to check a condition. For example, the loop repeats 'drink water' IF the total glasses drank is still under eight.

Is Water < 8? Drink Water
✏️ Worked example

Let's design a program to solve an everyday problem: sending a morning alarm reminder. We want our phone to beep exactly five times when the alarm event triggers, then stop.

  1. Identify the goal: We need to repeat a beep sound five times when the alarm goes off.
  2. Define the event: The program starts when the 'alarm triggers' event occurs.
  3. Set up a counter: Create a variable called 'count' and set its starting value to 0.
  4. Create the loop: Use a loop that repeats while 'count' is less than 5.
  5. Add actions inside the loop: Inside the loop, play the beep sound, then add 1 to our 'count' variable.
  6. Test the loop: The loop runs for count = 0, 1, 2, 3, and 4. Once count reaches 5, the loop stops successfully!
math
Want the full interactive lesson — quiz, animations, and Finn cheering them on?

More Coding lessons