SlideShare a Scribd company logo
CHAPTER FIVEDecision Structures
Chapter 5: Decision Structures2ObjectivesUse the GroupBox objectPlace RadioButton objects in applicationsDisplay a message boxMake decisions using If…Then statementsMake decisions using If…Then…Else statementsMake decisions using nested If statements
Chapter 5: Decision Structures3ObjectivesMake decisions using logical operatorsMake decisions using Case statementsInsert code snippetsTest input to ensure a value is numeric
Chapter 5: Decision Structures4Using the GroupBox ObjectDrag 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 objectWhen 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 grpWoodTypeClick 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 Structures5Using the GroupBox Object
Chapter 5: Decision Structures6Adding the RadioButton ObjectsDrag 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 verticallyRelease 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 Structures7Adding the RadioButton ObjectsName 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 radCherryChange 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 Structures8Adding the RadioButton Objects
Chapter 5: Decision Structures9Windows Application Container Objects
Chapter 5: Decision Structures10Displaying a Message Box
Chapter 5: Decision Structures11Displaying a Message Box
Chapter 5: Decision Structures12Displaying a Message Box
Chapter 5: Decision Structures13Displaying a Message Box
Displaying a Message BoxChapter 5: Decision Structures14
Chapter 5: Decision Structures15Message Box IntelliSenseIn the code editing window, inside the event handler you are coding, type msgto display MsgBox in the IntelliSense listPress 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 Structures16Displaying a Message Box
Chapter 5: Decision Structures17Making Decisions with Conditional Statements: Using an If…Then StatementA decision structure is one of the three fundamental control structures used in computer programmingWhen a condition is tested in a Visual Basic program, the condition either is true or false
Chapter 5: Decision Structures18Relational Operators
Chapter 5: Decision Structures19Relational OperatorsWith the insertion point located in the correct location in the code, type ifand then press the SPACEBARType inta to select the variable named intAge in the IntelliSense list. Then, type >=18as the condition to be tested. Press the ENTER keyOn 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 Structures20Comparing StringsA string value comparison compares each character in two strings, starting with the first character in each string
Chapter 5: Decision Structures21Comparing Different Data TypesEvery type of data available in Visual Basic can be comparedDifferent numeric types can be compared to each otherA single string character can be compared to a Char data type
Using the If…Then…Else StatementChapter 5: Decision Structures22
Chapter 5: Decision Structures23Using the If…Then…ElseIf Statement
Nested If StatementsChapter 5: Decision Structures24
Nested If StatementsChapter 5: Decision Structures25
Chapter 5: Decision Structures26Matching If, Else, and End If EntriesIf statements must be fully contained within the outer If statementPlace the correct statements with the correct If and Else statements within the nested If statementThis illustration shows incorrect logic
Testing the Status of a RadioButton Object in CodeChapter 5: Decision Structures27
Chapter 5: Decision Structures28Block-Level ScopeScope is defined by where the variable is declared within a programWithin an event handler, an If…Then…Else statement is considered a block of codeVariables can be declared within a block of codeThe variable can be referenced only within the block of code where it is declared
Chapter 5: Decision Structures29Using Logical OperatorsWhen more than one condition is included in an If...Then...Else statement, the conditions are called a compound condition
Using the And Logical OperatorChapter 5: Decision Structures30
Using the Or Logical OperatorChapter 5: Decision Structures31
Using the Not Logical OperatorChapter 5: Decision Structures32
Chapter 5: Decision Structures33Other Logical Operators
Chapter 5: Decision Structures34Order of Operations for Logical Operators
Chapter 5: Decision Structures35Select Case StatementIn some programming applications, different operations can occur based upon the value in a single field
Select Case StatementChapter 5: Decision Structures36
Chapter 5: Decision Structures37Select Case Test Expressions
Chapter 5: Decision Structures38Using Relational Operators in a Select Case Statement
Using Ranges in Select Case StatementsChapter 5: Decision Structures39
Chapter 5: Decision Structures40Selecting Which Decision Structure to UseYou might be faced with determining if you should use the Select Case statement or the If...Then...ElseIf statement to solve a problemGenerally, the Select Case statement is most useful when more than two or three values must be tested for a given variableThe If...Then...ElseIf statement is more flexibleMore than one variable can be used in the comparisonCompound conditions with the And, Or, and Not logical operators can be used
Chapter 5: Decision Structures41Code SnippetsRight-click the line in the code editing window where you want to insert the snippetClick Insert Snippet on the shortcut menuDouble-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 statementDouble-click the If...Else...End If Statement code snippet
Chapter 5: Decision Structures42Code Snippets
Chapter 5: Decision Structures43Validating DataDevelopers should anticipate that users will enter invalid dataDevelopers must write code that will prevent the invalid data from being used in the program to produce invalid output
Chapter 5: Decision Structures44Testing Input to Determine If the Value Is NumericThe 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 Structures45Checking for a Positive Number
Program DesignChapter 5: Decision Structures46
Program DesignChapter 5: Decision Structures47
Program DesignChapter 5: Decision Structures48
Chapter 5: Decision Structures49Chapter SummaryUse the GroupBox objectPlace RadioButton objects in applicationsDisplay a message boxMake decisions using If…Then statementsMake decisions using If…Then…Else statementsMake decisions using nested If statements
Chapter 5: Decision Structures50Chapter SummaryMake decisions using logical operatorsMake decisions using Case statementsInsert code snippetsTest input to ensure a value is numeric
CHAPTER FIVE COMPLETEDecision Structures

