Why Loops?

Grade 5, page 1 of 11. A one-minute read.

Picture this. You were chatting in class (again), and your teacher has given you a punishment: write "I will not talk in class" on the board 100 times.

You'd be there for ages. Your hand would cramp. By line 60, you'd start writing "I will not tlak in calss." By line 85, you'd be questioning all your life choices.

Now imagine you could just say: "Write this sentence 100 times," and it was done instantly, perfectly, every single line.

That's a loop.

JavaScript
for (let i = 1; i <= 100; i++) {
  console.log("I will not talk in class");
}

Three lines of code. A hundred perfect sentences. Zero hand cramps. Change 100 to 1000000 and it'll write a million of them in about a second.

Computers are spectacularly good at repeating things. They never get bored, never get tired, and never make typos on the 85th try. Loops let you harness that power. Any time you find yourself thinking "I need to do this for every item" or "keep doing this until something happens," you need a loop.

A loop: check a condition, run the body, update, and go round again

Reading is step one

CurlyScript turns these notes into a game: short questions, an explanation after every answer, and a test at the end of each grade. Free, with nothing to sign up for.

Start learning