1 / 57

Problem Solving with Decisions

Problem Solving with Decisions. Lesson 6. Overview. The Decision Logic Structure The If Instructions Using Straight-through Logic Using Positive Logic Using Negative Logic Logic Conversion Which Decision Logic? Decision Tables. Flowchart Symbols. Decision True/False/Else Process

deanne
Télécharger la présentation

Problem Solving with Decisions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem Solving with Decisions Lesson 6 COP1000

  2. Overview • The Decision Logic Structure • The If Instructions • Using Straight-through Logic • Using Positive Logic • Using Negative Logic • Logic Conversion • Which Decision Logic? • Decision Tables COP1000

  3. Flowchart Symbols • Decision • True/False/Else • Process • Assign Decision Process Assign COP1000

  4. Decisions, Decisions… Zzzzz! Alarm goes off! Oh, no! Snooze = 9? Snooze or Off? Snooze < 9 Sleep or Get up? Go Back to Sleep Get Dressed. . . COP1000

  5. Decision Logic Structure • Allows us to ask questions of our data. • Similar to the way we think. • We make decisions every day. • Have two parts: • recognition of what action to take (you have to get out of bed) and • execution of that action (you actually jump up out of bed) COP1000

  6. Common Forms • The If Statement • The Single Selection If/Then • The Double If/Then/Else • The Multiple If/Then/Else • The Nested If Statement • The Case Statement • The Switch Statement COP1000

  7. Three Types of Decisions • Straight-through Logic • All decisions are processed sequentially, one after another. • Least efficient, but most thorough • Positive Logic • Processing flow continues through the module instead of processing succeeding decisions, once the result is True. • Negative Logic • Flow is based on result being False. • Nested decisions use Positive or Negative, but not Straight-through. COP1000

  8. Straight-through • All conditions are tested. • Least efficient, but most exhaustive. T F T F COP1000

  9. Positive Logic • Uses If/Then/Else instructions • Continues processing based on True results T Grade >= 90 F LtrG= “A” Grade >= 80 T F LtrG= “B” LtrG = “Other” Note that the strings are ALWAYS in quotations marks! COP1000

  10. Negative Logic • Executes process based on False • Processes another decision when the result is True Grade < 90 T F Grade < 80 LtrG= “A” F T LtrG = “Other” LtrG= “B” COP1000

  11. So, Which Do You Use? • Optimum? • Evaluate all three • Reality? • this almost never happens • The Goal • Easiest to understand • Requires fewest tests • Easiest to maintain • Avoid the trap of always using the same COP1000

  12. IF Statements COP1000

  13. Grade >= 60 Display “Passed” true false The Single Selection IF • Syntax IfconditionThenstatement(s) EndIf • Tests for one thing only. • If the statement results in a False, it drops out without performing the statements. • Very inefficient, and not very flexible • Most thorough! COP1000

  14. Example If Grade <= 100 and Grade >= 90 Then LetterGrade = “A” EndIf This asks the question: if the value stored in Grade is less than or equal to 100 and is greater than or equal to 90, then LetterGrade takes on the value of the character “A”. Condition COP1000

  15. A Classic Example Private Sub Swap(X, Y) ‘X & Y passed in Dim Temp As Integer ‘local variable If X > Y Then Temp = X ‘Copies X into Temp X = Y ‘Copies Y into X Y = Temp ‘Copies Temp into Y EndIf End Sub VB code COP1000

  16. Grade >= 60 true false Display “Passed” Display “Failed” The Double Selection If • Allows two questions to be asked • Positive Logic • Syntax If condition Thenstatement(s) Else statement(s) EndIf COP1000

  17. An Example If Grade >= 60 Then LetterGrade= “Passed” Else LetterGrade= “Failed” EndIf Notice that there are two exclusive options. Greater than or equal to 60 or Less than. No other option is available. COP1000

  18. An Another Example If Hours > 40 Then Pay = PayRate * (40 + (1.5 * (Hours – 40))) Else Pay = PayRate * Hours EndIf Another way to write: Pay = (40 * PayRate) + _ ((1.5 * PayRate) * (Hours - 40)) COP1000

  19. The Multiple Selection If • The If/Then/Else permits multiple questions • Positive Logic • Always place the most likely to occur first. • Increases efficiency COP1000

  20. Example Grade >= 90 LetterGrade = “A” true false If Grade >= 90 Then LetterGrade = “A” ElseIf Grade >= 80 Then LetterGrade = “B” ElseIf Grade >= 70 Then LetterGrade = “C” ElseIf Grade >= 60 Then LetterGrade = “D” Else LetterGrade = “F” End If Grade >= 80 LetterGrade = “B” true false Grade >= 70 LetterGrade = “C” true false Grade >= 60 LetterGrade = “D” true false LetterGrade = “F” Notice Indentions COP1000

  21. How About This One? If Grade < 60 Then LetterGrade = “F” ElseIf Grade >= 60 Then LetterGrade = “D” ElseIf Grade >= 70 Then LetterGrade = “C” ElseIf Grade >= 80 Then LetterGrade = “B” Else LetterGrade = “A” End If Would you want me to use this one for your grades? COP1000

  22. How About This One? If Grade < 60 Then LetterGrade = “F” ElseIf Grade < 70 Then LetterGrade = “D” ElseIf Grade < 80 Then LetterGrade = “C” ElseIf Grade < 90 Then LetterGrade = “B” Else LetterGrade = “A” End If Would you want me to use this one for your grades? COP1000

  23. Nested Vs. Multiple Nested Ifs Multiple-Alternative Ifs if condition then if condition then if condition then statement1 if condition then elseifcondition then statement1 statement2 else elseifcondition then else statement3; else; {next statement} {next statement} nested ifs Which is easier to read? debug? COP1000

  24. Nested If’s If Grade < 90 Then If Grade < 80 Then If Grade < 70 Then If Grade < 60 Then LetterGrade= “F” Else LetterGrade = “D” Else LetterGrade = “C” Else LetterGrade = “B” Else LetterGrade = “A” EndIfUgly, isn’t it? Negative Logic Forces each decision to be tested COP1000

  25. Logical Operators & Opposites • Called Logical Opposites • When switching between Positive and Negative Logic, change all • < to >= • <= to > • > to <= • >= to < • = to <> • <> to = • And…exchange Then statements with Else statements COP1000

  26. The Case & The Switch COP1000

  27. The Case Statement • Is similar to a series of If/Then/Else statements. • Positive Logic • Syntax: Select Casetestvalue Casevalue1 statement group 1 Casevalue2 statement group 2 End Select COP1000

  28. Case Flow Chart True Case a Case a action False True Case b Case b action False Case Else Case Else action COP1000

  29. Case Example Select Case Grade Case 90..100 LetterGrade = “A” Case 80..89.9 LetterGrade = “B” Case 70..79.9 LetterGrade = “C” Case 60..69.9 LetterGrade = “D” Else LetterGrade = “F” End Select COP1000

  30. Case Usage • Used for menu-driven programs and event-driven programs • Menu • A list of options that a program based on case logic can perform • Event-driven • Order is dictated by the user, not the programmer, by which button is clicked on COP1000

  31. A fun Case example… Select Case Age Case 16 LabelAge.Caption = “You can drive now! Parents Beware!” Case 18 LabelAge.Caption = “You can vote now!” Case 21 LabelAge.Caption = “You can drink wine with your meals.” Case 65 LabelAge.Caption = “Time to retire and have fun!” Case Else LabelAge.Caption = “You’re a great age! Enjoy it! End Select COP1000

  32. The Switch Statement • Related to the If/Then/Else structure. • Each argument that is passed to Switch is either a condition or a value. • Positive Logic • All possible values must be accounted for because there is no Else. COP1000

  33. Switch Flow Chart Switch a True Switch aaction False Switch b True Switch baction False Switch c Switch caction True COP1000

  34. Switch Example LetterGrade = Switch _ (grade >= 90, “A”, _ grade >= 80, “B”, _ grade >= 70, “C”, _ grade >= 60, “D”, _ grade < 60, “F”) Much more concise than even the Case statement, but not in every language. COP1000

  35. Decision Tables COP1000

  36. Decision Tables • Exist in several different forms • Most consist of 4 parts: • The conditions • The actions • The combinations of True and False for the conditions • The action to be taken or the consequences for each combination of conditions. COP1000

  37. Decision Table Example COP1000

  38. How would you set up a Decision Table for Numeric Grades associated with Letter Grades? Another Decision Table Example COP1000

  39. Decision Table Solution COP1000

  40. Let’s work a problem… COP1000

  41. The Problem • Write the calculation module to choose the largest number from a set of three numbers, A, B, and C. COP1000

  42. The Flow Chart False True If A > B False False True True If B > C If A > C Largest = B Largest = C Largest = A Largest = C COP1000

  43. Pseudocode Solution If A is greater than B Then A is > B If A is greater than C Then A is > C Largest equals A A is > B & A > C Else Largest equals C A is > B & A < C Else If B is greater than C Then Largest equals B A is < B & B > C Else Largest equals C A is < B & C > B End If COP1000

  44. Modify? • How would you modify the logic to print the results in order, A, B, C? • What are the possible outputs that you would expect to see? A, B, C B, C, A C, B, A A, C, B B, A, C C, A, B COP1000

  45. Decision Table Solution COP1000

  46. Coding the Problem In Visual Basic Note: Again, up this point it didn’t matter what language we use… COP1000

  47. Data Dictionary Yes, I know…the variables are not mnemonic! COP1000

  48. Option Explicit On ' Input Values by User Dim A As Short Dim B As Short Dim C As Short VB Solution – Declare Variables COP1000

  49. VB Solution - Get Input Private Sub txtNum1_TextChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles txtNum1.TextChanged A = Val(txtNum1.Text) 'converts user input to numeric format End Sub Private Sub txtNum2_TextChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles txtNum2.TextChanged B = Val(txtNum2.Text) 'converts user input to numeric format End Sub Private Sub txtNum3_TextChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles txtNum3.TextChanged C = Val(txtNum3.Text) 'converts user input to numeric format End Sub COP1000

  50. VB Solution – Calculate Result Private Sub Calculate _ (ByVal A, ByVal B, ByVal C) Dim Largest As Short If A > B Then If A > C Then Largest = A Else Largest = C End If ElseIf B > C Then Largest = B Else Largest = C End If lblInstructions.Text = _ ("The Largest number is " & _ CStr(Largest)) End Sub COP1000

More Related