自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

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

看下面的例子:

  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; 

運(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à)的程序,但是長一些,如程序清單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循環(huán)的通用形式:

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

statement可以是一條簡單語句或復(fù)合語句。注意,do-while循環(huán)以分號(hào)結(jié)尾。

 

C語言的do-while語句的兩種寫法
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)。例如,下面是一個(gè)包含do while循環(huán)的密碼程序偽代碼:

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

避免使用這種形式的do-while結(jié)構(gòu):

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

這樣的結(jié)構(gòu)導(dǎo)致用戶在回答“no”之后,仍然執(zhí)行“其他行為”部分,因?yàn)闇y試條件執(zhí)行晚了。

責(zé)任編輯:未麗燕 來源: 今日頭條
相關(guān)推薦

2010-09-02 16:46:18

SQL刪除

2009-09-14 19:25:09

Ruby form

2024-02-26 12:13:32

C++開發(fā)編程

2022-10-28 07:38:06

Javawhile循環(huán)

2022-01-17 21:08:54

Python 循環(huán)結(jié)構(gòu)

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-17 17:28:23

C#轉(zhuǎn)義字符

2009-08-19 17:30:38

C#轉(zhuǎn)義字符

2021-05-27 10:57:01

TCP定時(shí)器網(wǎng)絡(luò)協(xié)議

2010-10-11 10:31:51

MySQL分區(qū)

2013-05-27 14:31:34

Hadoop 2.0

2010-01-12 10:57:16

C++的復(fù)雜性

2009-07-31 14:04:11

C#時(shí)間比較大小

2009-11-23 14:09:53

PHP的foreach

2021-11-16 06:55:36

Linux字符設(shè)備

2010-08-06 09:38:11

Flex讀取XML

2010-06-07 17:41:42

Sendmail 配置

2010-07-14 16:28:58

配線架
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)