Gaaker Go

Golang For loop with only conditional expression

The initialization and modification part of the for loop structure is optional.

Similar to the following

for ; condition; {
    ...
}

The two semicolons can also be omitted.

The following is a complete code example.

package main

import "fmt"

func main() {
	sum := 1
	for ; sum < 1000; {
		sum += sum
	}

	fmt.Println(sum)

	for sum < 2000 {
		sum += sum
	}
	
	fmt.Println(sum)
}

Below is the program output:

1024
2048
< For loop with only conditional expression For loop with only conditional expression >