成人免费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”之后,仍然執行“其他行為”部分,因為測試條件執行晚了。

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

2010-09-02 16:46:18

SQL刪除

2009-09-14 19:25:09

Ruby form

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卡死

2009-08-19 17:30:38

C#轉義字符

2021-05-27 10:57:01

TCP定時器網絡協議

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-07-31 14:04:11

C#時間比較大小

2009-11-23 14:09:53

PHP的foreach

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久久久久久久久6666 | 婷婷福利 | h视频在线播放 | 91在线导航 | 日韩成人在线播放 | 久久精品中文 | 日韩伦理一区二区 | 范冰冰一级做a爰片久久毛片 | 亚洲情综合五月天 | 男女免费在线观看视频 | 久久精品国产一区 | 欧美一级片免费看 | 国产免费va| 欧美视频在线播放 | 午夜免费网站 | 一区观看 | 毛片一区二区三区 | 欧美国产精品一区二区三区 | 国产在线高清 | 亚洲精品久久久久中文字幕二区 | 午夜激情国产 | 久久久婷婷 | 精品欧美一区二区三区久久久 | 美女亚洲一区 | 久久久久久久久久久久一区二区 | 欧美成人一区二区 | 精品1区2区| 免费观看日韩精品 | 日韩成人高清在线 | 国产精品久久久久久久久久尿 | 国产精品91视频 | 亚洲国产精品视频一区 | 国产无套一区二区三区久久 | 国产精品免费看 | 日韩精品 电影一区 亚洲 | 久久国产一区二区三区 | 嫩草黄色影院 | 国产欧美日韩一区 | 色婷婷av一区二区三区软件 | 国产高清美女一级a毛片久久w |