Day 5 45 min beginner

Control Flow - Loops

Master the only loop construct in Go: the versatile for loop

Learning Objectives

  • Use the standard three-component for loop
  • Implement while-style loops using for
  • Create infinite loops
  • Use break and continue to control loop flow
  • Introduction to the range keyword

The One and Only: for Loop

In many languages, you have for, while, and do-while loops. Go takes a simpler approach: it only has the for loop. However, this single construct is versatile enough to handle all looping needs.

Standard Three-Component Loop

The most common form is similar to C or Java:

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package main

import "fmt"

func main() {
    // init; condition; post
    for i := 0; i < 5; i++ {
        fmt.Println("Iteration:", i)
    }
}
  1. Init statement: executed before the first iteration (usually i := 0)
  2. Condition expression: evaluated before every iteration. If false, the loop stops.
  3. Post statement: executed at the end of every iteration (usually i++)
No Parentheses
Just like if, the for loop does not use parentheses around its components, but braces {} are required.

for as a “while” Loop

If you omit the init and post statements, the for loop behaves like a while loop:

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package main

import "fmt"

func main() {
    count := 1
    
    // Only condition is provided
    for count <= 5 {
        fmt.Println("Count is:", count)
        count++
    }
}

Infinite Loops

If you omit the condition as well, you get an infinite loop:

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package main

import "fmt"

func main() {
    for {
        fmt.Println("This runs forever...")
        break // ...unless we break out of it!
    }
}

Loop Control: break and continue

  • break: Immediately exits the loop.
  • continue: Skips the rest of the current iteration and starts the next one.
main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package main

import "fmt"

func main() {
    for i := 1; i <= 10; i++ {
        if i%2 == 0 {
            continue // Skip even numbers
        }
        if i > 7 {
            break // Stop if number is greater than 7
        }
        fmt.Println("Odd number:", i)
    }
}

Iterating with range

The range keyword is used to iterate over elements in a variety of data structures (arrays, slices, maps, strings, and channels). We’ll cover these structures in detail later, but here’s a sneak peek with a string:

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package main

import "fmt"

func main() {
    message := "Go"
    
    // range returns index and value
    for i, char := range message {
        fmt.Printf("Index %d has character %c\n", i, char)
    }
    
    // Use _ if you don't need the index
    for _, char := range "Hello" {
        fmt.Printf("%c ", char)
    }
}

Nested Loops

You can put loops inside loops:

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package main

import "fmt"

func main() {
    for i := 1; i <= 3; i++ {
        for j := 1; j <= 3; j++ {
            fmt.Printf("(%d, %d) ", i, j)
        }
        fmt.Println()
    }
}

Labelled break and continue

When dealing with nested loops, you can use labels to break or continue an outer loop:

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package main

import "fmt"

func main() {
OuterLoop:
    for i := 1; i <= 3; i++ {
        for j := 1; j <= 3; j++ {
            if i == 2 && j == 2 {
                break OuterLoop // Breaks the outer loop
            }
            fmt.Printf("(%d, %d) ", i, j)
        }
        fmt.Println()
    }
}

Summary

Today you learned:

  • Go only has one loop keyword: for
  • The three forms of for: standard, while-style, and infinite
  • How to use break and continue
  • How to use range for basic iteration
  • Using labels to control nested loops

Practice Exercise

  1. Write a program that prints the multiplication table for a given number (1 to 10).
  2. Write a program that finds all prime numbers between 1 and 50 using nested loops.
  3. Use a for loop to reverse a string (Hint: iterate backwards or use range).
  4. Implement a “Guess the Number” game where the user has limited attempts to guess a number, using for and break.

Next Steps

Tomorrow, we’ll dive into Functions - the building blocks of Go programs!

Now, test your loop knowledge with the quiz!

Quiz

Complete this quiz with a minimum score of 80% to mark Day 5 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!