CHAPTER FIVE<br />Decision Structures<br />
Chapter 5: Decision Structures<br />2<br />Objectives<br />Use the GroupBox object<br />Place RadioButton objects in appli...
Chapter 5: Decision Structures<br />3<br />Objectives<br />Make decisions using logical operators<br />Make decisions usin...
Chapter 5: Decision Structures<br />4<br />Using the GroupBox Object<br />Drag the GroupBox object in the Containers categ...
Chapter 5: Decision Structures<br />5<br />Using the GroupBox Object<br />
Chapter 5: Decision Structures<br />6<br />Adding the RadioButton Objects<br />Drag and drop one RadioButton object from t...
Chapter 5: Decision Structures<br />7<br />Adding the RadioButton Objects<br />Name the RadioButton objects by selecting a...
Chapter 5: Decision Structures<br />8<br />Adding the RadioButton Objects<br />
Chapter 5: Decision Structures<br />9<br />Windows Application Container Objects<br />
Chapter 5: Decision Structures<br />10<br />Displaying a Message Box<br />
Chapter 5: Decision Structures<br />11<br />Displaying a Message Box<br />
Chapter 5: Decision Structures<br />12<br />Displaying a Message Box<br />
Chapter 5: Decision Structures<br />13<br />Displaying a Message Box<br />
Displaying a Message Box<br />Chapter 5: Decision Structures<br />14<br />
Chapter 5: Decision Structures<br />15<br />Message Box IntelliSense<br />In the code editing window, inside the event han...
Chapter 5: Decision Structures<br />16<br />Displaying a Message Box<br />
Chapter 5: Decision Structures<br />17<br />Making Decisions with Conditional Statements: Using an If…Then Statement<br />...
Chapter 5: Decision Structures<br />18<br />Relational Operators<br />
Chapter 5: Decision Structures<br />19<br />Relational Operators<br />With the insertion point located in the correct loca...
Chapter 5: Decision Structures<br />20<br />Comparing Strings<br />A string value comparison compares each character in tw...
Chapter 5: Decision Structures<br />21<br />Comparing Different Data Types<br />Every type of data available in Visual Bas...
Using the If…Then…Else Statement<br />Chapter 5: Decision Structures<br />22<br />
Chapter 5: Decision Structures<br />23<br />Using the If…Then…ElseIf Statement<br />
Nested If Statements<br />Chapter 5: Decision Structures<br />24<br />
Nested If Statements<br />Chapter 5: Decision Structures<br />25<br />
Chapter 5: Decision Structures<br />26<br />Matching If, Else, and End If Entries<br />If statements must be fully contain...
Testing the Status of a RadioButton Object in Code<br />Chapter 5: Decision Structures<br />27<br />
Chapter 5: Decision Structures<br />28<br />Block-Level Scope<br />Scope is defined by where the variable is declared with...
Chapter 5: Decision Structures<br />29<br />Using Logical Operators<br />When more than one condition is included in an If...
Using the And Logical Operator<br />Chapter 5: Decision Structures<br />30<br />
Using the Or Logical Operator<br />Chapter 5: Decision Structures<br />31<br />
Using the Not Logical Operator<br />Chapter 5: Decision Structures<br />32<br />
Chapter 5: Decision Structures<br />33<br />Other Logical Operators<br />
Chapter 5: Decision Structures<br />34<br />Order of Operations for Logical Operators<br />
Chapter 5: Decision Structures<br />35<br />Select Case Statement<br />In some programming applications, different operati...
Select Case Statement<br />Chapter 5: Decision Structures<br />36<br />
Chapter 5: Decision Structures<br />37<br />Select Case Test Expressions<br />
Chapter 5: Decision Structures<br />38<br />Using Relational Operators in a Select Case Statement<br />
Using Ranges in Select Case Statements<br />Chapter 5: Decision Structures<br />39<br />
Chapter 5: Decision Structures<br />40<br />Selecting Which Decision Structure to Use<br />You might be faced with determi...
Chapter 5: Decision Structures<br />41<br />Code Snippets<br />Right-click the line in the code editing window where you w...
Chapter 5: Decision Structures<br />42<br />Code Snippets<br />
Chapter 5: Decision Structures<br />43<br />Validating Data<br />Developers should anticipate that users will enter invali...
Chapter 5: Decision Structures<br />44<br />Testing Input to Determine If the Value Is Numeric<br />The Visual Basic IsNum...
Chapter 5: Decision Structures<br />45<br />Checking for a Positive Number<br />
Program Design<br />Chapter 5: Decision Structures<br />46<br />
Program Design<br />Chapter 5: Decision Structures<br />47<br />
Program Design<br />Chapter 5: Decision Structures<br />48<br />
Chapter 5: Decision Structures<br />49<br />Chapter Summary<br />Use the GroupBox object<br />Place RadioButton objects in...
Chapter 5: Decision Structures<br />50<br />Chapter Summary<br />Make decisions using logical operators<br />Make decision...
CHAPTER FIVE COMPLETE<br />Decision Structures<br />
Upcoming SlideShare
Loading in …5
×

