c语言do while循环语句举例 C语言while、do-while循环详解

\u5173\u4e8eC\u8bed\u8a00\u4e2d\u7684do while\u8bed\u53e5\u4e0ewhile\u8bed\u53e5

\u4e00\u822c\u5728\u8fd0\u884c\u5faa\u73af\u8bed\u53e5\u7684\u65f6\u5019\uff0c\u4f1a\u4fdd\u8bc1\u5224\u65ad\u6761\u4ef6\u4e00\u76f4\u5728\u505a\u6539\u53d8\uff0c\u6240\u4ee5\u5728\u67d0\u4e2a\u65f6\u523b\u5bfc\u81f4\u6761\u4ef6\u4e3a\u5047\u800c\u9000\u51fa\u5faa\u73af\u3002
\u5982\uff1a
int n=10;while(n--) //\u5f53n--\u4e3a0\u7684\u65f6\u5019\u9000\u51fa\u5faa\u73af{ printf("n=[%d]\n");}\u800c\u6b7b\u5faa\u73af\uff0c\u5c31\u662f\u7531\u4e8e\u4eba\u4e3a\u7f16\u5199\u5931\u8bef\u6216\u7a0b\u5e8f\u9700\u8981\u5bfc\u81f4\u5faa\u73af\u6761\u4ef6\u4e00\u76f4\u4e3a\u771f\uff0c\u8fd9\u6837\u7a0b\u5e8f\u4f1a\u6c38\u8fdc\u6267\u884c\u5faa\u73af\u4e2d\u7684\u8bed\u53e5\uff0c\u5982\uff1a
int n=10;while(n++) //\u6b64\u65f6n++\u6c38\u8fdc\u4e0d\u7b49\u4e8e0\uff0c\u5219\u6761\u4ef6\u6c38\u8fdc\u4e3a\u771f\uff0c\u6b7b\u5faa\u73af{ printf("n=[%d]\n");}

这篇文章主要给大家介绍了关于C语言中do-while语句的2种写法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
while循环和for循环都是入口条件循环,即在循环的每次迭代之前检查测试条件,所以有可能根本不执行循环体中的内容。C语言还有出口条件循环(exit-condition loop),即在循环的每次迭代之后检查测试条件,这保证了至少执行循环体中的内容一次。这种循环被称为do while循环。
看下面的例子:
#include <stdio.h>
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 <stdio.h>
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;
}

int i=1,s=0;
while(i<101)
s+=i++;
pintf("Sum=%d\n",s);

扩展阅读:do while用法举例 ... do while死循环写法 ... c语言跳出while 1 循环 ... c语言do while循环排大小 ... vb do while循环语句 ... do while循环语句结束 ... do while循环语句用法 ... c语言do while循环1到10 ... c语言do while循环例子 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网