网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C语言中怎么使用do-while语句

这篇文章主要介绍“C语言中怎么使用do-while语句”,在日常操作中,相信很多人在C语言中怎么使用do-while语句问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C语言中怎么使用do-while语句”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联是一家专注于成都网站制作、成都网站设计、外贸营销网站建设与策划设计,宜宾网站建设哪家好?创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:宜宾等地区。宜宾做网站价格咨询:18982081108

while循环和for循环都是入口条件循环,即在循环的每次迭代之前检查测试条件,所以有可能根本不执行循环体中的内容。C语言还有出口条件循环(exit-condition  loop),即在循环的每次迭代之后检查测试条件,这保证了至少执行循环体中的内容一次。这种循环被称为do while循环。

看下面的例子:

#include  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循环也能写出等价的程序,但是长一些,如程序清单6.16所示。

#include  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循环的通用形式:

do     statement while ( expression );

statement可以是一条简单语句或复合语句。注意,do-while循环以分号结尾。

Structure of a =do while= loop=do-while循环在执行完循环体后才执行测试条件,所以至少执行循环体一次;而for循环或while循环都是在执行循环体之前先执行测试条件。do  while循环适用于那些至少要迭代一次的循环。例如,下面是一个包含do while循环的密码程序伪代码:

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”之后,仍然执行“其他行为”部分,因为测试条件执行晚了。

到此,关于“C语言中怎么使用do-while语句”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


名称栏目:C语言中怎么使用do-while语句
链接地址:http://bjjierui.cn/article/pccdeh.html

其他资讯