Menu
Coddy logo textTech

For Loop - Condition Only

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

Let's learn about the condition-only for loop in Go. This type of loop works like a while loop in other languages, using just a condition without initialization or post statements.

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

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

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

1
2
3

The loop works by checking if i is less than or equal to 3 before each iteration. Inside the loop, we print the current value of i and then increase it by 1 with i++. The loop continues running as long as the condition i <= 3 remains true, and stops when i becomes 4.

Unlike the standard for loop, we need to manually increment our counter variable inside the loop body, otherwise we'd create an infinite loop.

challenge icon

Challenge

Beginner

In this challenge, you'll practice using a for loop with only a condition (no initialization or post statement). This is similar to a while loop in other languages.

We've provided a boolean variable keepRunning set to true and a counter variable count set to 0. Your task is to complete the for loop that will print numbers from 0 to 4 and then stop.

Inside the loop, we increment count and check if it reaches 5. When it does, we set keepRunning to false to exit the loop.

Cheat sheet

The condition-only for loop in Go works like a while loop, using just a condition without initialization or post statements:

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

The loop checks the condition before each iteration and continues as long as it remains true. You must manually increment the counter variable inside the loop body to avoid infinite loops.

Try it yourself

package main

import "fmt"

func main() {
	// These variables are already set up for you
	keepRunning := true
	count := 0
	
	// TODO: Complete the for loop that uses only a condition (like a while loop)
	// The loop should continue as long as keepRunning is true
	for {
		// Print the current count
		fmt.Println(count)
		
		// Increment count
		count++
		
		// Check if count is 5, and if so, set keepRunning to false
		if count >= 5 {
			keepRunning = false
		}
	}
}
quiz iconTest yourself

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

All lessons in Fundamentals