SlideShare a Scribd company logo
1 of 51
CHAPTER FIVE Decision Structures
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
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
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 GroupBoxobject 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
Chapter 5: Decision Structures 5 Using the GroupBox Object
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
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
Chapter 5: Decision Structures 8 Adding the RadioButton Objects
Chapter 5: Decision Structures 9 Windows Application Container Objects
Chapter 5: Decision Structures 10 Displaying a Message Box
Chapter 5: Decision Structures 11 Displaying a Message Box
Chapter 5: Decision Structures 12 Displaying a Message Box
Chapter 5: Decision Structures 13 Displaying a Message Box
Displaying a Message Box Chapter 5: Decision Structures 14
Chapter 5: Decision Structures 15 Message Box IntelliSense In the code editing window, inside the event handler you are coding, type msgto 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
Chapter 5: Decision Structures 16 Displaying a Message Box
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
Chapter 5: Decision Structures 18 Relational Operators
Chapter 5: Decision Structures 19 Relational Operators With the insertion point located in the correct location in the code, type ifand then press the SPACEBAR Type inta to select the variable named intAge in the IntelliSense list. Then, type >=18as 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
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
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
Using the If…Then…Else Statement Chapter 5: Decision Structures 22
Chapter 5: Decision Structures 23 Using the If…Then…ElseIf Statement
Nested If Statements Chapter 5: Decision Structures 24
Nested If Statements Chapter 5: Decision Structures 25
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
Testing the Status of a RadioButton Object in Code Chapter 5: Decision Structures 27
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
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
Using the And Logical Operator Chapter 5: Decision Structures 30
Using the Or Logical Operator Chapter 5: Decision Structures 31
Using the Not Logical Operator Chapter 5: Decision Structures 32
Chapter 5: Decision Structures 33 Other Logical Operators
Chapter 5: Decision Structures 34 Order of Operations for Logical Operators
Chapter 5: Decision Structures 35 Select Case Statement In some programming applications, different operations can occur based upon the value in a single field
Select Case Statement Chapter 5: Decision Structures 36
Chapter 5: Decision Structures 37 Select Case Test Expressions
Chapter 5: Decision Structures 38 Using Relational Operators in a Select Case Statement
Using Ranges in Select Case Statements Chapter 5: Decision Structures 39
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
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
Chapter 5: Decision Structures 42 Code Snippets
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
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
Chapter 5: Decision Structures 45 Checking for a Positive Number
Program Design Chapter 5: Decision Structures 46
Program Design Chapter 5: Decision Structures 47
Program Design Chapter 5: Decision Structures 48
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
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
CHAPTER FIVE COMPLETE Decision Structures

More Related Content

What's hot

Scenario-based Design
Scenario-based DesignScenario-based Design
Scenario-based DesignHans Põldoja
 
HCI 2018 (3/10) Design Models, Methodologies and Guidelines
HCI 2018 (3/10) Design Models, Methodologies and GuidelinesHCI 2018 (3/10) Design Models, Methodologies and Guidelines
HCI 2018 (3/10) Design Models, Methodologies and GuidelinesSabin Buraga
 
Human computer interaction
Human computer interactionHuman computer interaction
Human computer interactionemaan waseem
 
HUMAN COMPUTER INTERACTION
HUMAN COMPUTER INTERACTIONHUMAN COMPUTER INTERACTION
HUMAN COMPUTER INTERACTIONJaved Ahmed Samo
 
Basic Principles of Interface design
Basic Principles of Interface designBasic Principles of Interface design
Basic Principles of Interface designZdeněk Lanc
 
Human computer interaction 3 4(revised)
Human computer interaction 3 4(revised)Human computer interaction 3 4(revised)
Human computer interaction 3 4(revised)emaan waseem
 
HCI 3e - Ch 15: Task analysis
HCI 3e - Ch 15:  Task analysisHCI 3e - Ch 15:  Task analysis
HCI 3e - Ch 15: Task analysisAlan Dix
 
