> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Go For Loop

The `for` loop is the sole iterative control flow statement in Go. It unifies the behavior of traditional `for` and `while` loops found in other C-family languages into a single keyword. Go achieves this by overloading the `for` statement with four distinct syntactic forms.

## 1. The Three-Component Loop

This is the standard C-style loop, consisting of an initialization statement, a boolean condition, and a post-iteration statement.

```go theme={"dark"}
for init; condition; post {
    // loop body
}
```

* **`init`**: Executed once before the loop begins. Variables declared here using the short variable declaration (`:=`) are lexically scoped to the loop block. As of Go 1.22, these variables are scoped per-iteration rather than per-loop, meaning each iteration creates a distinct instance of the variable.
* **`condition`**: A boolean expression evaluated before every iteration. If it evaluates to `false`, the loop terminates.
* **`post`**: Executed immediately after each iteration of the loop body, prior to the next condition evaluation.

*Note: Unlike C or Java, Go does not wrap the three components in parentheses, but the braces `{}` surrounding the body are mandatory.*

## 2. The Condition-Only Loop

By omitting the `init` and `post` statements, the `for` loop functions identically to a traditional `while` loop. The semicolons are dropped in this form.

```go theme={"dark"}
for condition {
    // loop body
}
```

The loop continues to execute as long as the `condition` evaluates to `true`.

## 3. The Infinite Loop

Omitting all three components creates an infinite loop.

```go theme={"dark"}
for {
    // loop body
}
```

Execution will continue indefinitely unless interrupted by a loop control statement (e.g., `break`, `return`, or a `panic`).

## 4. The For-Range Loop

The `for range` construct is used to iterate over elements of iterable data structures, integers, and iterator functions.

```go theme={"dark"}
for key, value := range iterable {
    // loop body
}
```

The `range` clause yields zero, one, or two iteration variables depending on the underlying type:

* **Arrays/Slices**: Yields the `index` (`int`) and a copy of the `value` at that index.
* **Strings**: Yields the byte `index` (`int`) and the Unicode code point (`rune`).
* **Maps**: Yields the `key` and a copy of the `value`. Iteration order is intentionally randomized.
* **Channels**: Yields only the `value` received from the channel until the channel is closed.
* **Integers (Go 1.22+)**: Yields an integer from `0` up to, but not including, the specified integer (e.g., `for i := range 10`).
* **Iterator Functions (Go 1.23+)**: Yields values from functions matching standard iterator signatures (e.g., `iter.Seq` or `iter.Seq2`).

If an iteration variable is not required, it must be explicitly discarded using the blank identifier (`_`). Alternatively, the second variable can be omitted entirely if only the first is needed (`for key := range iterable`). Both variables can also be omitted entirely if the loop only needs to execute a specific number of times without referencing the yielded values (`for range iterable { ... }`).

Similar to the three-component loop, variables declared with `:=` in a `range` clause are scoped per-iteration as of Go 1.22.

## Loop Control Statements

Go provides specific statements to alter the standard execution flow of a `for` loop:

* **`break`**: Immediately terminates the execution of the innermost enclosing `for`, `switch`, or `select` statement.
* **`continue`**: Halts the current iteration, skips the remaining body, executes the `post` statement (if applicable), and proceeds to the next `condition` evaluation.
* **Labels**: Both `break` and `continue` can be followed by an optional label to target a specific outer loop in nested loop architectures.

```go theme={"dark"}
OuterLoop:
for i := 0; i < 5; i++ {
    for j := 0; j < 5; j++ {
        if i == 2 && j == 2 {
            break OuterLoop // Terminates the loop labeled 'OuterLoop'
        }
    }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Go Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
