最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言中do-while語句的2種寫法示例

 更新時間:2020年07月24日 08:58:42   作者:Spiritinme  
這篇文章主要給大家介紹了關于C語言中do-while語句的2種寫法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

while循環(huán)和for循環(huán)都是入口條件循環(huán),即在循環(huán)的每次迭代之前檢查測試條件,所以有可能根本不執(zhí)行循環(huán)體中的內容。C語言還有出口條件循環(huán)(exit-condition loop),即在循環(huán)的每次迭代之后檢查測試條件,這保證了至少執(zhí)行循環(huán)體中的內容一次。這種循環(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;
}

運行結果:

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)也能寫出等價的程序,但是長一些,如程序清單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可以是一條簡單語句或復合語句。注意,do-while循環(huán)以分號結尾。

Structure of a =do while= loop=

do-while循環(huán)在執(zhí)行完循環(huán)體后才執(zhí)行測試條件,所以至少執(zhí)行循環(huán)體一次;而for循環(huán)或while循環(huán)都是在執(zhí)行循環(huán)體之前先執(zhí)行測試條件。do while循環(huán)適用于那些至少要迭代一次的循環(huán)。例如,下面是一個包含do while循環(huán)的密碼程序偽代碼:

do
{
 prompt for password
 read user input
} while (input not equal to password);

避免使用這種形式的do-while結構:

do
{
 ask user if he or she wants to continue
 some clever stuff
} while (answer is yes);

這樣的結構導致用戶在回答“no”之后,仍然執(zhí)行“其他行為”部分,因為測試條件執(zhí)行晚了。

總結

到此這篇關于C語言中do-while語句的2種寫法的文章就介紹到這了,更多相關C語言do-while語句寫法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

连州市| 彰化县| 博野县| 慈利县| 宁晋县| 尉犁县| 宁蒗| 安庆市| 开化县| 三河市| 克拉玛依市| 南和县| 甘孜县| 政和县| 读书| 大竹县| 马鞍山市| 长春市| 澄江县| 溧水县| 民勤县| 宜昌市| 贵德县| 金门县| 六盘水市| 惠安县| 余庆县| 故城县| 莱阳市| 临泽县| 顺义区| 邵东县| 阳曲县| 临清市| 忻城县| 枞阳县| 邹平县| 赣州市| 尼勒克县| 农安县| 涿州市|