英文:
'For' loop pre and post empty statements
问题
在Go语言中,for循环的前置语句和后置语句为空的意思是什么,就像下面的例子一样?
sum := 1
for ; sum < 10; {
sum += sum
}
fmt.Println(sum)
这段代码中,for循环的前置语句和后置语句都为空。这意味着在循环开始之前没有需要执行的语句,也没有在每次循环结束后需要执行的语句。循环的条件是sum < 10
,只要条件满足,循环就会一直执行。在每次循环中,sum += sum
语句会将sum
的值加倍。当sum
的值大于等于10时,循环结束。最后,通过fmt.Println(sum)
语句将sum
的值打印出来。
英文:
What does it mean in Go that the pre and post statements of a for loop are empty, like in the following example?
sum := 1
for ; sum < 10; {
sum += sum
}
fmt.Println(sum)
答案1
得分: 5
请记住,for循环和while循环是相同的。
你的代码可以用其他语言重写,如下所示:
sum := 1
while(sum < 10) {
sum += sum
}
fmt.Println(sum)
在for
循环中,有3个部分。
for(初始化语句 ; 条件 ; 结束语句通常是迭代语句)
这等同于
初始化语句
while(条件) {
这里是代码块
结束迭代语句
}
你的循环可以在没有前置和后置语句的情况下编写,因为你已经在代码的其他部分指定了它们。
英文:
Remember that a for loop is the same as a while loop.
Your code can be rewritten in other languages as
sum := 1
while(sum < 10) {
sum += sum
}
fmt.Println(sum)
In a for
loop, there are 3 parts.
for(initial statement ; condition ; end statement usually iterate)
This is equivalent to
initial statement
while(condition) {
Stuff here
End iteration statement
}
The reason your loop can be written withiut the pre and post statements is because you've specified them in other parts of the code.
答案2
得分: 3
它的行为类似于其他语言中的 while
循环。你不需要使用两个分号:
sum := 1
for sum < 10 {
sum += sum
}
fmt.Println(sum)
英文:
It behaves like a while
in other languages. You don't need the two semicolons:
sum := 1
for sum < 10 {
sum += sum
}
fmt.Println(sum)
答案3
得分: 3
一个 for 循环有三个元素:初始化语句、条件检查和变量改变。
for <初始化语句>; <条件检查>; <变量改变>{
<实际内容>
}
- 初始化语句在循环开始时只执行一次。根据名称,它初始化某些东西(在很多情况下是一个你要迭代的变量)。如果省略,则不执行任何操作。
- 条件检查验证条件是否为真。如果不是,则循环停止。如果省略,则条件始终为真。
- 变量改变在每次循环迭代期间修改变量。大多数情况下,迭代变量会增加/减少,但你可以做任何你想做的事情。如果省略,则不执行任何操作。
在解释完之后,你可以看到在初始化和后置条件阶段,这个循环不执行任何操作。
在这里,你也不需要使用分号。这就足够了。
sum := 1
for sum < 10 {
sum += sum
}
你甚至可以像这样编写一个循环:for {}
,它将永远不会停止执行,或者像 while 循环一样做一些事情:
t := 10
for t > 0{
t--
}
请注意,在初始化、条件和改变阶段内,你可以使用多个表达式(不仅仅是一个)。因此,通过一个简单的 while 循环,你可以做一些像这样的事情:
for f1, f2, n := 1, 1, 10; n > 0; f1, f2, n = f2, f1 + f2, n - 1{
fmt.Println(f1)
}
它创建了 斐波那契数列,请参见 Go Playground。我展示这个不是因为这是编写它的最佳方式,而是因为它是可能的。
英文:
A for loop has three elements: initialization statement, condition check, and variable change.
for <initialization statement>; <condition check>; <variable change>{
<actual body>
}
- The initialization statement is executed only once when the loop starts. Based on the name, it initializes something (in a lot of cases a variable you iterate through). If it is omitted, then it does nothing
- The condition check verifies whether the condition evaluates to true. If it is not, the loop stops. If it is omitted, then it is always true.
- The variable change is modifying variables during each iteration of the loop. Most of the time, the iterated variable is increased/decreased, but you can do whatever you want. If it is omitted, it does nothing
After this explanation, you can see that this loop does nothing during your initialization and post condition phase.
You also do not need to use semicolons here. This will be enough.
sum := 1
for sum < 10 {
sum += sum
}
You can even write a loop like this: for {}
which will never stop executing, or do something like a while loop:
t := 10
for t > 0{
t--
}
Note that inside your initialization, condition, and change phase you can use many expressions (not just one). So with a simple while loop you can do something like:
for f1, f2, n := 1, 1, 10; n > 0; f1, f2, n = f2, f1 + f2, n - 1{
fmt.Println(f1)
}
which creates Fibonacci numbers—see Go Playground. Showing this not because this is the best way to write it, but rather because it is possible.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论