Chapter 5 Decisions Chapter 5 Visual Basic Schneider

Chapter 5 Decisions Chapter 5 - Visual Basic Schneider 1

Outline and Objectives n. Relational and Logical Operators n. If Blocks n. Select Case Blocks Chapter 5 - Visual Basic Schneider 2

Relational Operators n n n The execution of an If block is controlled by a condition A condition can be either true or false Conditions can be formed by using the six Relational operators and the three Logical operators Chapter 5 - Visual Basic Schneider 3

Relational Operators Chapter 5 - Visual Basic Schneider 4

Using Relational Operators on Strings n n Computers use a special coding system to compare character strings called ANSI The ANSI values of characters are given in Appendix A. A snippet included in Slide 34 You do not need to memorize the table Just know the numbers, small and capital letters “ 0” = 48 “a” = 97 “A”= 65 Punctuation < Capitals letters < Small letters Chapter 5 - Visual Basic Schneider 5

Example of Comparing Character Strings “Chase” < “Chaz” True “ Cat” < “Cat” True “Pay” < “Pay “ True “Jones” <> “James” True “Hope” True < “Hopeful” Chapter 5 - Visual Basic Schneider 6

Logical Operators The result of a logical operator is also True or False n The three Logical Operators are: Not And Or n Precedence? ! n Chapter 5 - Visual Basic Schneider 7

Not n Not: Negates a single expression n Example: Suppose answ = “Y” Not (answ = “y”) is True Chapter 5 - Visual Basic Schneider 8

And n Takes two expressions, returns True only if both expressions are evaluated as being true n Example: ä (answ Suppose answ = “Y”) And (answ = “y”) is False Chapter 5 - Visual Basic Schneider 9

Or n Takes two expressions, returns true if either one of the expressions is evaluated as being true. n Example: Suppose answ = “Y” ä (answ = “Y”) Or (answ = “y”) is True Chapter 5 - Visual Basic Schneider 10

Determine whether the following conditions are true or false? n (“Y” <> “X”) And (143. 55 < 143. 55) n (0 = 14) Or (6 ^ 2 - 3 <= 4 / 2 + 8) n (Not (6 = 7)) And (44 > 33) Chapter 5 - Visual Basic Schneider 11

Truth Tables X Y X And Y X Or Y Not X F F T F T T T F F T T T T F Not ( X And Y ) = n Not ( X Or Y ) = n Chapter 5 - Visual Basic Schneider 12

Determine if True or False a = 2, b=3 n a*a < b Or Not a*a <a n True n ((a=b) Or Not (b<a)) And ((a<b) Or (b=a+1)) n True n “ 9 W” >= “ 9 W” And “duck” < “Du” & ”ck” n False n Chapter 5 - Visual Basic Schneider 13

Are the conditions equivelent? Not(a<m); n>m n (a=b) and (a<b); a<>b n Not ((a=b) or (a=c)) ; a<>b and a<>c n Not (a>=b); (a<b) and not (a=b) n Chapter 5 - Visual Basic Schneider 14

Write an Exp equal to the negation of a>b n (a<b) and (c<>d) n (a=b) or (a=d) n Not ( (a=b) or (a>b) ) n (a<> “”) and (a<b) and (len(a) < 5) n Chapter 5 - Visual Basic Schneider 15

Types of Decision Structures n If Block Statement ä Single alternative: If. . . Then ä Compound alternative: If Then. . . Else n Select Case Statement Chapter 5 - Visual Basic Schneider 16

Single Alternative Decision n An action is taken if the condition is true, otherwise the control goes to the next statement. Syntax: If condition Then action End If If condition is true, action is executed. Otherwise, action is skipped Chapter 5 - Visual Basic Schneider 17

Example of single-alternative decision If (grade > = 90) Then pic. Output. Print “Excellent Student” letter. Grade = “A” pic. Output. Print “Your grade is “; letter. Grade End If Chapter 5 - Visual Basic Schneider 18

Compound Alternative Decision n Syntax If condition Then action 1 Else action 2 End If Chapter 5 - Visual Basic Schneider 19

Example of Compound If statement Private Sub cmd. Find. Larger_Click() Dim larger. Num As Single If Val(txt. First. Num. Text) > Val(txt. Second. Num. Text) Then larger. Num = Val(txt. First. Num. Text) Else larger. Num = Val(txt. Second. Num. Text) End If pic. Result. Print "The larger number is"; larger. Num End Sub Chapter 5 - Visual Basic Schneider 20

Important Points in Using If Statement Use indentation n In the nested If statement, each If must have its own End If statement n Care should be taken to make If blocks easy to read and understand n Chapter 5 - Visual Basic Schneider 21

