C語(yǔ)言中do-while語(yǔ)句的2種寫法示例
while循環(huán)和for循環(huán)都是入口條件循環(huán),即在循環(huán)的每次迭代之前檢查測(cè)試條件,所以有可能根本不執(zhí)行循環(huán)體中的內(nèi)容。C語(yǔ)言還有出口條件循環(huán)(exit-condition loop),即在循環(huán)的每次迭代之后檢查測(cè)試條件,這保證了至少執(zhí)行循環(huán)體中的內(nèi)容一次。這種循環(huán)被稱為do while循環(huán)。
看下面的例子:
#include <stdio.h> int main(void) { const int secret_code = 13; int code_entered; do { printf("To enter the triskaidekaphobia therapy club,\n"); printf("please enter the secret code number: "); scanf("%d", &code_entered); } while (code_entered != secret_code); printf("Congratulations! You are cured!\n"); return 0; }
運(yùn)行結(jié)果:
To enter the triskaidekaphobia therapy club,
please enter the secret code number: 12
To enter the triskaidekaphobia therapy club,
please enter the secret code number: 14
To enter the triskaidekaphobia therapy club,
please enter the secret code number: 13
Congratulations! You are cured!
使用while循環(huán)也能寫出等價(jià)的程序,但是長(zhǎng)一些,如程序清單6.16所示。
#include <stdio.h> int main(void) { const int secret_code = 13; int code_entered; printf("To enter the triskaidekaphobia therapy club,\n"); printf("please enter the secret code number: "); scanf("%d", &code_entered); while (code_entered != secret_code) { printf("To enter the triskaidekaphobia therapy club,\n"); printf("please enter the secret code number: "); scanf("%d", &code_entered); } printf("Congratulations! You are cured!\n"); return 0; }
下面是do while循環(huán)的通用形式:
do statement while ( expression );
statement可以是一條簡(jiǎn)單語(yǔ)句或復(fù)合語(yǔ)句。注意,do-while循環(huán)以分號(hào)結(jié)尾。
Structure of a =do while= loop=
do-while循環(huán)在執(zhí)行完循環(huán)體后才執(zhí)行測(cè)試條件,所以至少執(zhí)行循環(huán)體一次;而for循環(huán)或while循環(huán)都是在執(zhí)行循環(huán)體之前先執(zhí)行測(cè)試條件。do while循環(huán)適用于那些至少要迭代一次的循環(huán)。例如,下面是一個(gè)包含do while循環(huán)的密碼程序偽代碼:
do { prompt for password read user input } while (input not equal to password);
避免使用這種形式的do-while結(jié)構(gòu):
do { ask user if he or she wants to continue some clever stuff } while (answer is yes);
這樣的結(jié)構(gòu)導(dǎo)致用戶在回答“no”之后,仍然執(zhí)行“其他行為”部分,因?yàn)闇y(cè)試條件執(zhí)行晚了。
總結(jié)
到此這篇關(guān)于C語(yǔ)言中do-while語(yǔ)句的2種寫法的文章就介紹到這了,更多相關(guān)C語(yǔ)言do-while語(yǔ)句寫法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言菜鳥基礎(chǔ)教程之?dāng)?shù)據(jù)類型
在 C 語(yǔ)言中,數(shù)據(jù)類型指的是用于聲明不同類型的變量或函數(shù)的一個(gè)廣泛的系統(tǒng)。變量的類型決定了變量存儲(chǔ)占用的空間,以及如何解釋存儲(chǔ)的位模式。2017-10-10C語(yǔ)言示例講解while循環(huán)語(yǔ)句的用法
在不少實(shí)際問(wèn)題中有許多具有規(guī)律性的重復(fù)操作,因此在程序中就需要重復(fù)執(zhí)行某些語(yǔ)句。一組被重復(fù)執(zhí)行的語(yǔ)句稱之為循環(huán)體,C語(yǔ)言while語(yǔ)句可以是單個(gè)語(yǔ)句,也可以是一個(gè)語(yǔ)句塊,其條件可以是任意表達(dá)式,true是任意非零值,當(dāng)條件為真時(shí),循環(huán)進(jìn)行迭代2022-06-06OpenCV實(shí)現(xiàn)視頻綠幕背景替換功能的示例代碼
這篇文章主要介紹了如何利用OpenCV實(shí)現(xiàn)視頻綠幕背景替換功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)OpenCV有一定的幫助,感興趣的可以學(xué)習(xí)一下2023-02-02C語(yǔ)言實(shí)現(xiàn)隨機(jī)發(fā)撲克牌
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)隨機(jī)發(fā)撲克牌,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04C++算法實(shí)現(xiàn)leetcode 1252奇數(shù)值單元格數(shù)目
這篇文章為大家主要介紹了C++實(shí)現(xiàn)leetcode 1252奇數(shù)值單元格的數(shù)目題解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08C++?容器中map和unordered?map區(qū)別詳解
這篇文章主要為大家介紹了C++?容器中map和unordered?map區(qū)別示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11