Menu
Coddy logo textTech

What The `for` Loop Explained

Part of the Fundamentals section of Coddy's GO journey — lesson 44 of 109.

The for loop is Go's only looping construct, used to repeat a block of code multiple times. Let's explore how it works step by step.

First, let's create a simple program that counts from 1 to 5 using a for loop:

for i := 1; i <= 5; i++ {
    fmt.Println(i)
}

After executing this code, the numbers 1 through 5 will show on the screen, one per line:

1
2
3
4
5

Let's understand what's happening in our for loop:

The for loop has three main parts:

1. i := 1 - This is the initialization part where we create a variable and set its starting value

2. i <= 5 - This is the condition that's checked before each loop iteration; the loop continues as long as this is true

3. i++ - This is the post statement that runs after each loop iteration (it adds 1 to i)

The loop works by first initializing i to 1, then checking if i is less or equal to 5. If true, it runs the code inside the loop (printing the value), then executes i++ to increase the value by 1. This process repeats until the condition becomes false (when i becomes 6).

challenge icon

Challenge

Beginner

In this challenge, you'll practice using a basic for loop to count and print numbers.

Your task is to write a for loop that counts from 1 to 10 and prints each number on a new line.

This will help you understand how to use the for loop for simple counting tasks.

Cheat sheet

The for loop is Go's only looping construct, used to repeat a block of code multiple times.

Basic for loop syntax:

for i := 1; i <= 5; i++ {
    fmt.Println(i)
}

The for loop has three main parts:

1. i := 1 - Initialization: creates a variable and sets its starting value

2. i <= 5 - Condition: checked before each iteration; loop continues while true

3. i++ - Post statement: runs after each iteration (increments i by 1)

Try it yourself

package main

import "fmt"

func main() {
	// TODO: Write a for loop that counts from 1 to 10
	// and prints each number on a new line
	
	
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals