为什么,while或while循环? [英] Why, do while or while loop?

查看:110
本文介绍了为什么,while或while循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于两者都是控制流,所以为什么要有两个目的相同的语句(2)?它们有何不同?

Since both are control flows, why have two(2) statements that have the same purpose? How do they differ?

do
{
//code here..
} while (bool == false);


while (bool == false);
{
//code here..
}



我的意思是,我所看到的唯一区别是,一个没有做",而另一个没有.叹气.



I mean sh*t, the only difference I see, is that one doesn''t have a ''do'' while the other one does. Sigh.

推荐答案

这很简单.它们都是在条件为真时执行的两个循环.一个检查循环之前的条件,另一个检查循环之后的条件.这导致一个为0或多次,而另一个为1或多次.就这样.因此,您可以根据此要求选择其中之一.
It''s quite simple. They are both loops that execute while a condition is true. One checks the condition before the loop, the other checks it after the loop. This leads to one being 0 or more times, and the other 1 or more times. That''s all. So you pick one of them based on that requirement.


用于不同的逻辑.在第一种情况下,一开始不会检查条件,但在第二种情况下会检查条件.您的示例将无法编译(bool是保留字,必须是变量).现在,考虑它是一个变量,例如Condition.然后(Condition == false)毫无意义,请使用(!Condition).您试图从Boolean制作Boolean,这没有任何意义.

现在,您的循环是无限的.为什么?在现实生活中,应该在每个周期中重新计算条件.当您这样做时,您将看到区别.例如,使用fist构造,您无法使循环运行0次-始终运行第一次迭代,而第二次迭代则可以运行,因为条件可能为true或false,具体取决于其计算.

现在,发布这样的问题毫无意义.首先阅读样本.提出这样的问题是非常无效的,会浪费您的时间以及其他时间.''

—SA
For different logics. Condition is not checked in the very beginning in first cases, but is checked in the second case. Your example will not compile (bool is the reserved word, must be a variable). Now, consider it is a variable, say Condition. Then (Condition == false) make no sense, use (!Condition). You''re trying to make Boolean from a Boolean, makes no sense.

Now, you cycles are infinite. Why? In real life the condition should be re-calculated in each cycle. When you do that, you will see the difference. For example, with the fist construct you cannot make a loop running 0 times — first iteration always run, with the second one you can, as condition may be true or false, depending in its calculation.

Now, posting question like that makes little or no sense. Read the manual first, with sample. Asking such question is very ineffective and waste your time, as well as others''.

—SA


我刚刚找到了这个...
如果表达式为假,则while循环可能永远不会执行一条语句,而do while将始终至少执行一次该语句.

听起来很合理,因为您的第一个示例将执行某项操作并在此之后检查一个表达式,而您的第一个示例首先将检查其是否需要执行某些操作.

这是我找到的链接: http://cplus.about.com/od/learningc/ss/clessonfive_4 .htm [ ^ ]
I just found this...
A while loop might never execute a statement if the expression is false but a do while will always execute the statement at least once.

Sounds logic, since your first example does something and checks an expression after that while your first example first checks if it needs to do something.

Here is the link I found: http://cplus.about.com/od/learningc/ss/clessonfive_4.htm[^]


这篇关于为什么,while或while循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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