while是计算机的一种基本循环模式。当满足条件时进入循环,进入循环后,当条件不满足时,跳出循环。while语句的一般表达式为:while(表达式){循环体}。
典型循环
WHILE<条件>条件
<语句体>语句体
endwhile
dowhile<条件>条件
<语句体>语句体
loop
语法
Pascal
while<条件>do<语句>语句条件
意为当条件符合时,接着做下面的语句;不符合时,退出循环。
C
do<语句>while(<条件>);条件语句
while(<条件>)<语句>;语句条件
C++
while(<条件>)<语句>;语句条件
do<语句>while(<条件>);条件语句
Java
while(<条件>){<语句>}语句条件
do{<语句>}while(<条件>);条件语句
二者的区别是do-while最少会被执行一次。
循环中可以使用continue结束当前循环,回到循环开始处开始下一次循环。也可以用break跳出整个循环。
javascript
while(变量<=结束值){需执行的代码}
while(变量<=结束值){需执行的代码}
注意:除了<=,还可以使用其他的比较运算符。
do…while
do{需执行的代码}while(变量<=结束值)
do{需执行的代码}while(变量<=结束值)
JavaScript while循环的目的是为了反复执行语句或代码块。
只要指定条件为true,循环就可以一直执行代码块。
注意:do…while循环是while循环的变种。该循环程序在初次运行时会首先执行一遍其中的代码,然后当指定的条件为true时,它会继续这个循环。所以可以这么说,do…while循环为执行至少一遍其中的代码,即使条件为false,因为其中的代码执行后才会进行条件验证。
PHP
while循环是php中最简单的循环类型。它和C语言中的while表现得一样。语法如下:
while(expr){statement}
使用示例
C++
1 2 3 4 5 6 7 8 9 | inta=NULL; while(a<10) { a++;//自加 if(a>5)//不等while退出循环,直接判断循环 { break;//跳出循环 } } |
结果:结束后a的值为6。
Javascript
下面的例子定义了一个循环程序,这个循环程序的参数i的起始值为0。该程序会反复运行,直到i大于10为止。i的步进值为1。
1 2 3 4 5 6 7 8 9 | <html> <body> <scripttype="text/javascript"> vari=0while(i<=10) {document.write("Thenumberis"+i) document.write("<br/>")i=i+1} </script> </body> </html> |
结果
The number is0
The number is1
The number is2
The number is3
The number is4
The number is5
The number is6
The number is7
The number is8
The number is9
The number is10
PHP
1 2 3 4 5 6 7 8 9 10 11 | <?php $num=1; $aaa="10以内的偶数为:"; while($num<=10){ if($num<=10){ $aaa.=$sum.""; } $sum++; } echo$aaa; ?> |
下面两个例子完全一样,都显示数字1到10:
1 | <?php/*example1*/$i=1;while($i<=10){echo$i++;/*theprintedvaluewouldbe$ibeforetheincrement(post-increment)*/}/*example2*/$i=1;while($i<=10):print$i;$i++;endwhile;?> |
C#
1 2 3 4 5 | inti=1; while(i<=10) { Console.WriteLine("{0}",i++); } |
Java
1
1 2 3 | while(true) {i++; }/i无限累加,死循环/ |
2
1 | do{i++;}while(i<60)/在i小于60的情况下进行累加/ |
Basic
DimiAsInteger
i=1
DoWhilei<=5
Printi
i=i+1
Loop
向屏幕输出从1到5的正整数
AS
1 2 3 4 5 6 | vari=1; while(i<=1000) { i++; } trace(i) |
Pascal
k:=10;
WHILEk>0DOBEGINWriteln(k);
k:=k-1END;
从10到1倒序输入
英语
释义
1.when和while在引导状语从句时,都可表示“当……的时候”。如
Doctor Smith called when / while we were preparing dinner.
当我们正在做饭的时候,史密斯医生来访了。
While I was in Shanghai, I met with the pop star.
当我在上海时,碰巧遇到了那位流行歌星。
【注意】
(1)when表示“当……的时候”,从句中既可以用延续性动词,表示状态或时间段,也可以用非延续性动词,表示动作或时间点;while表示“当……的时候”、“在……期间”,从句中只能用延续性动词,表示状态或时间段。
例如:
It was snowing when we arrived at the station.
When he came in, we all stood up, smiling.
I was very fat when / while I was a child.
When / While she was typing, someone knocked at the door.
(2)在when或while所引导的状语从句中,如果从句的主语和主句的主语相同,且动词又是be动词时,从句的主语和be动词往往可以省略。
2.when和while都可以表示“尽管、虽然”,但when经常指描述的事实或结果事与愿违或出人意料,而while在语气上含有让步之意。例如:
The boy was watching TV when he should have gone to bed.
虽然那孩子该睡觉了,但他还在看电视。
The old couple prefer walking when they might take a taxi.
尽管可以坐出租车,那对老夫妇更愿意步行。
While I accept that he is not perfect, I do actually like the person.
尽管我承认他并不完美,但我确实真地喜欢他这个人。
While he loves his students, he is very strict with them.
虽然他爱他的学生,但对他们的要求也很严格。
while的其它含义和用法:
(1)然而,可是
I like coffee, while my sister likes tea.
我喜欢咖啡,而我姐姐喜欢喝茶。
I was preparing for the exam while they were chatting.
我在准备考试, 可他们却在闲聊。
(2)只要
While there is water, there is hope of life.
只要有水,就有生存的希望。
I’ll offer help to those poor children while I’m alive.
只要我活着, 就要帮助那些贫困的孩子。
时态标志词
when和while引导的句子可以作过去进行时的时间状语,区别在于:when引导的时间状语从句可以接一段时间或者一个具体的时间点while引导的时间状语从句只能接一个时间点。如:
通常用在事情同时发生的情况,主句和从句都用过去进行时
Mother was cooking the meal while i was doing my homework.
当妈妈在做饭的时候,我正在做功课。
表示同时
表示而…的意味时,主句和从句的时态是一致的,即可以都是一般现在时,又可以都是一般过去时,如:
I like playing football while Jim likes playing basketball.
我喜欢踢足球,然而吉姆喜欢打篮球。
Yesterday my parents went to the cinema while I stayed at home.
昨天爸爸,妈妈去看电影了,而我呆在家里在。
该文章由作者:【杜老师】发布,本站仅提供存储、如有版权、错误、违法等相关信息请联系,本站会在1个工作日内进行整改,谢谢!