Download - Chapter 05
![Page 1: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/1.jpg)
Microsoft Visual Basic 2010
CHAPTER FIVE
Decision Structures
![Page 2: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/2.jpg)
5
Chapter 5: Decision Structures 2
Objectives
►Use the GroupBox object►Place RadioButton objects in applications►Display a message box►Make decisions using If…Then statements►Make decisions using If…Then…Else statements►Make decisions using nested If statements
![Page 3: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/3.jpg)
5
Chapter 5: Decision Structures 3
Objectives
►Make decisions using logical operators►Make decisions using Case statements►Insert code snippets►Test input to ensure a value is numeric
![Page 4: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/4.jpg)
5
Chapter 5: Decision Structures 4
Using the GroupBox Object
►Drag the GroupBox object in the Containers category of the Toolbox over the Form object to the approximate location where you want to place the GroupBox object
►When the mouse pointer is in the correct location, release the left mouse button. With the GroupBox object selected, scroll in the Properties window to the (Name) property. Double-click in the right column of the (Name) property and then enter the name grpWoodType
►Click to the right of the Size property of the GroupBox object and enter 125,100 as the size. Change the Font property to Goudy Old Style, Regular, Size 12. Change the BackColor property to White
![Page 5: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/5.jpg)
5
Chapter 5: Decision Structures 5
Using the GroupBox Object
![Page 6: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/6.jpg)
5
Chapter 5: Decision Structures 6
Adding the RadioButton Objects
►Drag and drop one RadioButton object from the Toolbox into the GroupBox object on the Form object.Drag a second RadioButton object from the Toolbox into the GroupBox object, using blue snap lines to align and separate the RadioButton objects vertically
►Release the left mouse button to place the RadioButton object on the Form object within the GroupBox object. Using the same technique, add a third RadioButton object
![Page 7: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/7.jpg)
5
Chapter 5: Decision Structures 7
Adding the RadioButton Objects
►Name the RadioButton objects by selecting a RadioButton object, double-clicking in the right column of the (Name) property in the Properties window, and entering the name. The names for the radio buttons, from top to bottom, should be radPine, radOak, and radCherry
►Change the Text property for each RadioButton by double-clicking in the right column of the Text property and typing Pine for the first RadioButton, Oak for the second RadioButton and Cherry for the third RadioButton
![Page 8: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/8.jpg)
5
Chapter 5: Decision Structures 8
Adding the RadioButton Objects
![Page 9: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/9.jpg)
5
Chapter 5: Decision Structures 9
Windows Application Container Objects
![Page 10: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/10.jpg)
5
Chapter 5: Decision Structures 10
Displaying a Message Box
![Page 11: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/11.jpg)
5
Chapter 5: Decision Structures 11
Displaying a Message Box
![Page 12: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/12.jpg)
5
Chapter 5: Decision Structures 12
Displaying a Message Box
![Page 13: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/13.jpg)
5
Chapter 5: Decision Structures 13
Displaying a Message Box
![Page 14: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/14.jpg)
5
Chapter 5: Decision Structures 14
Displaying a Message Box
![Page 15: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/15.jpg)
5
Chapter 5: Decision Structures 15
Message Box IntelliSense
►In the code editing window, inside the event handler you are coding, type msg to display MsgBox in the IntelliSense list
►Press the Tab key to select MsgBox from the IntelliSense list. Type the following text: (“You have been disconnected from the Internet”, m)
►Select the MsgBoxStyle.AbortRetryIgnore argument by pressing the UP ARROW until the correct argument is highlighted.Type a comma.Then type "ISP” and a right parenthesis
►Click the Start Debugging button on the Standard toolbar
![Page 16: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/16.jpg)
5
Chapter 5: Decision Structures 16
Displaying a Message Box
![Page 17: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/17.jpg)
5
Chapter 5: Decision Structures 17
Making Decisions with Conditional Statements: Using an If…Then Statement
►A decision structure is one of the three fundamental control structures used in computer programming
►When a condition is tested in a Visual Basic program, the condition either is true or false
![Page 18: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/18.jpg)
5
Chapter 5: Decision Structures 18
Relational Operators
![Page 19: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/19.jpg)
5
Chapter 5: Decision Structures 19
Relational Operators
►With the insertion point located in the correct location in the code, type if and then press the SPACEBAR
►Type inta to select the variable named intAge in the IntelliSense list. Then, type >=18 as the condition to be tested. Press the ENTER key
►On the blank line, enter the statement that should be executed when the condition is true. To place the message, “You are old enough to vote” in the Text property of the lblVotingEligibility Label object, insert the code shown in Figure 5-33 on page 315. Remember to use IntelliSense to reference the lblVotingEligibility Label object
![Page 20: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/20.jpg)
5
Chapter 5: Decision Structures 20
Comparing Strings
►A string value comparison compares each character in two strings, starting with the first character in each string
![Page 21: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/21.jpg)
5
Chapter 5: Decision Structures 21
Comparing Different Data Types
►Every type of data available in Visual Basic can be compared• Different numeric types can be compared to
each other• A single string character can be compared to a
Char data type
![Page 22: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/22.jpg)
5
Chapter 5: Decision Structures 22
Using the If…Then…Else Statement
![Page 23: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/23.jpg)
5
Chapter 5: Decision Structures 23
Using the If…Then…ElseIf Statement
![Page 24: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/24.jpg)
5
Chapter 5: Decision Structures 24
Nested If Statements
![Page 25: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/25.jpg)
5
Chapter 5: Decision Structures 25
Nested If Statements
![Page 26: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/26.jpg)
5
Chapter 5: Decision Structures 26
Matching If, Else, and End If Entries
►If statements must be fully contained within the outer If statement
►Place the correct statements with the correct If and Else statements within the nested If statement• This illustration shows incorrect logic
![Page 27: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/27.jpg)
5
Chapter 5: Decision Structures 27
Testing the Status of a RadioButton Object in Code
![Page 28: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/28.jpg)
5
Chapter 5: Decision Structures 28
Block-Level Scope
►Scope is defined by where the variable is declared within a program
►Within an event handler, an If…Then…Else statement is considered a block of code
►Variables can be declared within a block of code• The variable can be referenced only within the
block of code where it is declared
![Page 29: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/29.jpg)
5
Chapter 5: Decision Structures 29
Using Logical Operators
►When more than one condition is included in an If...Then...Else statement, the conditions are called a compound condition
![Page 30: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/30.jpg)
5
Chapter 5: Decision Structures 30
Using the And Logical Operator
![Page 31: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/31.jpg)
5
Chapter 5: Decision Structures 31
Using the Or Logical Operator
![Page 32: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/32.jpg)
5
Chapter 5: Decision Structures 32
Using the Not Logical Operator
![Page 33: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/33.jpg)
5
Chapter 5: Decision Structures 33
Other Logical Operators
![Page 34: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/34.jpg)
5
Chapter 5: Decision Structures 34
Order of Operations for Logical Operators
![Page 35: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/35.jpg)
5
Chapter 5: Decision Structures 35
Select Case Statement
►In some programming applications, different operations can occur based upon the value in a single field
![Page 36: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/36.jpg)
5
Chapter 5: Decision Structures 36
Select Case Statement
![Page 37: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/37.jpg)
5
Chapter 5: Decision Structures 37
Select Case Test Expressions
![Page 38: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/38.jpg)
5
Chapter 5: Decision Structures 38
Using Relational Operators in a Select Case Statement
![Page 39: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/39.jpg)
5
Chapter 5: Decision Structures 39
Using Ranges in Select Case Statements
![Page 40: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/40.jpg)
5
Chapter 5: Decision Structures 40
Selecting Which Decision Structure to Use
►You might be faced with determining if you should use the Select Case statement or the If...Then...ElseIf statement to solve a problem
►Generally, the Select Case statement is most useful when more than two or three values must be tested for a given variable
►The If...Then...ElseIf statement is more flexible• More than one variable can be used in the
comparison• Compound conditions with the And, Or, and
Not logical operators can be used
![Page 41: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/41.jpg)
5
Chapter 5: Decision Structures 41
Code Snippets
►Right-click the line in the code editing window where you want to insert the snippet
►Click Insert Snippet on the shortcut menu►Double-click the folder Code Patterns - If, For Each,Try
Catch, Property, etc, which contains commonly used code such as the If . . . Then . . . Else statement
►Double-click the Conditionals and Loops folder because an If...Then...Else statement is a conditional statement
►Double-click the If...Else...End If Statement code snippet
![Page 42: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/42.jpg)
5
Chapter 5: Decision Structures 42
Code Snippets
![Page 43: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/43.jpg)
5
Chapter 5: Decision Structures 43
Validating Data
►Developers should anticipate that users will enter invalid data
►Developers must write code that will prevent the invalid data from being used in the program to produce invalid output
![Page 44: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/44.jpg)
5
Chapter 5: Decision Structures 44
Testing Input to Determine If the Value Is Numeric
►The Visual Basic IsNumeric function can check the input value to determine if the value can be converted into a numeric value such as an Integer or Decimal data type
![Page 45: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/45.jpg)
5
Chapter 5: Decision Structures 45
Checking for a Positive Number
![Page 46: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/46.jpg)
5
Chapter 5: Decision Structures 46
Program Design
![Page 47: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/47.jpg)
5
Chapter 5: Decision Structures 47
Program Design
![Page 48: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/48.jpg)
5
Chapter 5: Decision Structures 48
Program Design
![Page 49: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/49.jpg)
5
Chapter 5: Decision Structures 49
Chapter Summary
►Use the GroupBox object►Place RadioButton objects in applications►Display a message box►Make decisions using If…Then statements►Make decisions using If…Then…Else statements►Make decisions using nested If statements
![Page 50: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/50.jpg)
5
Chapter 5: Decision Structures 50
Chapter Summary
►Make decisions using logical operators►Make decisions using Case statements►Insert code snippets►Test input to ensure a value is numeric
![Page 51: Chapter 05](https://reader035.vdocuments.site/reader035/viewer/2022062419/557ab179d8b42a74488b4e7c/html5/thumbnails/51.jpg)
Microsoft Visual Basic 2010
CHAPTER FIVE COMPLETE
Decision Structures