C語言中while循環和do...while循環
在實際生活中經常會將同一件事情重複做很多次,比如在走路時,會重複使用左右腳,打乒乓球時,會重複揮拍的動作等。同樣在C語言中,也經常需要重複執行同一代碼塊,這時就需要使用循環語句。循環語句分爲while循環語句、do…while循環語句和for循環語句三種。今天,我們將重點講講while循環和e循環語句。
C語言中while循環和e循環while循環語句
while循環語句和條件判斷語句有些相似,都是根據條件判斷來決定是否執行大括號內的執行語句。區別在於,while語句會反覆地進行條件判斷,只要條件成立, { } 內的執行語句就會一直執行,直到條件不成立,while循環結束。while循環語句的具體語法格式如下:
while (循環條件)
{
執行語句
………
}
在上面的語法格式中, { } 中的執行語句被稱作循環體,循環體是否執行取決於循環條件。當循環條件爲true時,循環體就會被執行。循環體執行完畢時會繼續判斷循環條件,如條件仍爲true則會繼續執行,直到循環條件爲false時,整個循環過程纔會結束。
while循環的執行流程如下圖所示:
接下來通過while語句來實現1~4之間自然數的打印,如下例所示:
#include
int main()
{
int x = 1; //定義變量x,初始值爲1
while (x <= 4) // 循環條件
{
printf("x = %d", x);// 條件成立,打印x的值
x++; // x進行自增
}
return 0;
}
運行結果如圖3-14所示:
例中,x的初始值爲1,在滿足循環條件x <= 4的情況下,循環體會重複執行,打印x的值並讓x進行自增。因此打印結果中x的值分別爲1、2、3、4。值得注意的是,例3-7中的第8行代碼用於在每次循環時改變變量x的值,直到循環條件不成立,如果沒有這行代碼,整個循環會進入無限循環的'狀態,永遠不會結束。
do-while循環語句
do…while循環語句和while循環語句功能類似,二者的不同之處在於,while語句需要先判斷循環條件,然後根據判斷結果來決定是否執行大括號中的代碼,而do…while循環語句先要執行一次大括號內的代碼再判斷循環條件,其具體語法格式如下:
do
{
執行語句
………
} while(循環條件);
在上面的語法格式中,關鍵字do後面 { } 中的執行語句是循環體。do…while循環語句將循環條件放在了循環體的後面。這也就意味着,循環體會無條件執行一次,然後再根據循環條件來決定是否繼續執行。
do…while循環的執行流程如圖所示:
接下來使用do…while循環語句將例3-7進行改寫,如例所示。
#include
int main()
{
int x = 1; //定義變量x,初始值爲1
do
{
printf("x = %d", x);// 條件成立,打印x的值
x++; // x進行自增
} while (x <= 4); // 循環條件
return 0;
}
運行結果如圖所示:
兩個例題的運行結果一致,這說明do …while循環和while循環能實現同樣的功能。然而在程序運行過程中,這兩種語句還是有差別的。如果循環條件在循環語句開始時就不成立,那麼while循環的循環體一次都不會執行,而do…while循環的循環體還是會執行一次。若將例中的循環條件x<=4改爲x < 1,後一個例子會打印x=1,而前一個例子則什麼也不會打印。
相關文章
-
PHP循環語句用法之while循環
計算機對於重複性的工作是很有一套解決辦法的,以下本站小編整理的PHP循環語句用法之while循環的介紹吧,希望對大家有所幫助,更多信息請關注應屆畢業生網! 計算機機非常擅長的一件事就是自動地、重複地執行任務。如 -
c++ c語言while 循環語句入門基礎教程
我們來看While 循環語句,它跟 For 循環區別在於,For 循環一般知道循環次數,在第一行裏頭就指明瞭,而while循環一般不知道循環次數,下面我們來看一個練習;1、啓動 Geany1)點菜單“應用程序-編程-Geany”啓動 Geany ,新建一個 c++ -
英語作文Some advice on how to learn English well
Dear Linda,I know it isn’t easy to lea English,but I have some ideas that may said you couldn’t understand people who talked ,you can try to listen for the most important words,not every said you -
Which Mode of Travel Do You like爲題目的作文
Directions:For this part you are allowed thirty minutes to write a composition on the topic Which Mode of Travel Do You like? You should write noless than 150 words and base your composition on the outl -
Wuhan Heping Middle School 英語作文
Our shool, Wuhan Heping Middle School, lies in the center of Wuhan city. It was just built and has the perfect size. The school yard is two hundred meters long, one hundred meters wide, and is surroun -
PHP中while 循環的運用
引導語:while循環語句,計算機的一種基本循環模式。當滿足條件時進入循環,不滿足跳出。以下是小編整理的PHP中while 循環的運用,歡迎參考閱讀! PHP 循環在您編寫代碼時,經常需要反覆運行同一代碼塊。我們可以使用循環來 -
The Light Will Always Come大學英語作文
as the water frothed,and the sun did go,the dark brought forth a tear,i sat up high,upon my rock,no one would see me here.when all is lost,i stare alas,and listen to the oceans prayer,you must listen -
PHP的while 循環
PHP while 循環在指定條件爲 true 時執行代碼塊。就跟隨本站小編一起去了解下吧,想了解更多相關信息請持續關注我們應屆畢業生考試網!PHP 循環在您編寫代碼時,經常需要反覆運行同一代碼塊。我們可以使用循環來執行這樣 -
高中英語作文:Wonderful life in winter holiday
英語作文from the sixteenth of january to the seventh of february is our winter holiday. i think everybody did a lot of things in the winter holiday. but i didn’t. let you to listen to my story of -
Iceman Otzi Died With A Bellyache賞析
Otzi—or as he’s also known, the Iceman—was discovered frozen in the Alps in 1991 by two German tourists. The 5,300-year-old body has been analyzed in various ways and been the subject of numerous s