lesson 6 mcmanus cop1006 1. the decision logic structure the if instructions using...
TRANSCRIPT
Lesson 6
McManusCOP1006 1
The Decision Logic Structure The If Instructions Using Straight-through Logic Using Positive Logic Using Negative Logic Logic Conversion Which Decision Logic? Decision Tables
McManusCOP1006 2
Decision◦ True/False/Else
Process Assign
McManusCOP1006 3
Assign
Process
Decision
McManusCOP1006 4
Snoozeor Off?
Alarm goes off!
Sleep or Get up?
Zzzzz!
Snooze
Go Back to Sleep
Get Dressed. . .
Snooze= 9?
< 9
Oh, no!
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)
McManusCOP1006 5
The If Statement◦ The Single Selection If/Then◦ The Double If/Then/Else◦ The Multiple If/Then/Else
The Case Statement The Switch Statement
McManusCOP1006 6
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.
McManusCOP1006 7
All conditions are tested.
Least efficient, but most exhaustive.
McManusCOP1006 8
TF
TF
Uses If/Then/Else instructions
Continues processing based on True results
McManusCOP1006 9
Grade >= 90
Grade >= 80LtrG
= “A”
LtrG = “Other”
T
T
F
LtrG= “B”
F
Note that the strings are in quotations marks!
Executes process based on False
Processes another decision when the result is True
McManusCOP1006 10
T
T
F
F
Grade < 90
LtrG= “A”
Grade < 80
LtrG = “Other”
LtrG= “B”
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
McManusCOP1006 11
McManusCOP1006 12
SyntaxIf 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
McManusCOP1006 13
Grade >= 60
true
false
Display“Passed”
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”.
McManusCOP1006 14
Condition
Private Sub Swap(X, Y) ‘X & Y passed inDim Temp As Integer ‘local variableIf X > Y Then
Temp = X ‘Copies X into TempX = Y ‘Copies Y into XY = Temp ‘Copies Temp into Y
End IfEnd Sub
VB code
McManusCOP1006 15
Allows two questions to be asked Syntax
If condition Thenstatement(s)
Elsestatement(s)
End If
McManusCOP1006 16
Grade >= 60
true
Display“Passed”
false
Display“Failed”
If Grade >= 60 ThenLetterGrade = “Passed”
ElseLetterGrade = “Failed”
End If
Notice that there are two exclusive options. Greater than or equal to 60 or Less than. No other option is available.
McManusCOP1006 17
If Hours > 40 ThenPay = PayRate * (40 + (1.5 * (Hours – 40)))
ElsePay = PayRate * Hours
End If
Another way to write:Pay = (40 * PayRate) + _
((1.5 * PayRate) * (Hours - 40))
McManusCOP1006 18
The If/Then/Else permits multiple questions Always place the most likely to occur first.
◦ Increases efficiency
McManusCOP1006 19
If Grade >= 90 ThenLetterGrade = “A”
ElseIf Grade >= 80 ThenLetterGrade = “B”
ElseIf Grade >= 70 ThenLetterGrade = “C”
ElseIf Grade >= 60 ThenLetterGrade = “D”
ElseLetterGrade = “F”
End If
McManusCOP1006 20
Grade >= 90
true LetterGrade = “A”
false
Grade >= 80
true LetterGrade = “B”
false
Grade >= 70
true LetterGrade = “C”
false
Grade >= 60
true LetterGrade = “D”
false
LetterGrade = “F”Notice
Indentions
If Grade < 60 ThenLetterGrade = “F”
ElseIf Grade >= 60 ThenLetterGrade = “D”
ElseIf Grade >= 70 ThenLetterGrade = “C”
ElseIf Grade >= 80 ThenLetterGrade = “B”
ElseLetterGrade = “A”
End If
McManusCOP1006 21
Would you want me to use this one for your grades?
McManusCOP1006 22
nested ifs
Which is easier to read? debug?
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}
If Grade < 90 ThenIf Grade < 80 Then
If Grade < 70 ThenIf Grade < 60 Then
LetterGrade = “F”Else LetterGrade = “D”
Else LetterGrade = “C”Else LetterGrade = “B”
Else LetterGrade = “A”End If Ugly, isn’t it?
McManusCOP1006 23
Forces each decision to be
tested
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
McManusCOP1006 24
McManusCOP1006 25
Is similar to a series of If/Then/Else statements.
Syntax:Select Case testvalue
Case value1statement group 1
Case value2statement group 2
End Select
McManusCOP1006 26
McManusCOP1006 27
Case a actionCase aTrue
False
Case b
False
Case b actionTrue
Case Else actionCase Else
Select Case GradeCase 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
McManusCOP1006 28
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.
McManusCOP1006 29
McManusCOP1006 30
Switch a action
Switch aTrue
False
Switch b action
Switch bTrue
False
Switch c actionSwitch c
True
LetterGrade = Switch _ (grade >= 90, “A”, _ grade >= 80, “B”, _ grade >= 70, “C”, _ grade >= 60, “D”, _ grade < 60, “F”)
McManusCOP1006 31
Much more concise than even the Case statement, but not in every
language.
McManusCOP1006 32
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.
McManusCOP1006 33
Filing Status(Condition)
Single or Divorced
Married filing Jointly or Qualifying Widower
Married filing separately
Head of Household
Select tax rate (Action)
a. Singleb. Married jtc. Married spd. Head hshd
X
X
X
X
Compute standard deduction (Action)
a. $4,000b. $2,500c. $1,500
X
X
X
X
McManus COP1006 34
McManusCOP1006 35
How would you set up a Decision Table
for Numeric Grades associated with Letter Grades?
McManusCOP1006 36
Letter Grade (Solution)
Numeric Grade (Condition)
A B C D F
90 <= n X
80 <= n < 90 X
70 <= n < 80 X
60 <= n < 70 X
< 60 X
McManusCOP1006 37
Write the calculation module to choose the largest number from a set of three numbers, A, B, and C.
McManusCOP1006 38
McManusCOP1006 39
If A > B
If A > CIf B > C
Largest = C
Largest = B
Largest = C
Largest = A
True
TrueTrue
False
False
False
If A is greater than B Then A is > BIf A is greater than C Then A is > C
Largest equals A A is > B & A > CElse
Largest equals C A is < B & B < CElse
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
McManusCOP1006 40
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, AA, C, B B, A, C C, A, B
McManusCOP1006 41
Largest A B C
Order A B C X
A C B X
B C A X
B A C X
C B A X
C A B X
McManus COP1006 42
In Visual Basic
Note: Again, up this point it didn’t matter what language we use…
McManusCOP1006 43
Variable Name
Data Type Module Defined Domain
Scope
A Short
2 bytes
GetInput
Calculate
0 < n < 10 Global
B Short
2 bytes
GetInput
Calculate
0 < n < 10 Global
C Short
2 bytes
GetInput
Calculate
0 < n < 10 Global
McManus COP1006 44
Yes, I know…the variables are not mnemonic!
McManusCOP1006 45
Option Explicit On
' Input Values by UserDim A As ShortDim B As ShortDim C As Short
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
McManusCOP1006 46
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
McManusCOP1006 47
Private Sub cmdPrintResult_Click()
lblOutput.Text = ("A = " & CStr(A) & _ " B = " & CStr(B) & _
" C = " & CStr(C))
End Sub
McManusCOP1006 48
McManusCOP1006 49
McManusCOP1006 50
1st step2nd
step 3rd step
McManusCOP1006 51
Input Variables to be tested◦ A
any numeric value greater than 0 and less than 10
◦ B any numeric value greater than 0 and less than 10
◦ C any numeric value greater than 0 and less than 10
McManusCOP1006 52
Analyze the Problem◦ Being able to restate the problem is one
indicator that you understand the problem Develop the Structure Chart (or flow chart
of its subparts) Develop the Algorithms Develop the Data Dictionary Develop the Decision Table Develop the Code Test the Solution
McManusCOP1006 53
McManusCOP1006 54