C语言编程学习课件 (23).pdf
《C语言编程学习课件 (23).pdf》由会员分享,可在线阅读,更多相关《C语言编程学习课件 (23).pdf(16页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Programming In CProgramming In C 01 02 03 while statement do-while statement for statement General principle to choose loop statement 04 Contents The while statement Syntax of while statement Execution process while(expression)loop body;“expressionexpression”is a loop conditionis a loop condition,wh
2、ich can be a constant,a variable,or any which can be a constant,a variable,or any type of expression.Usually it is a relational expression or a logical type of expression.Usually it is a relational expression or a logical expression.The loop body consists of one or more statements.expression.The loo
3、p body consists of one or more statements.(1 1)Calculate the expression value after while.If the value is Calculate the expression value after while.If the value is true,execute the following step(2).Otherwise,skip it and true,execute the following step(2).Otherwise,skip it and continue to execute t
4、he statement after the structure.continue to execute the statement after the structure.(2 2)Execute the loop body statement.Execute the loop body statement.(3 3)Repeat step(1).Repeat step(1).Flow chart Instruction for while statement(1 1)End of the loop is controlled by the expression End of the loo
5、p is controlled by the expression following the while.following the while.There must be a statement in the loop There must be a statement in the loop body that changes the value of the loop control variable to body that changes the value of the loop control variable to end the loop.Otherwise,it will
6、 lead to an endless loopend the loop.Otherwise,it will lead to an endless loop.(2 2)If there is more than one statements in the loop body,If there is more than one statements in the loop body,it must be enclosed in braces.If there is only one it must be enclosed in braces.If there is only one statem
7、ent,the braces can be omitted.statement,the braces can be omitted.Instruction Loop body TrueTrue FalseFalse ExpressionExpression while statement programing apply Program to realize the cumulative sum of 1-10.Algorithm analysis:Flow chart:true false i10 sum=sum+i i=i+1 sum=0,i=1 1.Define two integer
8、variables1.Define two integer variables;2.2.Assign corresponding initial values to ssign corresponding initial values to two variablestwo variables(initialize initialize the loop control the loop control variable);variable);3 3、whilewhile statementstatement(set loop conditionset loop condition););4
9、4、Calculate the sumCalculate the sum(loop body loop body););5 5、Augend changeAugend change(loop control variable loop control variable changechange););6.Output the accumulation sum6.Output the accumulation sum;Code:#includestdio.h void main()Running result:sum=55 int sum,i;sum=0;i=1;while(i=10)sum=s
10、um+i;i+;printf(sum=%dn,sum);while statement programing apply Program to realize the cumulative sum of 1-10.do-while statement do loop body;while(expression);(1 1)Execute the loop body statement.(2 2)Calculate the expression value after while.If the value is true,execute step(1).Otherwise,skip it and
11、 continue to execute the statement after the structure.(3 3)Repeat Step(1).Flow chart Loop body expressionexpression false true Syntax of while statement Execution process true false i10 sum=sum+i i=i+1 sum=0,i=1 1.Define two integer variables 1.Define two integer variables 2.2.Assign corresponding
12、initial values to ssign corresponding initial values to two variablestwo variables(initialize initialize the loop control the loop control variable);variable);3.Calculate the sum3.Calculate the sum(loop body);loop body);4.Augend change4.Augend change(loop control variable loop control variable chang
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C语言编程学习课件 23 语言 编程 学习 课件 23
限制150内