Decision StructuresSpacecraftDecisions (If Statements)QuestionsIf StatementsComparison OperatorsLogical OperatorsBoolean valueTest for SnowingAnds and OrsIf Blocks (True and False Options)If-then-else BlocksNested if-then-else structureIf –ElseIf – Else BlocksWhat if you have a bunch of choices?Select Case BlocksName CaseQuadratic equationSolutionQuadratic Formula SolverQuadratic EquationDecision StructuresSpacecraft•Divine Vessel V will orbits earth 14 times in 21 hours•First manned spacecraft of China, only the third country that can sendDecisions (If Statements)•Comparison Operators (>, < , <>, …)•Logical Operators (and, or, not)•TRUE or FALSE determines whether to proceed!QuestionsIfTemp < 32ThenIt is warm! It is COLD!TrueFalseIf Statements•If weather = “PRECIPITATION” Then•It is raining or snowing•If TEMP < 32 Then•It is COLD!If temp < 32 Then picture1.print “It is FREEZING!”End IfComparison Operators•See table 5.1•= equal to•<> unequal to•< less than•> greater than•<= less than or equal to•>= greater than or equal to•Works both for numbers and strings.Logical Operators•And, Or, Not•a >=b And b <> 2•a >=b Or b <> 2•And statements: Both must be true•Or statements: one or both must be true•Not statements: True if statement is falseBoolean value•Comparison operator and logical operator will return a Boolean value.•Boolean value has only two possibilities: True and FalseTest for Snowing•What are conditions for snowing?•Temperature?•Precipitation?Ands and Ors•If (weather = “PRECIPITATION”) And (TEMP < 32) Then•Tests for snow•If (weather = “PRECIPITATION”) Or (TEMP < 32) Then•Tests for bad weatherPRECIPNO<32>32NO< 32> 32And OrTFFF TTFTPRECIPIf Blocks (True and False Options)If condition1 Then …Statements…Else …Statements…End IfIf-then-else BlocksIf temp < 32 Then picture1.print “It is FREEZING!”Else picture1.print “It is not freezing”End IfNested if-then-else structureIf weather = “PRECIPITATION” thenIf TEMP <32 thenPicture1.Print “Snowing”ElsePicture1.Print “raining”End ifElse If TEMP <32 thenPicture1.Print “cold day”ElsePicture1.Print “warm day”End ifEnd ifIf –ElseIf – Else Blocks‘Get answer to question, “Did you‘have a fun spring break?” answer = Ucase(text1.text)answer = Trim(answer)If answer=“YES” Then picture1.print “Cool.”ElseIf answer=“NO” Thenpicture1.print “Sorry.”Else picture1.print “Can’t you even answer the question?”End IfWhat if you have a bunch of choices?•Case Selectors are great for multiple choices•Best for equalities, not inequalitiesSelect Case Blocks•Allows multiple options (not just true or false)Select Case Variable Case Condition1 Case Condition2 Case Condition3 Case ElseEnd SelectName CaseSelect Case Name Case “George W. Bush” picture1.print “Bring them on.” Case “terminator T-100” picture1.print “Alta vista babe.” Case “Conan O'Brien” picture1.print “This is such a good show. What a nice show... Great show” Case Else picture1.print “Wow, who is this guy!”End SelectQuadratic equation•aX2+bX+c=0•What is the solution to X?SolutionQuadratic Formula SolverQuadratic Equation•Write an algorithm for using the quadratic formula to determine if there are real roots for the equation•Will the program crash if b2-4ac < 0? •If you take square root for a negative number you will get a run time error.•The program should be able to deal with both real and complex
View Full Document