对于主场迎战而在C编程? [英] For vs. while in C programming?

查看:107
本文介绍了对于主场迎战而在C编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有3个回路在C:,而做的,而。什么是它们之间的区别?
例如,它似乎几乎所有的,而语句可以通过语句代替,对不对?那么,什么是使用的好处,而

There are 3 loops in C: for, while, and do-while. What's the difference between them? For example, it seems nearly all while statements can be replaced by for statements, right? Then, what's the advantage using while?

推荐答案

A while循环总是首先评估条件。

A while loop will always evaluate the condition first.

while (condition) {
  //gets executed after condition is checked
}

A DO / while循环总会执行
   在code在做{} 块第一
   然后计算条件。

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); 

A 循环可让你发起一个计数器变量,一个检查条件和方式来增加您的柜台都在一行中。

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

为的的之间的主要区别
  和,而的是一个问题
  语用学:我们通常使用
  有迭代已知数量,
  并使用,而构造时,
  在不知道的迭代次数
  提前。在,而 VS 做...而
  问题是语用学的还
  第二次执行这些指令一旦
  在启动,之后它的行为
  就像简单的一段时间。

The main difference between the for's and the while's is a matter of pragmatics: we usually use for when there is a known number of iterations, and use while constructs when the number of iterations in not known in advance. The while vs do ... while issue is also of pragmatics, the second executes the instructions once at start, and afterwards it behaves just like the simple while.

有关循环,因为它们是简练的特别好。为了使这种循环


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++;
}

在这种情况下,只是更多的东西跟上和计数++; 可能迷失在逻辑。这可能最终取决于其中计数被递增是麻烦,以及是否应该之前或循环的逻辑后得到增加。随着循环,你的计数器变量总是在循环,它增加了一些统一到code的下一次迭代前递增。

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.

为了完整起见,它可能是有意义的谈继续在这里报表时派上用场在做循环处理的时候。

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 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 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循环中,继续计算的第三部分前pression'的(第一部分,第二部分,第三部分);与此相反,在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.

这篇关于对于主场迎战而在C编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