资源描述
Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,Insight Through Computing,*,More on Conditionals,Nested ifs,Multiple Alternatives,Top-Down Design,Insight Through Computing,Recall the,if-else,“Template”,if,end,else,boolean,expression,Commands to execute if the,expression if TRUE,Commands to execute if the,expression if FALSE,Insight Through Computing,Question Time,Variables a,b,and c have whole number,values.True or false:This fragment prints,“Yes”if there is a right triangle with side,lengths a,b,and c and prints“No”otherwise.,if a2+b2=c2,disp(Yes,),else,disp(No,),end,A:,True,B:,False,Insight Through Computing,a=5;,b=3;,c=4;,if a2+b2=c2,disp(Yes,),else,disp(No,),end,Prints“no”even though we have:,5,4,3,Insight Through Computing,if,a2+b2=c2,disp(Yes,),else,disp(No,),end,The,boolean,expression should be true if,a,2,+b,2,=c,2,or,a,2,+c,2,=b,2,or,b,2,+c,2,=a,2,is true.,a2+b2=c2,(a2+b2=c2)|(a2+c2=b2)|(b2+c2=a2),Insight Through Computing,Developing“If”Solutions,Illustrate the thinking associated with the design of if statements.,The methodology of,stepwise refinement,.,Two examples.,Insight Through Computing,Write a script that solicits a positive integer Y and prints the number of days in year Yas determined by the Gregorian calendar.,Problem 1,Insight Through Computing,Leap Year Rule,A non-century year is a leap year if it is divisible by 4.,A century year is a leap year only if it is divisible by 400.,Insight Through Computing,Will Need the Built-In Function,rem,a,b,rem(a,b,),15,6,3,56,7,0,The value of,rem(a,b,)is the remainder when,the value of a is divided by the value of b.,(Assume a,b are whole numbers.),Insight Through Computing,Input y.,If y is not divisible by 100,Use the non-century year rule.,Otherwise,Use the century year rule.,“,Pseudocode,”Solution,Insight Through Computing,y=,input(Enter,the Year:);,if rem(y,100)=0,%y is not a multiple of 100,Use the non-century rule,else,%y is a multiple of 100,Use the century rule,end,Refine,Insight Through Computing,Refine the If-Box,%y is not a multiple of 100,Use the non-century rule,%y is not a multiple of 100,If y is divisible by 4,Print 366,Otherwise,Print 365,Insight Through Computing,%y is not a multiple of 100,if rem(y,4)=0,%y is divisible by 4,disp(366),else,%y is not divisible by 4,disp(365),end,Insight Through Computing,y=,input(Enter,the Year:);,if rem(y,100)=0,%y is not a multiple of 100,Use the non-century rule,else,%y is a multiple of 100,Use the century rule,end,Refine,Done,Next,Insight Through Computing,Refine the Else-Box,%y is divisible by 100,Use the Century rule,%y is divisible by 100,If y is divisible by 400,Print 366,Otherwise,Print 365,Insight Through Computing,%y is divisible by 100,if rem(y,400)=0,%y is divisible by 400,disp(366),else,%y is not divisible by 400,disp(365),end,Insight Through Computing,y=,input(Enter,the Year:);,if rem(y,100)=0,%y is not a multiple of 100,Use the non-century rule,else,%y is a multiple of 100,Use the century rule,end,Refine,Done,Done,Insight Through Computing,y=,input(Enter,the Year:);,if rem(y,100)=0,if rem(y,4)=0,disp(366),else,disp(365),end,else,if rem(y,400)=0,disp(366),else,disp(365),end,end,The,whole thing,without,comments,Insight Through Computing,Key Problem-SolvingStrategy,Progress from,pseudocode,to,Matlab,through a sequence of refinements.,Comments have an essential role during,the transitions.They“stay on”all the,way to the finished fragment.,Insight Through Computing,Starting Points VaryIn“Friendliness”,A non-century year is a leap year if it is divisible by 4.,A century year is a leap year only if it is divisible by 400.,A year is a leap year if it is divisible by 4,with the exception of century years that,are not divisible by 400.,Insight Through Computing,Problem 2,Write a fragment that prints theminimum value of,in the interval,L=x=R,Insight Through Computing,L,R,One Possibility,L=,xc,=R,minVal,at,xc,Insight Through Computing,L,R,Another Possibility,R,xc,minVal,at R,Insight Through Computing,L,R,Still Another Possibility,xc,L,minVal,at L,Insight Through Computing,We conclude that.,If,xc,is in the interval,The minimum is at,xc,Otherwise,The minimum is at an endpoint,Insight Through Computing,We Start With,Pseudocode,If,xc,is in the interval,The minimum is at,xc,Otherwise,The minimum is at an endpoint,Task:Convert to“legal,Matlab,.,Insight Through Computing,First refinement,if(L=,xc,)&(,xc,=R),%L=,xc,=R,%The minimum is at,xc,.,else,%,xc,L or R,xc,%The minimum is at an endpoint.,end,(1)Boolean expression,(2)commented if-box,(3)commented else box,Insight Through Computing,Refine the If-Box,%L=,xc,=R,%The minimum is at,xc,.,%L=,xc,=R,%The minimum is at,xc,.,minVal,=xc2+b*,xc,+c,Insight Through Computing,Refine the Else-Box,%,xc,L or R,xc,%The minimum is at an endpoint.,R,xc,minVal,at R,xc,L,minVal,at L,Insight Through Computing,%,xc,L or R,xc,%The minimum is at an endpoint.,if,xc,is to the left of L,The minimum is at L,Otherwise,The minimum is at R,Insight Through Computing,%,xc,L or R,xc,%The minimum is at an endpoint.,if,xc,L,%The minimum is at L,minVal,=L2+b*L+c,else,%The minimum is at R,minVal,=R2+b*R+c,end,Insight Through Computing,Overall(w/o Comments),if(L=,xc,)&(,xc,=R),minVal,=xc2+b*,xc,+c.,else,if,xc,L,minVal,=L2+b*L+c,else,minVal,=R2+b*R+c,end,end,Insight Through Computing,Notice there are 3 Alternatives,if(L=,xc,)&(,xc,=R),minVal,=xc2+b*,xc,+c.,else,if,xc,L,minVal,=L2+b*L+c,else,minVal,=R2+b*R+c,end,end,Insight Through Computing,The if-,elseif,-else Construct,if(L=,xc,)&(,xc,=R),minVal,=xc2+b*,xc,+c.,elseif,xc,L,minVal,=L2+b*L+c,else,minVal,=R2+b*R+c,end,Execute exactly one block.,Insight Through Computing,When there are Many Alternatives,if,elseif,elseif,else,end,Boolean Expression,Boolean Expression,Boolean Expression,Find the,first true,boolean,expression,&execute,its block.,Otherwise,execute the else,block.,Insight Through Computing,A Common Situation,if,end,Boolean Expression,When there is nothing to do if the,boolean,expression is false.,Insight Through Computing,Question Time,What can you say about the value of,x and y after this fragment is executed?,x=3;Y=4;,if x y,x=3;y=4;,end,A:,X is 3 and Y is 4,B:,X is 4 and Y is 3,C:,X is 4 and Y is 4,Insight Through Computing,Top-Down Design,State problem,Define inputs,&outputs,Design algorithm,Convert algorithm,to program,Test and debug,An algorithm is an,idea,.To use an algorithm you must choose a programming language and,implement,the algorithm.,Decomposition,Stepwise refinement,Insight Through Computing,
展开阅读全文