Example If cond 1 Then If cond 2 Then action(s) End If If cond 1 And cond 2 Then action(s) End If A confusing If Block This is easier to understand Chapter 5 - Visual Basic Schneider 22

Compound Alternative Decision n Syntax If condition 1 Then action 1 Else. If condition 2 Then action 2 Else. If condittion 3 Then action 3 Else action 4 End If Chapter 5 - Visual Basic Schneider 23

Example (find the larger of two numbers, and report if the two numbers are equal) Private Sub cmd. Find. Larger_Click() pic. Result. Cls If Val(txt. First. Num. Text) > Val(txt. Second. Num. Text) Then pic. Result. Print "The larger number is"; txt. First. Num. Text Else. If Val(txt. Second. Num. Text) > Val(txt. First. Num. Text) Then pic. Result. Print "The larger number is "; txt. Second. Num. Text Else pic. Result. Print "The two numbers are equal. " End If End Sub Chapter 5 - Visual Basic Schneider 24

Example (find cost of phone call from NY to LA) Private Sub Display. Cost (length As Single) Pic. Output. Print "Cost of call: "; Format. Currency(Cost(length)) End Sub Private Function Cost (length As Single) As Single If length < 1 Then Cost =. 46 Else Cost =. 46 + (length – 1) *. 36 End If End Function Chapter 5 - Visual Basic Schneider 25

Single Line If Statement n Syntax If condition Then action There is no End If or Else blocks n If the condition is true, the action will be executed n If the condition is false, no action will be taken and the next statement will be executed n Chapter 5 - Visual Basic Schneider 26

Examples If 5 Then action(s) End If If n Then action(s) End If If Not(n < m) Then action(s) End If Equivalent to If (n >= m) Then If 2 < n < 5 Then action(s) End If Wrong! (n > 2) And (n < 5) Chapter 5 - Visual Basic Schneider 27

The Select Case Block Similar to If statement n Used instead of compound If statements n Action is selected from a list of alternatives n Avoids confusion of deeply nested If blocks n Chapter 5 - Visual Basic Schneider 28

Select Case Block (Syntax) Select Case selector Case value-list-1 action 1 Case value-list-2 action 2 …. . Case Else action of last resort End Select Chapter 5 - Visual Basic Schneider 29

Select Case Block Each value-list contains one or more of the following types of items separated by a comma n n n a constant a variable an expression n an inequality sign preceded by Is and followed by a constant, variable, or expression n a range expressed in the form a To b, where a and b are constants, variables, or expressions. Chapter 5 - Visual Basic Schneider 30

Example of Select Case letter. Grade = txt. Grade. text Select Case letter. Grade Case “A”, “B” pic. Output. Print “ Good Work” Case “C” pic. Output. Print “Average Work” Case Else pic. Output. Print “Poor Work” End Select Chapter 5 - Visual Basic Schneider 31

Example of Select Case letter. Grade = txt. Grade. text Select Case letter. Grade If(letter. Grade = “A” or letter. Grade=“B”) The Case “A”, “B” pic. Output. Print “ Good Work” pic. Output. Print “ Good Elseif (letter. Grade=“C”) Then Work” pic. Output. Print “Average Work” Case “C” Else pic. Output. Print “Average pic. Output. Print “Poor Work” End if Case Else pic. Output. Print “Poor Work” End Select Chapter 5 - Visual Basic Schneider 32

Example of If statement letter. Grade = txt. Grade. Text If (letter. Grade = “A”) Or (letter. Grade = “B”) Then pic. Output. print “Good Work” Else. If (letter. Grade = “C”) Then pic. Output. Print “Average Work” Else pic. Output. Print “Poor Work” End If Chapter 5 - Visual Basic Schneider 33

Several different types of value-list Private Sub cmd. Interpret_Click() Dim x As Integer, y As Integer, num As Integer x=2 num = Val(txt. Number. Text) Select Case num Case 3 - x, x pic. Phrase. Print "Buckle my shoe. " Case Is <= 4 pic. Phrase. Print "Shut the door. " Case x + 3 To x * 3 pic. Phrase. Print "Pick up sticks. " Case Else pic. Phrase. Print "Start all over again. “ End Select End Sub Chapter 5 - Visual Basic Schneider 34

Selector Is a String Variable Private Sub cmd. Interpret_Click() Select Case txt. First. Name. Text Case "Thomas" pic. Solution. Print "Correct. " Case "Woodrow" pic. Solution. Print "Sorry, his full name was" pic. Solution. Print "Thomas Woodrow Wilson. " Case "President" pic. Solution. Print "Are you for real? " Case Else pic. Solution. Print "Nice try, but no cigar. " End Select End Sub Chapter 5 - Visual Basic Schneider 35

Chapter 5 - Visual Basic Schneider 36
- Slides: 36