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#

2009-08-17 17:28:23

C#转义字符

2009-08-19 17:30:38

C#转义字符

2021-05-27 10:57:01

TCP定时器网络协议

2010-10-11 10:31:51

MySQL分区

2013-05-27 14:31:34

Hadoop 2.0

2009-07-31 14:04:11

C#时间比较大小

2021-11-16 06:55:36

Linux字符设备

2010-01-12 10:57:16

C++的复杂性

2023-12-13 08:47:13

编程语言编译型解释型

2010-08-06 09:38:11

Flex读取XML

2010-06-07 17:41:42

Sendmail 配置

2010-07-14 16:28:58

配线架

2023-03-29 13:06:36

点赞
收藏

51CTO技术栈公众号