lesson 6 mcmanus cop1006 1. the decision logic structure the if instructions using...

54
Lesson 6 McManus COP1006 1

Upload: archibald-jefferson

Post on 21-Jan-2016

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

Lesson 6

McManusCOP1006 1

Page 2: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 3: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

Decision◦ True/False/Else

Process Assign

McManusCOP1006 3

Assign

Process

Decision

Page 4: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

McManusCOP1006 4

Snoozeor Off?

Alarm goes off!

Sleep or Get up?

Zzzzz!

Snooze

Go Back to Sleep

Get Dressed. . .

Snooze= 9?

< 9

Oh, no!

Page 5: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 6: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 7: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 8: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

All conditions are tested.

Least efficient, but most exhaustive.

McManusCOP1006 8

TF

TF

Page 9: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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!

Page 10: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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”

Page 11: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 12: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

McManusCOP1006 12

Page 13: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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”

Page 14: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 15: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 16: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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”

Page 17: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 18: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 19: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

The If/Then/Else permits multiple questions Always place the most likely to occur first.

◦ Increases efficiency

McManusCOP1006 19

Page 20: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 21: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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?

Page 22: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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}

Page 23: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 24: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 25: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

McManusCOP1006 25

Page 26: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 27: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

McManusCOP1006 27

Case a actionCase aTrue

False

Case b

False

Case b actionTrue

Case Else actionCase Else

Page 28: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 29: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 30: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

McManusCOP1006 30

Switch a action

Switch aTrue

False

Switch b action

Switch bTrue

False

Switch c actionSwitch c

True

Page 31: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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.

Page 32: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

McManusCOP1006 32

Page 33: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 34: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 35: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

McManusCOP1006 35

How would you set up a Decision Table

for Numeric Grades associated with Letter Grades?

Page 36: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 37: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

McManusCOP1006 37

Page 38: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

McManusCOP1006 38

Page 39: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

McManusCOP1006 39

If A > B

If A > CIf B > C

Largest = C

Largest = B

Largest = C

Largest = A

True

TrueTrue

False

False

False

Page 40: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 41: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 42: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 43: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

In Visual Basic

Note: Again, up this point it didn’t matter what language we use…

McManusCOP1006 43

Page 44: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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!

Page 45: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

McManusCOP1006 45

Option Explicit On

' Input Values by UserDim A As ShortDim B As ShortDim C As Short

Page 46: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 47: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 48: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

Private Sub cmdPrintResult_Click()

lblOutput.Text = ("A = " & CStr(A) & _ " B = " & CStr(B) & _

" C = " & CStr(C))

End Sub

McManusCOP1006 48

Page 49: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

McManusCOP1006 49

Page 50: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

McManusCOP1006 50

1st step2nd

step 3rd step

Page 51: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

McManusCOP1006 51

Page 52: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 53: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

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

Page 54: Lesson 6 McManus COP1006 1.  The Decision Logic Structure  The If Instructions  Using Straight-through Logic  Using Positive Logic  Using Negative

McManusCOP1006 54