Go provides three types of loops to handle repetitive tasks: the
for loop, which is the only looping construct in Go. Unlike some other languages, Go does not have while or do-while loops. Instead, the for loop in Go is versatile and can be used in multiple ways to achieve the functionality of traditional loops like while or do-while.Here are the different forms of the for loop in Go:
1. Basic for Loop
The basic for loop is similar to the for loop in other languages. It has three components:
- Initialization: Executes once at the beginning.
- Condition: Checks whether the loop should continue or stop.
- Post Statement: Executes after each iteration.
Syntax:
Example:
Explanation:
- Initialization:
i := 0initializesi. - Condition:
i < 5checks ifiis less than 5. - Post:
i++incrementsiafter each iteration. - This loop will print the values 0 through 4.
2. for Loop as a while Loop
Go does not have a while loop, but you can easily simulate it using a for loop. If you omit the initialization and post statements, the for loop works like a traditional while loop.
Syntax:
Example:
Explanation:
- The loop will continue to execute as long as
iis less than 5. - There’s no initialization or post statement; the loop behaves like a
whileloop.
3. Infinite for Loop
You can create an infinite loop by omitting all three components of the for loop. This loop will continue indefinitely until explicitly broken with a break statement or another control flow mechanism like a return or panic.
Syntax:
Example:
Explanation:
- The loop runs infinitely but is stopped when
ireaches 5. - The
breakstatement exits the loop once the condition is met.
4. for Loop with continue
The continue statement allows you to skip the current iteration and move to the next iteration of the loop.
Example:
Explanation:
- When
iis 2, thecontinuestatement is executed, skipping thefmt.Println(i)for that iteration. - The output will be
0, 1, 3, 4because the loop skips printing wheniis 2.
5. for Loop with break
The break statement is used to exit the loop prematurely, regardless of the loop’s condition.
Example:
Explanation:
- The loop will print
0, 1, 2and will exit wheniequals 3 due to thebreakstatement.
Summary of Loop Types in Go
| Loop Type | Description | Example Use Case |
|---|---|---|
Basic for loop | Loops with initialization, condition, and post statements | Iterating over a known range or fixed condition |
for as a while loop | Omits initialization and post statements to act like while | Loops based on a condition only |
Infinite for loop | Omits all components, resulting in an infinite loop | Continuous execution until explicitly stopped |
for loop with continue | Skips the current iteration and proceeds to the next one | Skip specific iterations based on a condition |
for loop with break | Exits the loop early when a condition is met | Stop the loop based on a specific condition |
Conclusion
Go uses a single looping construct — the for loop — to handle all forms of iteration. Whether you're using it as a traditional for loop, a while loop, or even for infinite loops, the flexibility of the for statement makes it highly versatile. With the addition of break and continue, Go provides a powerful control flow mechanism for managing loops in various scenarios.

comment
ReplyDelete