HCI 3e - Ch 2: The computer
HCI 3e - Ch 2:  The computerHCI 3e - Ch 2:  The computer
HCI 3e - Ch 2: The computerAlan Dix
 
evaluation techniques in HCI
evaluation techniques in HCIevaluation techniques in HCI
evaluation techniques in HCIsawsan slii
 
Human computer interaction -Input output channel
Human computer interaction -Input output channelHuman computer interaction -Input output channel
Human computer interaction -Input output channelN.Jagadish Kumar
 
Fundamentals of User Interface Design
Fundamentals of User Interface DesignFundamentals of User Interface Design
Fundamentals of User Interface DesignMerlin Rebrović
 
HCI 3e - Ch 5: Interaction design basics
HCI 3e - Ch 5:  Interaction design basicsHCI 3e - Ch 5:  Interaction design basics
HCI 3e - Ch 5: Interaction design basicsAlan Dix
 
Human Computer Interaction HCI
Human Computer Interaction HCI Human Computer Interaction HCI
Human Computer Interaction HCI Gaditek
 

What's hot (20)

Scenario-based Design
Scenario-based DesignScenario-based Design
Scenario-based Design
 
HCI 2018 (3/10) Design Models, Methodologies and Guidelines
HCI 2018 (3/10) Design Models, Methodologies and GuidelinesHCI 2018 (3/10) Design Models, Methodologies and Guidelines
HCI 2018 (3/10) Design Models, Methodologies and Guidelines
 
Human computer interaction
Human computer interactionHuman computer interaction
Human computer interaction
 
HUMAN COMPUTER INTERACTION
HUMAN COMPUTER INTERACTIONHUMAN COMPUTER INTERACTION
HUMAN COMPUTER INTERACTION
 
Basic Principles of Interface design
Basic Principles of Interface designBasic Principles of Interface design
Basic Principles of Interface design
 
Human computer interaction 3 4(revised)
Human computer interaction 3 4(revised)Human computer interaction 3 4(revised)
Human computer interaction 3 4(revised)
 
HCI 3e - Ch 15: Task analysis
HCI 3e - Ch 15:  Task analysisHCI 3e - Ch 15:  Task analysis
HCI 3e - Ch 15: Task analysis
 
HCI Presentation
HCI PresentationHCI Presentation
HCI Presentation
 
Interaction
InteractionInteraction
Interaction
 
HCI 3e - Ch 2: The computer
HCI 3e - Ch 2:  The computerHCI 3e - Ch 2:  The computer
HCI 3e - Ch 2: The computer
 
Hci and psychology
Hci and psychologyHci and psychology
Hci and psychology
 
evaluation techniques in HCI
evaluation techniques in HCIevaluation techniques in HCI
evaluation techniques in HCI
 
Human computer interaction -Input output channel
Human computer interaction -Input output channelHuman computer interaction -Input output channel
Human computer interaction -Input output channel
 
E3 chap-08
E3 chap-08E3 chap-08
E3 chap-08
 
Fundamentals of User Interface Design
Fundamentals of User Interface DesignFundamentals of User Interface Design
Fundamentals of User Interface Design
 
Interactive design basics
Interactive design basicsInteractive design basics
Interactive design basics
 
ppt
ppt ppt
ppt
 
HCI 3e - Ch 5: Interaction design basics
HCI 3e - Ch 5:  Interaction design basicsHCI 3e - Ch 5:  Interaction design basics
HCI 3e - Ch 5: Interaction design basics
 
Human Computer Interaction HCI
Human Computer Interaction HCI Human Computer Interaction HCI
Human Computer Interaction HCI
 
Hci activity#2
Hci activity#2Hci activity#2
Hci activity#2
 

Viewers also liked

Chapter 06
Chapter 06Chapter 06
Chapter 06llmeade
 
Chapter 03
Chapter 03Chapter 03
Chapter 03llmeade
 
