Download presentation
Presentation is loading. Please wait.
1
Chapter 5 - Visual Basic Schneider1 Chapter 5 Decisions
2
Chapter 5 - Visual Basic Schneider2 Outline and Objective n Relational and Logical Operators n If Blocks n Select Case Blocks
3
Chapter 5 - Visual Basic Schneider3 Relational Operators n The execution of the block If is controlled by a Boolean Expression n A Boolean Expression can be either True or False n Boolean Expressions are formed by using six Relational operators and three Logical operators
4
Chapter 5 - Visual Basic Schneider4 Relational Operators
5
Chapter 5 - Visual Basic Schneider5 Using Relational Operators on Strings n Computer uses a special coding system to compare character strings n Usually ANSI (also called ASCII) coding system is used on PC. n The association between characters and values of characters are given in Appendix A.
6
Chapter 5 - Visual Basic Schneider6 Example of Comparing Character Strings “Chase” < “Chaz” True “ Cat” < “Cat” True “Pay” < “Pay “ True “Jones” <> “James” True “Hope” < “Hopeful” True
7
Chapter 5 - Visual Basic Schneider7 Logical Operators n The result of a logical operator is also True or False n The three Logical Operators are: Not And Or
8
Chapter 5 - Visual Basic Schneider8 Not n Not: Negates a single expression n Example: Suppose answ = “Y” Not ( answ = “y”) is True
9
Chapter 5 - Visual Basic Schneider9 And n Combines two expressions; each expression must be True for the entire expression to be True. n Example: Suppose answ = “Y” ä (answ = “Y”) And (answ = “y”) is False
10
Chapter 5 - Visual Basic Schneider10 Or n Combines two expressions; either expression (or both expressions) must be True for the entire expression to be True. n Example: Suppose answ = “Y” ä (answ = “Y”) Or (answ = “y”) is True
11
Chapter 5 - Visual Basic Schneider11 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)
12
Chapter 5 - Visual Basic Schneider12 Control Structures n Allows the programmer to alter the normal flow of statement execution. n Types of control structures: ä Loop ä Decision
13
Chapter 5 - Visual Basic Schneider13 Types of Decision Structures n The Block If Statement ä Simple alternative if ä Compound alternative if….else n Select Case Statement
14
Chapter 5 - Visual Basic Schneider14 Single Alternative Decision n An action is taken if the condition is true, otherwise the control goes to the next statement.
15
Chapter 5 - Visual Basic Schneider15 Single Alternative Decision n Syntax If condition Then action(s) End If If condition is true, action(s) is executed;otherwise action(s) is skipped
16
Chapter 5 - Visual Basic Schneider16 Example of single-alternative decision If (grade > = 90) Then picOutput.Print “Excellent Student” letterGrade = “A” picOutput.Print “Your grade is “; letterGrade End If
17
Chapter 5 - Visual Basic Schneider17 Compound Alternative Decision n Syntax If condition Then action1 Else action 2 End If
18
Chapter 5 - Visual Basic Schneider18 Example of Compound If statement (find the larger of two numbers) Private Sub cmdFindLarger_Click() Dim largerNum As Single picResult.Cls If Val(txtFirstNum.Text) > Val(txtSecondNum.Text) Then largerNum = Val(txtFirstNum.Text) Else largerNum = Val(txtSecondNum.Text) End If picResult.Print "The larger number is"; largerNum End Sub
19
Chapter 5 - Visual Basic Schneider19 Important Points in Using If Statement n 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 understand
20
Chapter 5 - Visual Basic Schneider20 Example If cond1 Then If cond2 Then action(s) End If If cond1 And cond2 Then action(s) End If This is easier to understand A confusing If Block
21
Chapter 5 - Visual Basic Schneider21 Compound Alternative Decision n Syntax If condition1 Then action1 ElseIf condition2 Then action 2 ElseIf condittion3 Then action3 Else action4 End If
22
Chapter 5 - Visual Basic Schneider22 Example (find the larger of two numbers, and report if the two numbers are equal) Private Sub cmdFindLarger_Click() picResult.Cls If Val(txtFirstNum.Text) > Val(txtSecondNum.Text) Then picResult.Print "The larger number is"; txtFirstNum.Text ElseIf Val(txtSecondNum.Text) > Val(txtFirstNum.Text) Then picResult.Print "The larger number is "; txtSecondNum.Text Else picResult.Print "The two numbers are equal." End If End Sub
23
Chapter 5 - Visual Basic Schneider23 Example (find cost of phone call from NY to LA) Private Sub cmdDisplay_Click() Dim length As Single Call InputLength (length) Call DisplayCost(length) End Sub
24
Chapter 5 - Visual Basic Schneider24 Sub DisplayCost & InputLenght Private Sub DisplayCost (length As Single) ' Display the cost of a call PicOutput.Print "Cost of call: " ; FormatCurrency ( Cost (length)) End Sub Private Sub InputLength(length As Single) ' Request the length of a phone call length = Val (InputBox("Duration of the call in minutes? ")) End Sub
25
Chapter 5 - Visual Basic Schneider25 Function Cost Private Function Cost (length As Single) As Single If length < 1 Then Cost =.46 Else Cost =.46 + (length –1) *.36 End If End Function
26
Chapter 5 - Visual Basic Schneider26 The Select Case Block n Similar to If statement n Used instead of nested If statement n An action to be selected from a list of alternatives n Avoids confusion of deeply nested If blocks
27
Chapter 5 - Visual Basic Schneider27 Select Case Block (Syntax) Select Case selector Case value-list-1 action1 Case value-list-2 action2 ….. Case Else action of last resort End Select
28
Chapter 5 - Visual Basic Schneider28 Select Case Block Each value-list contains one or more of the following types of items separated by a comma n a constant n a variable n 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.
29
Chapter 5 - Visual Basic Schneider29 Example of Select Case letterGrade = txtGrade.text Select Case letterGrade Case “A”, “B” picOutput.Print “ Good Work” Case “C” picOutput.Print “Average Work” Case Else picOutputPrint “Poor Work” End Select
30
Chapter 5 - Visual Basic Schneider30 Example of If statement letterGrade = txtGrade.Text If ( letterGrade = “A”) Or (letterGrade = “B”) Then picOutput.print “Good Work” ElseIf (letterGrade = “C”) Then picOutput.Print “ Average Work” Else picOutput.Print “Poor Work” End If
31
Chapter 5 - Visual Basic Schneider31 Several different types of value-list n Private Sub cmdInterpret_Click() Dim x As Integer, y As Integer, num As Integer ' One, Two, Buckle My Shoe picPhrase.Cls x = 2 y = 3 num = Val(txtNumber.Text) Select Case num Case y - x, x picPhrase.Print "Buckle my shoe." Case Is <= 4 picPhrase.Print "Shut the door." Case x + y To x * y picPhrase.Print "Pick up sticks." Case 7, 8 picPhrase.Print "Lay them straight." Case Else picPhrase.Print "Start all over again." End Select End Sub
32
Chapter 5 - Visual Basic Schneider32 Selector Is a String Variable Private Sub cmdInterpret_Click() Dim firstName As String firstName = txtAnswer.Text Select Case firstName Case "Thomas" picSolution.Print "Correct." Case "Woodrow" picSolution.Print "Sorry, his full name was" picSolution.Print "Thomas Woodrow Wilson." Case "President" picSolution.Print "Are you for real?" Case Else picSolution.Print "Nice try, but no cigar." End Select End Sub
33
Chapter 5 - Visual Basic Schneider33 Summary Points n How to use If statements n Select Case statements n How to use relational operators n How to use Not, And, and Or in Boolean expressions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.