本文是小编为大家收集整理的关于do while循环的目的是什么?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我知道该怎么办,以及它如何与while循环合作,但是while循环代码不会相同,无论是在那里吗?
推荐答案
考虑以下内容:
while(condition){ myFunction(); }
和
do{ myFunction(); }while(condition);
第二个表单至少执行myFunction(),然后检查condition!为了使用while循环进行写作:
myFunction(); while(condition){ myFunction(); }
其他推荐答案
while和do-while语句之间的最大区别是,是否至少执行一次.
while(false) printf("print!");
使用此语句,该程序将永远不会打印字符串.但是,
do{ printf("print!"); }while(false);
在此语句中,该程序将一次打印字符串.
但是很多人不建议在do-nile语句中使用 - 因为可以在陈述时代替.这并不意味着do-wile陈述是严重有害的,但很少使用.
实际上,对于循环是最安全和建议的方式,因为程序员可以轻松处理迭代号,循环条件和增加变量.因此,如果您没有任何特定原因,则需要在循环时使用,只需用于循环.
其他推荐答案
即使条件失败,必须至少执行任务一次时,请使用do-while()构造. 在您的任务仅在某些条件成功下执行时使用while().
问题描述
I know what do does, and how it cooperates with the while loop, but won't a while loop code be the same, whether or not the do is there?
推荐答案
Consider the following:
while(condition){ myFunction(); }
and
do{ myFunction(); }while(condition);
The second form executes myFunction() at least once then checks the condition! To do so with a while loop you've to write:
myFunction(); while(condition){ myFunction(); }
其他推荐答案
The biggest difference between while and do-while statement is, whether it is executed at least one time or not.
while(false) printf("print!");
with this statement, the program will never print the string. However,
do{ printf("print!"); }while(false);
with this statement, the program will print the string once.
But many people don't recommend to use do-while statement--because it can be substituted with while statement. It doesn't mean that do-while statement is critically harmful, but it is rarely used.
In fact, for loop is the most safe and recommended way because the programmer can handle the iteration number, loop conditions, and increasing variables easily. So if you don't have any specific reason that you have to use while loop, just use for loop.
其他推荐答案
Use do-while() construct when you have to get your task executed at least once even if condition fails. Use while() when you your task to be executed only on certain condition success.