当前位置: 首页> 财经> 产业 > C primer plus 6.8 出口条件循环: do while

C primer plus 6.8 出口条件循环: do while

时间:2025/7/11 14:24:57来源:https://blog.csdn.net/xnglan/article/details/141189975 浏览次数:0次

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

do while 循环

        下面是do while 循环的通用语句:

         do

                statement

        while  (expression);

        一般注解;

                do while 语句创建一个循环,在expression为假或0之前重复执行循环体的内容,do while 语句是一种出口条件循环,即在执行完循环体后才根据测试条件决定是否再次执行循环。所以该循环至少执行一次。

                其中,statement可以是一条简单语句或复杂语句。

               注意:do while 循环以分号结尾

#include<stdio.h>
int main(void)
{conset 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:13

Congratulations!You are cured!

关键字:C primer plus 6.8 出口条件循环: do while

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: