Golang之旅#18继续(Golang tour #18 For continued)

这可能是一个我无法看到的简单事情,但我正在浏览golang之旅和“For continue”部分,我想知道是否有人可以向我解释逻辑如何执行并提供声明1024. https://tour.golang.org/flowcontrol/2

package main import "fmt" func main() { sum := 1 for ; sum < 1000; { sum += sum } fmt.Println(sum) }

This may be a simple thing I can't see for some reason but I am going through the golang tour and in the "For continued" section and I was wondering if someone could explain to me how the logic executes and delivers the statement to be 1024. https://tour.golang.org/flowcontrol/2

package main import "fmt" func main() { sum := 1 for ; sum < 1000; { sum += sum } fmt.Println(sum) }

最满意答案

它真的只是做2的力量

所以输出是

1 2 4 8 16 32 ... 1024

把这个循环想象成

sum = 1 while(sum < x) { sum = sum * 2 }

Its really just doing powers of 2

so the outputs are

1 2 4 8 16 32 ... 1024

Think of this loop as

sum = 1 while(sum < x) { sum = sum * 2 }

更多推荐