成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C語言的do-while語句的兩種寫法

開發 后端
while循環和for循環都是入口條件循環,即在循環的每次迭代之前檢查測試條件,所以有可能根本不執行循環體中的內容。C語言還有出口條件循環(exit-condition loop),即在循環的每次迭代之后檢查測試條件,這保證了至少執行循環體中的內容一次。

while循環和for循環都是入口條件循環,即在循環的每次迭代之前檢查測試條件,所以有可能根本不執行循環體中的內容。C語言還有出口條件循環(exit-condition loop),即在循環的每次迭代之后檢查測試條件,這保證了至少執行循環體中的內容一次。這種循環被稱為do while循環。

看下面的例子:

  1. #include <stdio.h> 
  2. int main(void) 
  3.     const int secret_code = 13; 
  4.     int code_entered; 
  5.  
  6.     do 
  7.     { 
  8.         printf("To enter the triskaidekaphobia therapy club,\n"); 
  9.         printf("please enter the secret code number: "); 
  10.         scanf("%d", &code_entered); 
  11.     } while (code_entered != secret_code); 
  12.     printf("Congratulations! You are cured!\n"); 
  13.  
  14.     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循環也能寫出等價的程序,但是長一些,如程序清單6.16所示。

  1. #include <stdio.h> 
  2. int main(void) 
  3.     const int secret_code = 13; 
  4.     int code_entered; 
  5.  
  6.     printf("To enter the triskaidekaphobia therapy club,\n"); 
  7.     printf("please enter the secret code number: "); 
  8.     scanf("%d", &code_entered); 
  9.     while (code_entered != secret_code) 
  10.     { 
  11.         printf("To enter the triskaidekaphobia therapy club,\n"); 
  12.         printf("please enter the secret code number: "); 
  13.         scanf("%d", &code_entered); 
  14.     } 
  15.     printf("Congratulations! You are cured!\n"); 
  16.  
  17.     return 0; 

下面是do while循環的通用形式:

  1. do 
  2.     statement 
  3. while ( expression ); 

statement可以是一條簡單語句或復合語句。注意,do-while循環以分號結尾。

 

C語言的do-while語句的兩種寫法
Structure of a =do while= loop=

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

  1. do 
  2.     prompt for password 
  3.     read user input 
  4. } while (input not equal to password); 

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

  1. do 
  2.    ask user if he or she wants to continue 
  3.    some clever stuff 
  4. } while (answer is yes); 

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

責任編輯:未麗燕 來源: 今日頭條
相關推薦

2009-09-14 19:25:09

Ruby form

2010-09-02 16:46:18

SQL刪除

2024-02-26 12:13:32

C++開發編程

2022-10-28 07:38:06

Javawhile循環

2022-01-17 21:08:54

Python 循環結構

2009-07-22 15:50:36

J#和C++ASP.NET

2010-08-24 09:00:43

JavaC#

2024-12-10 08:41:12

語句if卡死

2021-05-27 10:57:01

TCP定時器網絡協議

2009-08-19 17:30:38

C#轉義字符

2009-08-17 17:28:23

C#轉義字符

2010-10-11 10:31:51

MySQL分區

2013-05-27 14:31:34

Hadoop 2.0

2010-01-12 10:57:16

C++的復雜性

2009-11-23 14:09:53

PHP的foreach

2009-07-31 14:04:11

C#時間比較大小

2011-04-06 12:41:41

Java異常

2009-06-25 13:43:00

Buffalo AJA

2010-10-21 16:24:18

sql server升

2024-06-06 08:32:52

.NET框架代碼
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产999精品久久久 精品三级在线观看 | 91观看| 国产精品久久一区 | 狠狠干夜夜草 | 久久九 | 福利网址 | 日韩国产一区二区三区 | 午夜看片网站 | 中文字幕亚洲一区二区三区 | 91中文字幕在线观看 | 亚洲精品区 | 在线观看日韩av | 久久99精品久久久久久国产越南 | www.亚洲成人网 | 色欧美片视频在线观看 | 黄色片大全在线观看 | 亚洲精品片 | 国产午夜精品久久久久 | 国产日韩欧美在线观看 | 亚洲一区中文字幕 | 亚洲欧美日韩精品 | 日韩视频一区二区 | 本道综合精品 | 国精产品一区一区三区免费完 | 国产精品日韩一区二区 | 国产99久久精品一区二区永久免费 | 国产一区久久 | 狠狠干五月天 | 日韩在线一区二区 | 每日更新av | 日韩电影一区 | 亚洲成人www | 成人免费一区二区三区牛牛 | www.夜夜草| 欧美日高清视频 | 特级丰满少妇一级aaaa爱毛片 | 国产欧美日韩一区二区三区 | 国产精品久久av | 亚洲一一在线 | 欧美在线视频不卡 | 精品久久久一区二区 |