1 / 54

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

star
Download Presentation

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 COP1006

  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 COP1006

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

  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. . . COP1006

  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) COP1006

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

  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. COP1006

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

  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 in quotations marks! COP1006

  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” COP1006

  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 COP1006

  12. If Statements COP1006

  13. Grade >= 60 Display “Passed” true false The Single Selection IF • Syntax If condition Then statement(s) End If • Tests for one thing only. • If the statement results in a False, it drops out without performing the statements. • Very efficient, but not very flexible COP1006

  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 COP1006

  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 End If End Sub VB code COP1006

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

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

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

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

  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 COP1006

  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? COP1006

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

  23. Nested If’s Forces each decision to be tested 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” End If Ugly, isn’t it? COP1006

  24. 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 COP1006

  25. The Case & The Switch COP1006

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

  27. Case Flow Chart True Case a Case a action False True Case b Case b action False Case Else Case Else action COP1006

  28. 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 COP1006

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

  30. Switch Flow Chart Switch a True Switch aaction False Switch b True Switch baction False Switch c Switch caction True COP1006

  31. 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. COP1006

  32. Decision Tables COP1006

  33. 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. COP1006

  34. Decision Table Example COP1006

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

  36. Decision Table Solution COP1006

  37. Let’s work a problem… COP1006

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

  39. 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 COP1006

  40. 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 & B < 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 COP1006

  41. 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 COP1006

  42. Decision Table Solution COP1006

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

  44. Data Dictionary Yes, I know…the variables are not mnemonic! COP1006

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

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

  47. 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 COP1006

  48. VB Solution - Produce Output Private Sub cmdPrintResult_Click() lblOutput.Text = ("A = " & CStr(A) & _ " B = " & CStr(B) & _ " C = " & CStr(C)) End Sub COP1006

  49. VB Solution – the Form COP1006

  50. Getting the Input 1st step 2nd step 3rd step COP1006

More Related