C语言编程学习课件 (23).pdf
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,which 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 loop 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 the 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 loop 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 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 statement,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 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 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=sum+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 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 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 changechange);5.while5.while statementstatement(set loop conditionset loop condition)6.Output the accumulation sum.6.Output the accumulation sum.do-while statement programing apply Program to realize the cumulative sum of 1-10.Algorithm analysis:Flow chart:Code:#includestdio.h void main()Running result:sum=55 int sum,i;sum=0;i=1;do sum=sum+i;i+;while(i=10);printf(sum=%dn,sum);do-while statement programing apply Program to realize the cumulative sum of 1-10.Compare while and do-while statement main()int sum=0,i;scanf(%d,&i);while(i=10)sum=sum+i;i+;printf(sum=%dn,sum);main()int sum=0,i;scanf(%d,&i);do sum=sum+i;i+;while(i=10);printf(sum=%dn,sum);Running result:1 sum=55 Running again:11 sum=0 Running result:1 sum=55 Running again:11 sum=11 for statement(1 1)Execute e1.This part is usually used to calculate the initial Execute e1.This part is usually used to calculate the initial value(execute only once).value(execute only once).(2 2)Execute e2.The condition is evaluated in this part:if the Execute e2.The condition is evaluated in this part:if the value is true,then execute step(3),otherwise skip it and continue value is true,then execute step(3),otherwise skip it and continue to execute the statement following the structure.to execute the statement following the structure.(3 3)Execute the loop body statement.Execute the loop body statement.(4 4)Execute e3 to update the loop control variable.Execute e3 to update the loop control variable.(5 5)Repeat Step(2).Repeat Step(2).for(Initializer expression e1;Conditional expression e2;Incremental expression e3)loop body;Syntax of while statement Execution process Flow chart:1.Define two integer variables1.Define two integer variables;2 2、initialize variableinitialize variable;3 3、for statementfor statement(initial values,initial values,condition and incrementcondition and increment););4.Calculate the sum4.Calculate the sum(loop body);loop body);5.Output the accumulation sum.5.Output the accumulation sum.true false i10 sum=sum+i sum=0,i=1 i=i+1 Instruction for the for statement Program to realize the cumulative sum of 1-10.Algorithm analysis:Code:#includestdio.h void main()Running result:sum=55 int sum=0,i;for(i=1;i=10;i+)sum=sum+i;printf(sum=%dn,sum);Program to realize the cumulative sum of 1-10.Instruction for the for statement Flow chart Instruction for the for statement Instruction true false Condition e2 Loop body Initializer expression e1 Incremental expression e3(1 1)If there is more than one statement in it,loop If there is more than one statement in it,loop body should be enclosed in braces.If there is only body should be enclosed in braces.If there is only one statement in,braces can be omitted.one statement in,braces can be omitted.(2 2)Three expressions in parentheses after the Three expressions in parentheses after the for can be written in other positions according to for can be written in other positions according to their execution order and functions,but no matter their execution order and functions,but no matter how to change,the;in the parentheses cannot be how to change,the;in the parentheses cannot be omitted.omitted.General principle to choose loop statement If the number of repetitions is known and the loop is If the number of repetitions is known and the loop is controlled by counting.controlled by counting.for statement for statement If the number of repetitions is unknown and conditional If the number of repetitions is unknown and conditional control loop is adopted.control loop is adopted.while statementwhile statement If the loop body is to be executed at least once.If the loop body is to be executed at least once.dodo-while statementwhile statement This is just a This is just a generalgeneral principle.principle.Programming In CProgramming In C