For vs. while 在 C 编程中? [英] For vs. while in C programming?
问题描述
C 中有三个循环:for
、while
和 do-while
.它们有什么区别?
There are three loops in C: for
, while
, and do-while
. What's the difference between them?
例如,似乎几乎所有的 while
语句都可以替换为 for
语句,对吗?那么,使用while
有什么好处?
For example, it seems nearly all while
statements can be replaced by for
statements, right? Then, what's the advantage using while
?
推荐答案
while 循环 将始终首先评估条件.
A while loop will always evaluate the condition first.
while (condition) {
//gets executed after condition is checked
}
do/while 循环 将始终执行首先是 do{}
块中的代码然后评估条件.
A do/while loop will always execute
the code in the do{}
block first
and then evaluate the condition.
do {
//gets executed at least once
} while (condition);
for 循环允许您在一行中启动计数器变量、检查条件和增加计数器的方法.
A for loop allows you to initiate a counter variable, a check condition, and a way to increment your counter all in one line.
for (int x = 0; x < 100; x++) {
//executed until x >= 100
}
归根结底,它们仍然是循环,但它们在执行方式方面提供了一定的灵活性.
At the end of the day, they are all still loops, but they offer some flexibility as to how they are executed.
这里很好地解释了使用每种不同类型的循环背后的推理,可能有助于澄清问题.谢谢 clyfe
Here is a great explanation of the reasoning behind the use of each different type of loop that may help clear things up. Thanks clyfe
for
的主要区别而 while
是一个问题语用学:我们通常使用 for
的时候有一个已知的迭代次数,并使用 while
构造,当迭代次数未知进步.while
vs do ... while
问题也是语用学的,second 执行一次指令在开始时,然后它的行为就像简单的while一样.
The main difference between the
for
's and thewhile
's is a matter of pragmatics: we usually usefor
when there is a known number of iterations, and usewhile
constructs when the number of iterations in not known in advance. Thewhile
vsdo ... while
issue is also of pragmatics, the second executes the instructions once at start, and afterwards it behaves just like the simple while.
For 循环特别好,因为它们很简洁.为了这个 for 循环:
For loops are especially nice because they are concise. In order for this for loop:
for (int x = 0; x < 100; x++) {
//executed until x >= 100
}
要编写为 while 循环,您必须执行以下操作:
to be written as a while loop, you'd have to do the following:
int count = 0;
while (count < 100) {
//do stuff
count++;
}
在这种情况下,只有更多的东西要跟上,count++;
可能会丢失在逻辑中.这最终可能会很麻烦,这取决于 count
在哪里增加,以及它是否应该在循环逻辑之前或之后增加.使用 for
循环,您的计数器变量总是在循环的下一次迭代之前递增,这为您的代码增加了一些统一性.
In this case, there's just more stuff to keep up with and the count++;
could get lost in the logic. This could end up being troublesome depending on where count
gets incremented, and whether or not it should get incremented before or after the loop's logic. With a for
loop, your counter variable is always incremented before the next iteration of the loop, which adds some uniformity to your code.
为了完整起见,在这里讨论 break
和 continue
语句可能很有意义,它们在进行循环处理时会派上用场.
For the sake of completeness, it's probably meaningful to talk about break
and continue
statements here which come in handy when doing loop processing.
break 将立即终止当前循环,不再执行迭代.
break will instantly terminate the current loop and no more iterations will be executed.
//will only run "do stuff" twice
for (int x = 0; x < 100; x++) {
if (x == 2) {
break;
}
//do stuff
}
continue 将终止当前迭代并继续进行下一个迭代.
continue will terminate the current iteration and move on to the next one.
//will run "do stuff" until x >= 100 except for when x = 2
for (int x = 0; x < 100; x++) {
if (x == 2) {
continue;
}
//do stuff
}
请注意,在 for 循环中,continue
计算 for (part1; part2; part3)
的 part3
表达式;相反,在 while 循环中,它只是跳转以重新评估循环条件.
Note that in a for loop, continue
evaluates the part3
expression of for (part1; part2; part3)
; in contrast, in a while loop, it just jumps to re-evaluate the loop condition.
这篇关于For vs. while 在 C 编程中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!