Chapter 3 — Program Design and Coding
Chapter 3 — Program Design and Coding Chapter 3 — Program Design and Coding
Chapter 3 — Program Design and Coding francopw
 
Chapter 2 — Program and Graphical User Interface Design
Chapter 2 — Program and Graphical User Interface DesignChapter 2 — Program and Graphical User Interface Design
Chapter 2 — Program and Graphical User Interface Designfrancopw
 
Chapter 1 — Introduction to Visual Basic 2010 Programming
Chapter 1 — Introduction to Visual Basic 2010 Programming Chapter 1 — Introduction to Visual Basic 2010 Programming
Chapter 1 — Introduction to Visual Basic 2010 Programming francopw
 
Introduction to visual basic programming
Introduction to visual basic programmingIntroduction to visual basic programming
Introduction to visual basic programmingRoger Argarin
 
Chapter 04
Chapter 04Chapter 04
Chapter 04llmeade
 
Chapter 4 — Variables and Arithmetic Operations
Chapter 4 — Variables and Arithmetic OperationsChapter 4 — Variables and Arithmetic Operations
Chapter 4 — Variables and Arithmetic Operationsfrancopw
 
Chapter 08
Chapter 08Chapter 08
Chapter 08llmeade
 
Presentation on visual basic 6 (vb6)
Presentation on visual basic 6 (vb6)Presentation on visual basic 6 (vb6)
Presentation on visual basic 6 (vb6)pbarasia
 
Basic controls of Visual Basic 6.0
Basic controls of Visual Basic 6.0Basic controls of Visual Basic 6.0
Basic controls of Visual Basic 6.0Salim M
 
Visual basic ppt for tutorials computer
Visual basic ppt for tutorials computerVisual basic ppt for tutorials computer
Visual basic ppt for tutorials computersimran153
 
Visual Basic Codes And Screen Designs
Visual Basic Codes And Screen DesignsVisual Basic Codes And Screen Designs
Visual Basic Codes And Screen Designsprcastano
 

Viewers also liked (16)

Chapter 06
Chapter 06Chapter 06
Chapter 06
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 
Chapter 3 — Program Design and Coding
Chapter 3 — Program Design and Coding Chapter 3 — Program Design and Coding
Chapter 3 — Program Design and Coding
 
Chapter 2 — Program and Graphical User Interface Design
Chapter 2 — Program and Graphical User Interface DesignChapter 2 — Program and Graphical User Interface Design
Chapter 2 — Program and Graphical User Interface Design
 
Chapter 1 — Introduction to Visual Basic 2010 Programming
Chapter 1 — Introduction to Visual Basic 2010 Programming Chapter 1 — Introduction to Visual Basic 2010 Programming
Chapter 1 — Introduction to Visual Basic 2010 Programming
 
Introduction to visual basic programming
Introduction to visual basic programmingIntroduction to visual basic programming
Introduction to visual basic programming
 
Chapter 04
Chapter 04Chapter 04
Chapter 04
 
Chapter 4 — Variables and Arithmetic Operations
Chapter 4 — Variables and Arithmetic OperationsChapter 4 — Variables and Arithmetic Operations
Chapter 4 — Variables and Arithmetic Operations
 
Chapter 08
Chapter 08Chapter 08
Chapter 08
 
The Best Source Code VB
The Best Source Code VBThe Best Source Code VB
The Best Source Code VB
 
Visual Basic 6.0
Visual Basic 6.0Visual Basic 6.0
Visual Basic 6.0
 
Presentation on visual basic 6 (vb6)
Presentation on visual basic 6 (vb6)Presentation on visual basic 6 (vb6)
Presentation on visual basic 6 (vb6)
 
Visual Basic Controls ppt
Visual Basic Controls pptVisual Basic Controls ppt
Visual Basic Controls ppt
 
Basic controls of Visual Basic 6.0
Basic controls of Visual Basic 6.0Basic controls of Visual Basic 6.0
Basic controls of Visual Basic 6.0
 
