Skip to main content

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.

In Go, a condition-only for loop is a control flow statement that repeatedly executes a block of code as long as a specified boolean expression evaluates to true. It serves as Go’s idiomatic equivalent to the while loop found in other C-family languages, as Go intentionally omits the while keyword from its syntax.
for condition {
    // loop body
}

Technical Characteristics

  • Strict Boolean Evaluation: The condition must be a strictly typed boolean expression. Go does not support “truthiness” evaluation; you cannot use integers (e.g., 1 or 0), strings, or pointers directly as the loop condition.
  • Omission of Semicolons: When using the condition-only form, the initialization and post-statement components of a standard three-part for loop (for init; condition; post) are entirely omitted, along with their separating semicolons.
  • State Mutation: Because there is no built-in post-statement to increment or decrement a counter, the state evaluated by the condition must be mutated within the loop body to prevent an infinite loop.
  • Lexical Scoping: Variables evaluated in the condition are typically declared in the outer lexical scope prior to the loop. Variables declared within the loop body are scoped strictly to the current iteration.

Execution Flow

  1. The boolean condition is evaluated.
  2. If the condition evaluates to true, the loop body executes sequentially.
  3. Upon reaching the end of the loop body, control flow returns to step 1.
  4. If the condition evaluates to false, the loop terminates, and control flow transfers to the statement immediately following the loop block.

Syntax Example

package main

import "fmt"

func main() {
    n := 1 // State initialized in the outer scope

    // Condition-only for loop
    for n < 5 {
        fmt.Println(n)
        n *= 2 // State mutated within the loop body
    }
}

Control Flow Modifiers

The execution of a condition-only for loop can be altered internally using standard Go jump statements:
  • break: Immediately terminates the loop entirely, bypassing the condition evaluation.
  • continue: Halts the current iteration, skips any remaining statements in the loop body, and immediately re-evaluates the condition for the next iteration.
Master Go with Deep Grasping Methodology!Learn More