Menu
Coddy logo textTech

For Loop - Basic

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

Let's learn about the basic for loop in Go, focusing on the initialization part. A for loop helps us repeat code multiple times.

First, let's create a simple loop that counts from 1 to 3:

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

After executing this code, the numbers 1, 2, and 3 will show on the screen one after another:

1
2
3

The initialization part i := 1 happens only once before the loop starts. It creates a variable i and sets it to 1. This variable i is only available inside the loop.

The middle part i <= 3 is the condition that checks if we should continue looping. The last part i++ increases the value of i by 1 after each loop.

challenge icon

Challenge

Beginner
In this challenge, you'll practice using a for loop with initialization, condition, and post statement. Your task is to complete the for loop to print the numbers from 1 to 5.

The for loop should:
1. Initialize a variable i to 1
2. Continue as long as i is less than or equal to 5
3. Increment i by 1 after each iteration

Cheat sheet

A for loop in Go repeats code multiple times with three parts: initialization, condition, and increment:

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

The initialization i := 1 creates a variable and sets its initial value (happens only once). The condition i <= 3 checks if the loop should continue. The increment i++ increases the variable by 1 after each iteration.

The loop variable i is only available inside the loop scope.

Try it yourself

package main

import "fmt"

func main() {
	// TODO: Complete the for loop to print numbers from 1 to 5
	// Use the initialization, condition, and post statement format
	for /* Your code here */ {
		fmt.Println(i)
	}
}
quiz iconTest yourself

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

All lessons in Fundamentals