while do和while的区别 C语言中while和do–while循环的主要区别是什么?

while\u548cdo-while\u7684\u4f7f\u7528\u65b9\u6cd5

while\u5faa\u73af\u5f00\u59cb\u540e\uff0c\u5148\u5224\u65ad\u6761\u4ef6\u662f\u5426\u6ee1\u8db3\uff0c\u5982\u679c\u6ee1\u8db3\u5c31\u6267\u884c\u5faa\u73af\u4f53\u5185\u7684\u8bed\u53e5\uff0c\u6267\u884c\u5b8c\u6bd5\u540e\u518d\u56de\u6765\u5224\u65ad\u6761\u4ef6\u662f\u5426\u6ee1\u8db3\uff0c\u5982\u6b64\u65e0\u9650\u91cd\u590d\uff1b\u76f4\u5230\u6761\u4ef6\u4e0d\u6ee1\u8db3\u65f6\uff0c\u6267\u884cwhile\u5faa\u73af\u540e\u8fb9\u7684\u8bed\u53e5\u3002\u7b80\u5355\u6765\u8bb2\u5c31\u662f\u8bf4while\u5faa\u73af\u662f\u5148\u5224\u65ad\u540e\u5faa\u73af\uff0c \u5224\u65ad\u5982\u679c\u6ee1\u8db3\u6761\u4ef6\u8fdb\u5165\u5faa\u73af \u672c\u6b21\u5faa\u73af\u540e\u518d\u6b21\u5224\u65ad \u3002\u4e3e\u4e2a\u4f8b\u5b50:

do-while\u5faa\u73af\u4e0ewhile\u5faa\u73af\u7684\u4e0d\u540c\u5728\u4e8e:\u5b83\u5148\u6267\u884c\u5faa\u73af\u4e2d\u7684\u8bed\u53e5,\u7136\u540e\u518d\u5224\u65ad\u8868\u8fbe\u5f0f\u662f\u5426\u4e3a\u771f, \u5982\u679c\u4e3a\u771f\u5219\u7ee7\u7eed\u5faa\u73af\uff1b\u5982\u679c\u4e3a\u5047, \u5219\u7ec8\u6b62\u5faa\u73af\u3002\u56e0\u6b64, do-while\u5faa\u73af\u81f3\u5c11\u8981\u6267\u884c\u4e00\u6b21\u5faa\u73af\u8bed\u53e5\u3002 \u7b80\u5355\u6765\u8bb2\u5c31\u662f\u8bf4while\u5faa\u73af\u662f\u5148\u5faa\u73af\u540e\u5224\u65ad \u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a

\u603b\u7ed3\uff1awhile\u5faa\u73af\u662f\u5148\u5224\u65ad\u540e\u5faa\u73af \uff0c\u800cdo\u2013while\u5faa\u73af\u662f\u5148\u5faa\u73af\u540e\u5224\u65ad\u3002
\u6269\u5c55\u8d44\u6599\uff1a
while\u662f\u8ba1\u7b97\u673a\u7684\u4e00\u79cd\u57fa\u672c\u5faa\u73af\u6a21\u5f0f\u3002\u5f53\u6ee1\u8db3\u6761\u4ef6\u65f6\u8fdb\u5165\u5faa\u73af\uff0c\u8fdb\u5165\u5faa\u73af\u540e\uff0c\u5f53\u6761\u4ef6\u4e0d\u6ee1\u8db3\u65f6\uff0c\u6267\u884c\u5b8c\u5faa\u73af\u4f53\u5185\u5168\u90e8\u8bed\u53e5\u540e\u518d\u8df3\u51fa\uff08\u800c\u4e0d\u662f\u7acb\u5373\u8df3\u51fa\u5faa\u73af\uff09\u3002while\u8bed\u53e5\u7684\u4e00\u822c\u8868\u8fbe\u5f0f\u4e3a\uff1awhile\uff08\u8868\u8fbe\u5f0f\uff09{\u5faa\u73af\u4f53}\u3002
\u53c2\u8003\u8d44\u6599\uff1a\u767e\u5ea6\u767e\u79d1 while \uff08\u5faa\u73af\u8bed\u53e5\u53ca\u82f1\u6587\u5355\u8bcd\uff09 \u767e\u5ea6\u767e\u79d1 do while

C\u8bed\u8a00\u4e2dwhile\u548cdo\u2013while\u5faa\u73af\u7684\u4e3b\u8981\u533a\u522b\u5982\u4e0b\uff1a
1\u3001\u5faa\u73af\u7ed3\u6784\u7684\u8868\u8fbe\u5f0f\u4e0d\u540c
while\u5faa\u73af\u7ed3\u6784\u7684\u8868\u8fbe\u5f0f\u4e3a\uff1awhile\uff08\u8868\u8fbe\u5f0f\uff09{\u5faa\u73af\u4f53}\u3002
do-while\u5faa\u73af\u7ed3\u6784\u8868\u8fbe\u5f0f\u4e3a\uff1ado{\u5faa\u73af\u4f53;}while (\u6761\u4ef6\u8868\u8fbe);\u3002

