GCGrowCarrer

Loops and Iteration

Locked

Lesson 5 of 25 · Syntax · Easy

Finish these first:Conditionals and Branching →Or pass the tests below to unlock it now.

1

The big idea

Go keeps looping simple: for is the only loop keyword you need to learn. Write the start, stop rule, and step right after for (no parentheses needed): Each pass adds i to coins and increases i by 1 (i++) until i <= 3 is false.

Think of it like this: Think of Go's `for` loop like counting beads: start at 1, keep going while `i <= 5`, and step up by 1 each time.
Key words (tap to learn):
2

Read the lesson

Go keeps looping simple: for is the only loop keyword you need to learn.

Write the start, stop rule, and step right after for (no parentheses needed):

coins := 0
for i := 1; i <= 3; i++ {
    coins = coins + i   // coins becomes 6 (1 + 2 + 3)
}

Each pass adds i to coins and increases i by 1 (i++) until i <= 3 is false.

3

Quick check

Does Go put parentheses around a `for` loop header?

4

Your challenge

2 sample tests, 1 hidden

Inside main(), write a for loop from 1 to 5 that adds each number to total (so total becomes 15).

Stuck? Get a hint (free)
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.

Mastery0%