Loops and Iteration
LockedLesson 5 of 25 · Syntax · Easy
Finish these first:Conditionals and Branching →Or pass the tests below to unlock it now.
The big idea
A for loop repeats a block of code while counting through a range of numbers. Put the start, stop rule, and step inside ( ...
Think of it like this: Think of a `for` loop like counting steps on a staircase: start at step 1, stop after step 5, and climb 1 step each time.
Read the lesson
A for loop repeats a block of code while counting through a range of numbers.
Put the start, stop rule, and step inside ( ... ):
let coins = 0;
for (let i = 1; i <= 3; i++) {
coins = coins + i; // coins becomes 6 (1 + 2 + 3)
}
Each pass adds i to coins and steps i up by 1 (i++) until i <= 3 becomes false.
Quick check
What does `i++` do at the end of each pass through a `for` loop?
Your challenge
Write a for loop from 1 to 5 that adds each number to total (so total becomes 15).
More learning tools(compare languages, visual guide, related lessons)
Compare with another language
Similar to python.loops (90% overlap). Iteration: for-of in JS, range-for in C++, single 'for' keyword in Go, Iterator/for-in in Rust.
Visual walkthrough
A diagram, a step-by-step animation, and short captions for Loops and Iteration.
Learn this first
- Conditionals and BranchingNot yet