do while循环语句环

Keyboard Shortcuts?
Next menu item
Previous menu item
Previous man page
Next man page
Scroll to bottom
Scroll to top
Goto homepage
Goto search(current page)
Focus search box
Change language:
Brazilian Portuguese
Chinese (Simplified)
do-while 循环和 while
循环非常相似,区别在于表达式的值是在每次循环结束时检查而不是开始时。和一般的
while 循环主要的区别是 do-while
的循环语句保证会执行一次(表达式的真值在每次循环结束后检查),然而在一般的
while 循环中就不一定了(表达式真值在循环开始时检查,如果一开始就为
FALSE 则整个循环立即终止)。
do-while 循环只有一种语法:
以上循环将正好运行一次,因为经过第一次循环后,当检查表达式的真值时,其值为
FALSE($i 不大于 0)而导致循环终止。
资深的 C 语言用户可能熟悉另一种不同的 do-while
循环用法,把语句放在 do-while(0) 之中,在循环内部用
语句来结束执行循环。以下代码片段示范了此方法:
如果还不能立刻理解也不用担心。即使不用此“特性”也照样可以写出强大的代码来。自
PHP 5.3.0 起,还可以使用
来跳出循环。
Do-while loops can also be used inside other loops, for example:&?php$numbers = array();$array_size = 10;for ($i=0;$i&$array_size;$i++) { & & & do { & & & && $random = rand(1,1000);& && } while (($random % 2) == 1);& && $numbers[] = $random; }asort($numbers);echo '&pre&';print_r($numbers);echo '&/pre&';?&
There is one major difference you should be aware of when using the do--while loop vs. using a simple while loop:& And that is when the check condition is made.& In a do--while loop, the test condition evaluation is at the end of the loop.& This means that the code inside of the loop will iterate once through before the condition is ever evaluated.& This is ideal for tasks that need to execute once before a test is made to continue, such as test that is dependant upon the results of the loop.& Conversely, a plain while loop evaluates the test condition at the begining of the loop before any execution in the loop block is ever made. If for some reason your test condition evaluates to false at the very start of the loop, none of the code inside your loop will be executed.
What actually surprised me: There is no alternative-syntax or template syntax for a do-while-loop.So you can write &?phpwhile ($a & 10) :& & $a++;?&But this won't work:&?phpdo :& & $a++while ($a &= 10);?&
If you put multiple conditions in the while check, a do-while loop checks these conditions in order and runs again once it encounters a condition that returns true. This can be helpful to know when troubleshooting why a do-while loop isn't finishing. An (illustrative-only) example:&?php& & $numberOne = 0;& & do {& & & & echo $numberOne;& & & & $numberOne++;& & } while( $numberOne & 5 || incrementNumberTwo() );& & function incrementNumberTwo() {& & & & echo "function incrementNumberTwo called";& & & & return false;& & }& & ?&
The last example on this page is simply abuse of the `break` keyword.& Also, the suggestion to use `goto` if you don't understand the abuse of `break` is unsettling.& (See the manual page for `goto` for more than enough reasons not to use it.)The final example is generally better expressed using a typical if-else statement.&?phpif ($i & 5) {& & echo "i is not big enough";} else {& & $i *= $factor;& & if ($i &= $minimum_limit) {& & & echo "i is ok";& & & }}?&This version is easier to read and understand.& And arguments for code golf are invalid as well as this version is 3 lines shorter.In conclusion, although you can certainly write code that abuses the `break` keyword, you shouldn't in practice.& Keep the code easy to read and understand for whoever inherits your code.& And remember, code is for humans not computers.
I'm guilty of writing constructs without curly braces sometimes... writing the do--while seemed a bit odd without the curly braces ({ and }), but just so everyone is aware of how this is written with a do--while...a normal while:&?php&& while ( $isValid ) $isValid = doSomething($input);?&a do--while:&?php&& do $isValid = doSomething($input);&& while ( $isValid );?&Also, a practical example of when to use a do--while when a simple while just won't do (lol)... copying multiple 2nd level nodes from one document to another using the DOM XML extension&?php&& $fileDoc& = domxml_open_file('example.xml'); $fileRoot = $fileDoc-&document_element();&& $newDoc&& = domxml_new_doc('1.0'); $newRoot& = $newDoc-&create_element('rootnode');&& $newRoot& = $newDoc-&append_child($newRoot); $child = $fileRoot-&first_child(); do $newRoot-&append_child($child-&clone_node(true)); while ( $child = $child-&next_sibling() ); ?&
Example of Do while :-&?php$i = 0;echo 'This code will run at least once because i default value is 0.&br/&';do {echo 'i value is ' . $i . ', so code block will run. &br/&';++$i;} while ($i & 10);?&C++ do...while 循环
不像 for 和 while 循环,它们是在循环头部测试循环条件。do...while 循环是在循环的尾部检查它的条件。
do...while 循环与 while 循环类似,但是 do...while 循环会确保至少执行一次循环。
C++ 中 do...while 循环的语法:
statement(s);
}while( condition );
请注意,条件表达式出现在循环的尾部,所以循环中的 statement(s) 会在条件被测试之前至少执行一次。
如果条件为真,控制流会跳转回上面的 do,然后重新执行循环中的 statement(s)。这个过程会不断重复,直到给定条件变为假为止。
#include &iostream&
int main ()
// 局部变量声明
int a = 10;
// do 循环执行
cout && "a 的值:" && a &&
a = a + 1;
}while( a & 20 );
当上面的代码被编译和执行时,它会产生下列结果:
a 的值: 10
a 的值: 11
a 的值: 12
a 的值: 13
a 的值: 14
a 的值: 15
a 的值: 16
a 的值: 17
a 的值: 18
a 的值: 19
感谢您的支持,我会继续努力的!
扫码打赏,你说多少就多少
记住登录状态
重复输入密码不像for和while循环在循环顶部测试循环条件,C语言编程do ... while循环的检查其状态在循环的底部。
do... while循环类似于while循环,不同的是do ... while循环是保证至少执行一次。
C编程语言的do...while循环的语法是:
statement(s);
}while( condition );
注意,条件表达式出现在循环结束,所以在循环语句(多个)执行一次前的状态进行测试。
如果条件为真,控制流跳回起来做,并在循环语句(S)再次执行。重复这个过程,直到给定的条件为假。
#include &stdio.h&
int main ()
/* local variable definition */
int a = 10;
/* do loop execution */
printf(&value of a: %d
a = a + 1;
}while( a & 20 );
让我们编译和运行上面的程序,这将产生以下结果:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加3个群。
Java技术群:
(人数:2000,等级:LV5,免费:否)
MySQL/SQL群:
(人数:2000,等级:LV5,免费:否)
大数据开发群:
(人数:2000,等级:LV5,免费:否)
Python技术群:
(人数:2000,等级:LV5,免费:否)
人工智能深度学习:
(人数:2000,等级:LV5,免费:否)
测试工程师(新群):
(人数:1000,等级:LV1,免费:是)
前端技术群(新群):
(人数:1000,等级:LV1,免费:是)
C/C++技术(新群):
(人数:1000,等级:LV1,免费:是)
Node.js技术(新群):
(人数:1000,等级:LV1,免费:是)
PostgreSQL数据库(新群):
(人数:1000,等级:LV1,免费:否)
Linux技术:
(人数:2000,等级:LV5,免费:否)
PHP开发者:
(人数:2000,等级:LV5,免费:是)
Oracle数据库:
(人数:2000,等级:LV5,免费:是)
C#/ASP.Net开发者:
(人数:2000,等级:LV5,免费:是)
数据分析师:
(人数:1000,等级:LV1,免费:是)R语言,Matlab语言等技术Javascript中while和do-while循环用法详解
作者:用户
本文讲的是Javascript中while和do-while循环用法详解,
while 语句与 if 语句相似,都有条件来控制语句(或语句块)的执行,其语言结构基本相同:
while(conditions){
while 语句与 if 语句的不同之处在于
while 语句与 if 语句相似,都有条件来控制语句(或语句块)的执行,其语言结构基本相同:
while(conditions){
while 语句与 if 语句的不同之处在于:在if条件假设语句中,若逻辑条件表达式为真,则运行statements语句(或语句块),且仅运行一次;while 循环语句则是在逻辑条件表达式为真的情况下,反复执行循环体内包含的语句(或语句块)。
注意:while语句的循环变量的赋值语句在循环体前,循环变量更新则放在循环体内;for循环语句的循环变量赋值和更新语句都在for后面的小括号中,在编程中应注意二者的区别。
【例3-5】用while()循环来1+2+3 ... +98+99+100的值:
&title&计算1+2+3 ... +98+99+100的值&/title&
&script language="JavaScript" type="text/javascript"&
var total=0;
while(i&=100){
alert(total);
在某些情况下,while循环大括号内的statements语句(或语句块)可能一次也不被执行,因为对逻辑条件表达式的运算在执行statements语句(或语句块)之前。若逻辑条件表达式运算结果为假,则程序直接跳过循环而一次也不执行statements语句(或语句块)。
do...while循环
若希望至少执行一次statements语句(或语句块),可改用do…while语句,其基本语法结构如下:
}while(condition);
【例3-6】用do-while()循环来计算1+2+3 ... +98+99+100的值:
&title&计算1+2+3 ... +98+99+100的值&/title&
&script language="JavaScript" type="text/javascript"&
var total=0;
}while(i&=100);
alert(total);
for、while、do…while三种循环语句具有基本相同的功能,在实际编程过程中,应根据实际需要和本着简单易懂的原则来选择循环语句。
do while实例
&title&一个使用到do...while循环的Javascript示例&/title&
&script type="text/javascript"&
document.write( i + "&br&")
while (i &= 5)
i 等于 0。
循环首先会运行。
每循环一次,i 就会累加 1。
当 i 小于或等于 5 时,循环会继续运行。 javascrpt while
&script type="text/javascript"&
while (i &= 5)
document.write("数字是 " + i)
document.write("&br&")
i 等于 0。
当 i 小于或等于 5 时,循环将继续运行。
循环每运行一次,i 会累加 1。
Javascript示例代码解释:这个Javascript示例用到了do...while循环语句。
循环语句允许重复执行一行或数行代码,do后面跟的是重复执行的代码,while后面跟的是终止循环的条件。这个Javascript示例中,设一个变量为i,i初始值为0,i++表示每次重复执行后i的值就加1,终止循环条件为while (i &= 5),也就是说,一旦i的值大于5,就终止循环。该示例中,重复循环的语句是while循环里面的document.write语句。
从上面的实例我们可以看出来关于js while ,do while 它们之间的区别。
以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索结构
while循环用法详解、javascript while循环、java while循环的用法、while循环的用法、while循环语句用法,以便于您获取更多的相关知识。
弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率
40+云计算产品,6个月免费体验
稳定可靠、可弹性伸缩的在线数据库服务,全球最受欢迎的开源数据库之一
云服务器9.9元/月,大学必备
云栖社区(yq.aliyun.com)为您免费提供相关信息,包括
,所有相关内容均不代表云栖社区的意见!第1章 初始C程序
第2章 C中数据类型
第3章 C中的运算符
第4章 C程序结构语句
第5章 函数的秘密
第6章 数组
小明英语测验,&computer&这个单词再一次写错了,小明又决定抄10遍,但是小明想&我写一遍就能记住了,剩下的交给计算机帮我完成!&。
对于小明这个想法,就可以用到C语言中的do-while循环,一般形式如下:
do-while循环语句的语义是:它先执行循环中的执行代码块,然后再判断while中表达式是否为真,如果为真则继续循环;如果为假,则终止循环。因此,do-while循环至少要执行一次循环语句。其执行过程可用下图表示:
根据do-while语句的特点,小明的代码实现如下:
注意:使用do-while结构语句时,while括号后必须有分号。
某公司2014年在职人数为200人,以每年20%增长速度不断扩大招工规模,请使用do-while循环计算从2014开始至哪一年招工规模能够突破1000人。
在代码编辑中,第8行,第10行补全代码。
运行结果为: 到2023年招工规模突破1000人&
?不会了怎么办
1、是否改变了循环变量的值以避免死循环
2、while后的表达式是否忘记了分号?
#include &stdio.h&
int main()
int number=200;
int year=2014;
//这里是不是应该写点什么?
}while(number&1000)?
//这里好像缺点什么
printf(&到%d年招工规模突破1000人\n&, year);
请输入问题内容...
请输入笔记内容...
请验证,完成请求
由于请求次数过多,请先验证,完成再次请求
你发的评论可能会是问题?是否将他发到问答中
Copyright (C) 2018 imooc.com All Rights Reserved | 京ICP备 号-2
本次提问将花费2个积分
你的积分不足,无法发表
本次提问将花费2个积分
继续发表请点击 "确定"

我要回帖

更多关于 vb do while循环语句 的文章

 

随机推荐