Studyfin Studyfin ✨ Sign up free

Algorithms

Grade 9 · Coding · Free lesson

Hi there! I'm Studyfin, and today we will explore how coding loops make our algorithms incredibly powerful and efficient.

An algorithm is a step-by-step set of instructions to solve a problem. When we need to repeat steps, we use a tool called iteration, or a loop.

machine learning

Imagine writing a program to send ten birthday invites. Without iteration, you must write the 'send' command ten separate times. That makes your code long and hard to read.

1. Send Invite to Sam2. Send Invite to Alex3. Send Invite to TaylorMessy & Repetitive!

With iteration, we write the 'send' command just once inside a loop. This makes our algorithm shorter, easier to fix, and much faster to run.

Repeat 10 Times:Send Invite
✏️ Worked example

Analyze how using a loop improves an algorithm that prints the numbers 1 to 100.

  1. Identify the goal: We want to print every number from 1 to 100.
  2. Compare the manual way: Writing 'print(1)', 'print(2)', all the way to 'print(100)' takes 100 lines of code.
  3. Apply iteration: Create a loop that starts a counter at 1, prints the counter, adds 1 to it, and stops after 100.
  4. Analyze the benefits: The loop version takes only 3 lines of code instead of 100. It is cleaner, has fewer places for typos, and is easy to change if we want to print to 1000 instead.
algorithm
Want the full interactive lesson — quiz, animations, and Finn cheering them on?

More Coding lessons