while

while

循環語句及英文單詞
while是計算機的一種基本循環模式。當滿足條件時進入循環,進入循環後,當條件不滿足時,跳出循環。while語句的一般表達式為:while(表達式){循環體}。
    中文名:while語句 外文名:while statement 所屬領域:編程語言 一般表達式: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

vari=0while(i<=10)

{document.write("Thenumberis"+i)

document.write("
")i=i+1}

結果

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

$num=1;

$aaa="10以内的偶數為:";

while($num<=10){

if($num<=10){

$aaa.=$sum."";

}

$sum++;

}

echo$aaa;

?>

下面兩個例子完全一樣,都顯示數字1到10:

1

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.

昨天爸爸,媽媽去看電影了,而我呆在家裡在。

上一篇:凍品

下一篇:冗餘

相關詞條

相關搜索

其它詞條