Chapter 05

1,243 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,243
On SlideShare
0
From Embeds
0
Number of Embeds
155
Actions
Shares
0
Downloads
115
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter 05

  1. 1. CHAPTER FIVE<br />Decision Structures<br />
  2. 2. Chapter 5: Decision Structures<br />2<br />Objectives<br />Use the GroupBox object<br />Place RadioButton objects in applications<br />Display a message box<br />Make decisions using If…Then statements<br />Make decisions using If…Then…Else statements<br />Make decisions using nested If statements<br />
  3. 3. Chapter 5: Decision Structures<br />3<br />Objectives<br />Make decisions using logical operators<br />Make decisions using Case statements<br />Insert code snippets<br />Test input to ensure a value is numeric<br />
  4. 4. Chapter 5: Decision Structures<br />4<br />Using the GroupBox Object<br />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<br />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<br />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<br />
  5. 5. Chapter 5: Decision Structures<br />5<br />Using the GroupBox Object<br />
  6. 6. Chapter 5: Decision Structures<br />6<br />Adding the RadioButton Objects<br />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<br />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<br />
  7. 7. Chapter 5: Decision Structures<br />7<br />Adding the RadioButton Objects<br />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<br />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<br />
  8. 8. Chapter 5: Decision Structures<br />8<br />Adding the RadioButton Objects<br />
  9. 9. Chapter 5: Decision Structures<br />9<br />Windows Application Container Objects<br />
  10. 10. Chapter 5: Decision Structures<br />10<br />Displaying a Message Box<br />
  11. 11. Chapter 5: Decision Structures<br />11<br />Displaying a Message Box<br />
  12. 12. Chapter 5: Decision Structures<br />12<br />Displaying a Message Box<br />
  13. 13. Chapter 5: Decision Structures<br />13<br />Displaying a Message Box<br />
  14. 14. Displaying a Message Box<br />Chapter 5: Decision Structures<br />14<br />
  15. 15. Chapter 5: Decision Structures<br />15<br />Message Box IntelliSense<br />In the code editing window, inside the event handler you are coding, type msgto display MsgBox in the IntelliSense list<br />Press the Tab key to select MsgBox from the IntelliSense list. Type the following text: (“You have been disconnected from the Internet”, m)<br />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 <br />Click the Start Debugging button on the Standard toolbar<br />
  16. 16. Chapter 5: Decision Structures<br />16<br />Displaying a Message Box<br />
  17. 17. Chapter 5: Decision Structures<br />17<br />Making Decisions with Conditional Statements: Using an If…Then Statement<br />A decision structure is one of the three fundamental control structures used in computer programming<br />When a condition is tested in a Visual Basic program, the condition either is true or false<br />
  18. 18. Chapter 5: Decision Structures<br />18<br />Relational Operators<br />
  19. 19. Chapter 5: Decision Structures<br />19<br />Relational Operators<br />With the insertion point located in the correct location in the code, type ifand then press the SPACEBAR<br />Type inta to select the variable named intAge in the IntelliSense list. Then, type >=18as the condition to be tested. Press the ENTER key<br />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<br />
  20. 20. Chapter 5: Decision Structures<br />20<br />Comparing Strings<br />A string value comparison compares each character in two strings, starting with the first character in each string<br />
  21. 21. Chapter 5: Decision Structures<br />21<br />Comparing Different Data Types<br />Every type of data available in Visual Basic can be compared<br />Different numeric types can be compared to each other<br />A single string character can be compared to a Char data type<br />
  22. 22. Using the If…Then…Else Statement<br />Chapter 5: Decision Structures<br />22<br />
  23. 23. Chapter 5: Decision Structures<br />23<br />Using the If…Then…ElseIf Statement<br />
  24. 24. Nested If Statements<br />Chapter 5: Decision Structures<br />24<br />
  25. 25. Nested If Statements<br />Chapter 5: Decision Structures<br />25<br />
  26. 26. Chapter 5: Decision Structures<br />26<br />Matching If, Else, and End If Entries<br />If statements must be fully contained within the outer If statement<br />Place the correct statements with the correct If and Else statements within the nested If statement<br />This illustration shows incorrect logic<br />
  27. 27. Testing the Status of a RadioButton Object in Code<br />Chapter 5: Decision Structures<br />27<br />
  28. 28. Chapter 5: Decision Structures<br />28<br />Block-Level Scope<br />Scope is defined by where the variable is declared within a program<br />Within an event handler, an If…Then…Else statement is considered a block of code<br />Variables can be declared within a block of code<br />The variable can be referenced only within the block of code where it is declared<br />
  29. 29. Chapter 5: Decision Structures<br />29<br />Using Logical Operators<br />When more than one condition is included in an If...Then...Else statement, the conditions are called a compound condition<br />
  30. 30. Using the And Logical Operator<br />Chapter 5: Decision Structures<br />30<br />
  31. 31. Using the Or Logical Operator<br />Chapter 5: Decision Structures<br />31<br />
  32. 32. Using the Not Logical Operator<br />Chapter 5: Decision Structures<br />32<br />
  33. 33. Chapter 5: Decision Structures<br />33<br />Other Logical Operators<br />
  34. 34. Chapter 5: Decision Structures<br />34<br />Order of Operations for Logical Operators<br />
  35. 35. Chapter 5: Decision Structures<br />35<br />Select Case Statement<br />In some programming applications, different operations can occur based upon the value in a single field<br />
  36. 36. Select Case Statement<br />Chapter 5: Decision Structures<br />36<br />
  37. 37. Chapter 5: Decision Structures<br />37<br />Select Case Test Expressions<br />
  38. 38. Chapter 5: Decision Structures<br />38<br />Using Relational Operators in a Select Case Statement<br />
  39. 39. Using Ranges in Select Case Statements<br />Chapter 5: Decision Structures<br />39<br />
  40. 40. Chapter 5: Decision Structures<br />40<br />Selecting Which Decision Structure to Use<br />You might be faced with determining if you should use the Select Case statement or the If...Then...ElseIf statement to solve a problem<br />Generally, the Select Case statement is most useful when more than two or three values must be tested for a given variable<br />The If...Then...ElseIf statement is more flexible<br />More than one variable can be used in the comparison<br />Compound conditions with the And, Or, and Not logical operators can be used<br />
  41. 41. Chapter 5: Decision Structures<br />41<br />Code Snippets<br />Right-click the line in the code editing window where you want to insert the snippet<br />Click Insert Snippet on the shortcut menu<br />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 <br />Double-click the Conditionals and Loops folder because an If...Then...Else statement is a conditional statement<br />Double-click the If...Else...End If Statement code snippet<br />
  42. 42. Chapter 5: Decision Structures<br />42<br />Code Snippets<br />
  43. 43. Chapter 5: Decision Structures<br />43<br />Validating Data<br />Developers should anticipate that users will enter invalid data<br />Developers must write code that will prevent the invalid data from being used in the program to produce invalid output<br />
  44. 44. Chapter 5: Decision Structures<br />44<br />Testing Input to Determine If the Value Is Numeric<br />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<br />
  45. 45. Chapter 5: Decision Structures<br />45<br />Checking for a Positive Number<br />
  46. 46. Program Design<br />Chapter 5: Decision Structures<br />46<br />
  47. 47. Program Design<br />Chapter 5: Decision Structures<br />47<br />
  48. 48. Program Design<br />Chapter 5: Decision Structures<br />48<br />
  49. 49. Chapter 5: Decision Structures<br />49<br />Chapter Summary<br />Use the GroupBox object<br />Place RadioButton objects in applications<br />Display a message box<br />Make decisions using If…Then statements<br />Make decisions using If…Then…Else statements<br />Make decisions using nested If statements<br />
  50. 50. Chapter 5: Decision Structures<br />50<br />Chapter Summary<br />Make decisions using logical operators<br />Make decisions using Case statements<br />Insert code snippets<br />Test input to ensure a value is numeric<br />
  51. 51. CHAPTER FIVE COMPLETE<br />Decision Structures<br />

×