More Related Content

PPT
HCI 3e - Ch 3: The interaction
PPTX
Introduction to Distributed System
PPTX
Design process design rules
PDF
Lecture 4: Human-Computer Interaction: Prototyping (2014)
PPSX
Cocomo model
PPTX
Discovering Computers: Chapter 05
PPT
HCI 3e - Ch 5: Interaction design basics
PPTX
Fault tolerance in distributed systems
HCI 3e - Ch 3: The interaction
Introduction to Distributed System
Design process design rules
Lecture 4: Human-Computer Interaction: Prototyping (2014)
Cocomo model
Discovering Computers: Chapter 05
HCI 3e - Ch 5: Interaction design basics
Fault tolerance in distributed systems

What's hot (20)

DOCX
PPS UNIT 1- R18.docx
PPTX
Human computer interaction
PPT
Chapter 4 universal design
PDF
Introduction to Operating Systems
PPT
Lecture 5 - Structured Programming Language
PPTX
Design Concept software engineering
PPT
HCI - Chapter 2
PPT
Introduction to HCI
PPT
HCI - Chapter 6
PPTX
Operating system security
PPTX
Object Oriented Analysis & Design
PPT
key distribution in network security
PDF
User Interface Design - Module 1 Introduction
PPT
HCI 3e - Ch 15: Task analysis
PPT
Unit 1 - Introduction to Software Engineering.ppt
PPT
Computer Programming - Lecture 2
PPTX
Sequence diagram
PPTX
Requirement Elicitation and Analysis.pptx
PPT
Networkingconcepts
PDF
Multichannel User Interfaces
PPS UNIT 1- R18.docx
Human computer interaction
Chapter 4 universal design
Introduction to Operating Systems
Lecture 5 - Structured Programming Language
Design Concept software engineering
HCI - Chapter 2
Introduction to HCI
HCI - Chapter 6
Operating system security
Object Oriented Analysis & Design
key distribution in network security
User Interface Design - Module 1 Introduction
HCI 3e - Ch 15: Task analysis
Unit 1 - Introduction to Software Engineering.ppt
Computer Programming - Lecture 2
Sequence diagram
Requirement Elicitation and Analysis.pptx
Networkingconcepts
Multichannel User Interfaces
Ad

Viewers also liked (16)

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

Similar to Chapter 05 (20)

PPSX
Chapter 05 show
PPT
CHPATER OF VISUAL PROGRAMMING IN WHICH ALL
PPT
Ppt lesson 09
PPTX
Vb6.0 intro
PPTX
Chapter - 6.pptx
PPT
Lect02 Introducing Programming.ppt
PPT
Vb introduction.
PPTX
Getting started with the visual basic editor
PPTX
Visual Programming
PDF
Diving into VS 2015 Day5
PPTX
Tugas testing
DOCX
Visual C# 2010
PDF
Notes how to work with variables, constants and do calculations
PDF
PDF
Gui builder
DOC
Practicalfileofvb workshop
PPT
Ppt lesson 08
DOCX
The Wear-Ever Shoes company maintains inventory data and custome.docx
PPT
9781439035665 ppt ch06
Chapter 05 show
CHPATER OF VISUAL PROGRAMMING IN WHICH ALL
Ppt lesson 09
Vb6.0 intro
Chapter - 6.pptx
Lect02 Introducing Programming.ppt
Vb introduction.
Getting started with the visual basic editor
Visual Programming
Diving into VS 2015 Day5
Tugas testing
Visual C# 2010
Notes how to work with variables, constants and do calculations
Gui builder
Practicalfileofvb workshop
Ppt lesson 08
The Wear-Ever Shoes company maintains inventory data and custome.docx
9781439035665 ppt ch06