Visual basic ppt for tutorials computer
Visual basic ppt for tutorials computerVisual basic ppt for tutorials computer
Visual basic ppt for tutorials computer
 
Visual Basic Codes And Screen Designs
Visual Basic Codes And Screen DesignsVisual Basic Codes And Screen Designs
Visual Basic Codes And Screen Designs
 

Similar to Chapter 05

Chapter 05 show
Chapter 05 showChapter 05 show
Chapter 05 showchda01008
 
Lect02 Introducing Programming.ppt
Lect02 Introducing Programming.pptLect02 Introducing Programming.ppt
Lect02 Introducing Programming.pptNourhanTarek23
 
Getting started with the visual basic editor
Getting started with the visual basic editorGetting started with the visual basic editor
Getting started with the visual basic editorputiadetiara
 
Visual Programming
Visual ProgrammingVisual Programming
Visual ProgrammingBagzzz
 
Diving into VS 2015 Day5
Diving into VS 2015 Day5Diving into VS 2015 Day5
Diving into VS 2015 Day5Akhil Mittal
 
Notes how to work with variables, constants and do calculations
Notes how to work with variables, constants and do calculationsNotes how to work with variables, constants and do calculations
Notes how to work with variables, constants and do calculationsWilliam Olivier
 
Gui builder
Gui builderGui builder
Gui builderlearnt
 
Practicalfileofvb workshop
Practicalfileofvb workshopPracticalfileofvb workshop
Practicalfileofvb workshopdhi her
 
The Wear-Ever Shoes company maintains inventory data and custome.docx
The Wear-Ever Shoes company maintains inventory data and custome.docxThe Wear-Ever Shoes company maintains inventory data and custome.docx
The Wear-Ever Shoes company maintains inventory data and custome.docxpelise1
 
9781439035665 ppt ch06
9781439035665 ppt ch069781439035665 ppt ch06
9781439035665 ppt ch06Terry Yoast
 
Introduction to programming using Visual Basic 6
Introduction to programming using Visual Basic 6Introduction to programming using Visual Basic 6
Introduction to programming using Visual Basic 6Jeanie Arnoco
 

Similar to Chapter 05 (20)

Chapter 05 show
Chapter 05 showChapter 05 show
Chapter 05 show
 
Ppt lesson 09
Ppt lesson 09Ppt lesson 09
Ppt lesson 09
 
Vb6.0 intro
Vb6.0 introVb6.0 intro
Vb6.0 intro
 
Chapter - 6.pptx
Chapter - 6.pptxChapter - 6.pptx
Chapter - 6.pptx
 
Lect02 Introducing Programming.ppt
Lect02 Introducing Programming.pptLect02 Introducing Programming.ppt
Lect02 Introducing Programming.ppt
 
Vb introduction.
Vb introduction.Vb introduction.
Vb introduction.
 
Getting started with the visual basic editor
Getting started with the visual basic editorGetting started with the visual basic editor
Getting started with the visual basic editor
 
Visual Programming
Visual ProgrammingVisual Programming
Visual Programming
 
Diving into VS 2015 Day5
Diving into VS 2015 Day5Diving into VS 2015 Day5
Diving into VS 2015 Day5
 
Tugas testing
Tugas testingTugas testing
Tugas testing
 
Visual C# 2010
Visual C# 2010Visual C# 2010
Visual C# 2010
 
Notes how to work with variables, constants and do calculations
Notes how to work with variables, constants and do calculationsNotes how to work with variables, constants and do calculations
Notes how to work with variables, constants and do calculations
 
Winforms
WinformsWinforms
Winforms
 
Excel300
Excel300Excel300
Excel300
 
Gui builder
Gui builderGui builder
Gui builder
 
Practicalfileofvb workshop
Practicalfileofvb workshopPracticalfileofvb workshop
Practicalfileofvb workshop
 
