Module 05 / 15

Loops: Don't Repeat Yourself

Make the computer do the hard work while you relax.

Computers never get tired. They don't complain about doing the same thing a million times. We use Loops to give a command once and tell the computer how many times to repeat it.

What You Will Learn
  • What a "Loop" is and why it saves time.
  • How to write a for loop.
  • How to use a counter to keep track of repeats.
Who Is This For?

This is for the "Smart-Lazy" student. If you have a list of 500 students and you need to send them all the same email, you don't want to click "Send" 500 times. You want a loop!

Real Life Connection

Think about a Race Track:

  • Start: You begin at Lap 1.
  • Action: You run around the track.
  • Check: Have you finished 10 laps?
  • Repeat: If NO, run again. If YES, stop!

A loop is just the computer running laps around your code.

The 'For' Loop Recipe

The for loop has three parts inside its parentheses ():

  1. Start: Where do we begin? (Usually at 0).
  2. Condition: When do we stop? (e.g., as long as we are under 5).
  3. Step: How do we count? (Add 1 every time).

We use i++ as a shortcut for "Add 1 to the count".

The Loop Cycle
DO ACTION

COUNT +1
Is count < limit?
YES? Go again! 🔄
NO? Exit! 🚪
Writing Your First Loop

Let's make the computer count from 1 to 5. We use let i = 1 to create a counter that can change.

function startCounting() {
  // Start at 1; Stop at 5; Add 1 each time
  for (let i = 1; i <= 5; i++) {
    // This happens 5 times!
    Logger.log("Current Number: " + i);
  }
}
The Full "Repeat Machine"

Copy this and change the 5 to 10 or 20!

function repeatMachine() { for (let counter = 1; counter <= 5; counter++) { Logger.log("Running lap number: " + counter); } Logger.log("Race Finished! 🎉"); }
The Result

Your log will show the message repeated perfectly with the updated number each time:

[Execution Log]
Running lap number: 1
Running lap number: 2
Running lap number: 3
Running lap number: 4
Running lap number: 5
Race Finished! 🎉
The "Infinite Loop" Trap!
Click to avoid breaking your browser!

1. The Infinite Loop: If you forget to add i++, the count stays at 1 forever. The computer will keep running the loop until it crashes! 😱

2. Semicolons: Inside the for(), you MUST use semicolons ; to separate the three parts. If you use commas, it will fail.

3. Wrong Direction: If you start at 10 and say i++ (add 1) while trying to reach 1, the condition will never be met. Be careful with your math!

The "Chalkboard" Challenge

Write a script that logs the sentence: "I will always use Google Apps Script" exactly 10 times. Can you make it show the number (1 to 10) next to the sentence?

Who Else Uses This?

Instagram uses loops to show you posts. It says: "For every post in my database that my user likes, show it on their screen." Without loops, your feed would only have one picture!

Module Summary

You’ve unlocked the power of automation! Loops allow you to repeat complex tasks instantly. Instead of writing 100 lines of code, you can now write 3 lines and let the computer do the rest.

Status: Efficiency Expert 🔄 | Level: Automation Pilot ✈️

Type NEXT to unlock the next module 🚀