Chapter 05

  • 2. Chapter 5: Decision Structures2ObjectivesUse the GroupBox objectPlace RadioButton objects in applicationsDisplay a message boxMake decisions using If…Then statementsMake decisions using If…Then…Else statementsMake decisions using nested If statements
  • 3. Chapter 5: Decision Structures3ObjectivesMake decisions using logical operatorsMake decisions using Case statementsInsert code snippetsTest input to ensure a value is numeric
  • 4. Chapter 5: Decision Structures4Using the GroupBox ObjectDrag 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 objectWhen 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 grpWoodTypeClick 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 Structures5Using the GroupBox Object
  • 6. Chapter 5: Decision Structures6Adding the RadioButton ObjectsDrag 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 verticallyRelease 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 Structures7Adding the RadioButton ObjectsName 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 radCherryChange 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 Structures8Adding the RadioButton Objects
  • 9. Chapter 5: Decision Structures9Windows Application Container Objects
  • 10. Chapter 5: Decision Structures10Displaying a Message Box
  • 11. Chapter 5: Decision Structures11Displaying a Message Box
  • 12. Chapter 5: Decision Structures12Displaying a Message Box
  • 13. Chapter 5: Decision Structures13Displaying a Message Box
  • 14. Displaying a Message BoxChapter 5: Decision Structures14
  • 15. Chapter 5: Decision Structures15Message Box IntelliSenseIn the code editing window, inside the event handler you are coding, type msgto display MsgBox in the IntelliSense listPress 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 Structures16Displaying a Message Box
  • 17. Chapter 5: Decision Structures17Making Decisions with Conditional Statements: Using an If…Then StatementA decision structure is one of the three fundamental control structures used in computer programmingWhen a condition is tested in a Visual Basic program, the condition either is true or false
  • 18. Chapter 5: Decision Structures18Relational Operators
  • 19. Chapter 5: Decision Structures19Relational OperatorsWith the insertion point located in the correct location in the code, type ifand then press the SPACEBARType inta to select the variable named intAge in the IntelliSense list. Then, type >=18as the condition to be tested. Press the ENTER keyOn 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 Structures20Comparing StringsA string value comparison compares each character in two strings, starting with the first character in each string
  • 21. Chapter 5: Decision Structures21Comparing Different Data TypesEvery type of data available in Visual Basic can be comparedDifferent numeric types can be compared to each otherA single string character can be compared to a Char data type
  • 22. Using the If…Then…Else StatementChapter 5: Decision Structures22
  • 23. Chapter 5: Decision Structures23Using the If…Then…ElseIf Statement
  • 24. Nested If StatementsChapter 5: Decision Structures24
  • 25. Nested If StatementsChapter 5: Decision Structures25
  • 26. Chapter 5: Decision Structures26Matching If, Else, and End If EntriesIf statements must be fully contained within the outer If statementPlace the correct statements with the correct If and Else statements within the nested If statementThis illustration shows incorrect logic
  • 27. Testing the Status of a RadioButton Object in CodeChapter 5: Decision Structures27
  • 28. Chapter 5: Decision Structures28Block-Level ScopeScope is defined by where the variable is declared within a programWithin an event handler, an If…Then…Else statement is considered a block of codeVariables can be declared within a block of codeThe variable can be referenced only within the block of code where it is declared
  • 29. Chapter 5: Decision Structures29Using Logical OperatorsWhen more than one condition is included in an If...Then...Else statement, the conditions are called a compound condition
  • 30. Using the And Logical OperatorChapter 5: Decision Structures30
  • 31. Using the Or Logical OperatorChapter 5: Decision Structures31
  • 32. Using the Not Logical OperatorChapter 5: Decision Structures32
  • 33. Chapter 5: Decision Structures33Other Logical Operators
  • 34. Chapter 5: Decision Structures34Order of Operations for Logical Operators
  • 35. Chapter 5: Decision Structures35Select Case StatementIn some programming applications, different operations can occur based upon the value in a single field
  • 36. Select Case StatementChapter 5: Decision Structures36
  • 37. Chapter 5: Decision Structures37Select Case Test Expressions
  • 38. Chapter 5: Decision Structures38Using Relational Operators in a Select Case Statement
  • 39. Using Ranges in Select Case StatementsChapter 5: Decision Structures39
  • 40. Chapter 5: Decision Structures40Selecting Which Decision Structure to UseYou might be faced with determining if you should use the Select Case statement or the If...Then...ElseIf statement to solve a problemGenerally, the Select Case statement is most useful when more than two or three values must be tested for a given variableThe If...Then...ElseIf statement is more flexibleMore than one variable can be used in the comparisonCompound conditions with the And, Or, and Not logical operators can be used
  • 41. Chapter 5: Decision Structures41Code SnippetsRight-click the line in the code editing window where you want to insert the snippetClick Insert Snippet on the shortcut menuDouble-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 statementDouble-click the If...Else...End If Statement code snippet
  • 42. Chapter 5: Decision Structures42Code Snippets
  • 43. Chapter 5: Decision Structures43Validating DataDevelopers should anticipate that users will enter invalid dataDevelopers must write code that will prevent the invalid data from being used in the program to produce invalid output
  • 44. Chapter 5: Decision Structures44Testing Input to Determine If the Value Is NumericThe 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 Structures45Checking for a Positive Number
  • 46. Program DesignChapter 5: Decision Structures46
  • 47. Program DesignChapter 5: Decision Structures47
  • 48. Program DesignChapter 5: Decision Structures48
  • 49. Chapter 5: Decision Structures49Chapter SummaryUse the GroupBox objectPlace RadioButton objects in applicationsDisplay a message boxMake decisions using If…Then statementsMake decisions using If…Then…Else statementsMake decisions using nested If statements
  • 50. Chapter 5: Decision Structures50Chapter SummaryMake decisions using logical operatorsMake decisions using Case statementsInsert code snippetsTest input to ensure a value is numeric