Ppt lesson 08
Ppt lesson 08Ppt lesson 08
Ppt lesson 08
 
The Wear-Ever Shoes company maintains inventory data and custome.docx
The Wear-Ever Shoes company maintains inventory data and custome.docxThe Wear-Ever Shoes company maintains inventory data and custome.docx
The Wear-Ever Shoes company maintains inventory data and custome.docx
 
9781439035665 ppt ch06
9781439035665 ppt ch069781439035665 ppt ch06
9781439035665 ppt ch06
 
Introduction to programming using Visual Basic 6
Introduction to programming using Visual Basic 6Introduction to programming using Visual Basic 6
Introduction to programming using Visual Basic 6
 

Chapter 05

  • 2. 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
  • 3. 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
  • 4. 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 GroupBoxobject 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
  • 5. Chapter 5: Decision Structures 5 Using the GroupBox Object
  • 6. 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
  • 7. 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
  • 8. Chapter 5: Decision Structures 8 Adding the RadioButton Objects
  • 9. Chapter 5: Decision Structures 9 Windows Application Container Objects
  • 10. Chapter 5: Decision Structures 10 Displaying a Message Box
  • 11. Chapter 5: Decision Structures 11 Displaying a Message Box
  • 12. Chapter 5: Decision Structures 12 Displaying a Message Box
  • 13. Chapter 5: Decision Structures 13 Displaying a Message Box
  • 14. Displaying a Message Box Chapter 5: Decision Structures 14
  • 15. Chapter 5: Decision Structures 15 Message Box IntelliSense In the code editing window, inside the event handler you are coding, type msgto 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
  • 16. Chapter 5: Decision Structures 16 Displaying a Message Box
  • 17. 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
  • 18. Chapter 5: Decision Structures 18 Relational Operators
  • 19. Chapter 5: Decision Structures 19 Relational Operators With the insertion point located in the correct location in the code, type ifand then press the SPACEBAR Type inta to select the variable named intAge in the IntelliSense list. Then, type >=18as 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
  • 20. 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
  • 21. 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
  • 22. Using the If…Then…Else Statement Chapter 5: Decision Structures 22
  • 23. Chapter 5: Decision Structures 23 Using the If…Then…ElseIf Statement
  • 24. Nested If Statements Chapter 5: Decision Structures 24
  • 25. Nested If Statements Chapter 5: Decision Structures 25
  • 26. 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
  • 27. Testing the Status of a RadioButton Object in Code Chapter 5: Decision Structures 27
  • 28. 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
  • 29. 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
  • 30. Using the And Logical Operator Chapter 5: Decision Structures 30
  • 31. Using the Or Logical Operator Chapter 5: Decision Structures 31
  • 32. Using the Not Logical Operator Chapter 5: Decision Structures 32
  • 33. Chapter 5: Decision Structures 33 Other Logical Operators
  • 34. Chapter 5: Decision Structures 34 Order of Operations for Logical Operators
  • 35. Chapter 5: Decision Structures 35 Select Case Statement In some programming applications, different operations can occur based upon the value in a single field
  • 36. Select Case Statement Chapter 5: Decision Structures 36
  • 37. Chapter 5: Decision Structures 37 Select Case Test Expressions
  • 38. Chapter 5: Decision Structures 38 Using Relational Operators in a Select Case Statement
  • 39. Using Ranges in Select Case Statements Chapter 5: Decision Structures 39
  • 40. 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
  • 41. 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
  • 42. Chapter 5: Decision Structures 42 Code Snippets
  • 43. 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
  • 44. 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
  • 45. Chapter 5: Decision Structures 45 Checking for a Positive Number
  • 46. Program Design Chapter 5: Decision Structures 46
  • 47. Program Design Chapter 5: Decision Structures 47
  • 48. Program Design Chapter 5: Decision Structures 48
  • 49. 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
  • 50. 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
  • 51. CHAPTER FIVE COMPLETE Decision Structures