2\u3001\u6267\u884c\u65f6\u5224\u65ad\u65b9\u5f0f\u4e0d\u540c
while\u5faa\u73af\u6267\u884c\u65f6\u53ea\u6709\u5f53\u6ee1\u8db3\u6761\u4ef6\u65f6\u624d\u4f1a\u8fdb\u5165\u5faa\u73af\uff0c\u8fdb\u5165\u5faa\u73af\u540e\uff0c\u6267\u884c\u5b8c\u5faa\u73af\u4f53\u5185\u5168\u90e8\u8bed\u53e5\u81f3\u5f53\u6761\u4ef6\u4e0d\u6ee1\u8db3\u65f6\uff0c\u518d\u8df3\u51fa\u5faa\u73af\u3002
do-while\u5faa\u73af\u5c06\u5148\u8fd0\u884c\u4e00\u6b21\uff0c\u5728\u7ecf\u8fc7\u7b2c\u4e00\u6b21do\u5faa\u73af\u540e\uff0c\u6267\u884c\u5b8c\u4e00\u6b21\u540e\u68c0\u67e5\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u503c\u662f\u5426\u6210\u7acb\uff0c\u5176\u503c\u4e3a\u4e0d\u6210\u7acb\u65f6\u800c\u4f1a\u9000\u51fa\u5faa\u73af\u3002
3\u3001\u6267\u884c\u6b21\u6570\u4e0d\u540c
while\u5faa\u73af\u662f\u5148\u5224\u65ad\u540e\u6267\u884c\uff0c\u5982\u679c\u5224\u65ad\u6761\u4ef6\u4e0d\u6210\u7acb\u53ef\u4ee5\u4e0d\u6267\u884c\u4e2d\u95f4\u5faa\u73af\u4f53\u3002
do-while\u5faa\u73af\u662f\u5148\u6267\u884c\u540e\u5224\u65ad\uff0c\u6267\u884c\u6b21\u6570\u81f3\u5c11\u4e3a\u4e00\u6b21\uff0c\u6267\u884c\u4e00\u6b21\u540e\u5224\u65ad\u6761\u4ef6\u662f\u5426\u6210\u7acb\uff0c\u5982\u679c\u4e0d\u6210\u7acb\u8df3\u51fa\u5faa\u73af\uff0c\u6210\u7acb\u5219\u7ee7\u7eed\u8fd0\u884c\u5faa\u73af\u4f53\u3002
4\u3001\u6267\u884c\u672b\u5c3e\u5faa\u73af\u4f53\u7684\u987a\u5e8f\u4e0d\u540c
while\u5faa\u73af\u7684\u672b\u5c3e\u5faa\u73af\u4f53\u4e5f\u662f\u5728\u4e2d\u95f4\u5faa\u73af\u4f53\u91cc\uff0c\u5e76\u5728\u4e2d\u95f4\u5faa\u73af\u4f53\u4e2d\u6267\u884c\uff0c\u5faa\u73af\u4f53\u662f\u5426\u7ee7\u7eed\u8fd0\u884c\u7684\u6761\u4ef6\u4e5f\u5728\u5faa\u73af\u4f53\u4e2d\u3002
do-while\u5faa\u73af\u662f\u5728\u4e2d\u95f4\u5faa\u73af\u4f53\u4e2d\u52a0\u5165\u672b\u5c3e\u5faa\u73af\u4f53\uff0c\u5e76\u5728\u6267\u884c\u4e2d\u95f4\u5faa\u73af\u4f53\u65f6\u6267\u884c\u672b\u5c3e\u5faa\u73af\u4f53\uff0c\u5faa\u73af\u4f53\u662f\u5426\u7ee7\u7eed\u8fd0\u884c\u7684\u6761\u4ef6\u5728\u672b\u5c3e\u5faa\u73af\u4f53\u91cc\u3002
\u53c2\u8003\u8d44\u6599\u6765\u6e90\uff1a\u767e\u5ea6\u767e\u79d1\u2014\u2014while
\u767e\u5ea6\u767e\u79d1\u2014\u2014do while

while do和while的区别是
一、while语句的一般形式为:
while(表达式)语句
其中表达式是循环条件,语句为循环体。
while语句的语义是:计算表达式的值,当值为真(非0)时, 执行循环体语句。

二、do-while语句的一般形式为:
do
语句
while(表达式);
这个循环与while循环的不同在于:它先执行循环中的语句,然后再判断表达式是否为真, 如果为真则继续循环;如果为假, 则终止循环。因此, do-while循环至少要执行一次循环语句。

循环结构可以减少源程序重复书写的工作量,用来描述重复执行某段算法的问题,这是程序设计中最能发挥计算机特长的程序结构 。
循环结构可以看成是一个条件判断语句和一个向回转向语句的组合。另外,循环结构的三个要素:循环变量、循环体和循环终止条件. ,循环结构在程序框图中是利用判断框来表示,判断框内写上条件,两个出口分别对应着条件成立和条件不成立时所执行的不同指令,其中一个要指向循环体,然后再从循环体回到判断框的入口处。

区别不大
do-while的条件检查在后,所以不管如何肯定会执行一次
while条件检查在前。
其他都一样

扩展阅读:xbox series x ... 初中阶段when和while ... do while java ... while循环改成do while ... java switch case ... 初中when和while图解 ... do while用法举例 ... while true ... do while至少执行一次 ...

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