1 / 46

Chapter 5: More on the Selection Structure

Chapter 5: More on the Selection Structure. Programming with Microsoft Visual Basic .NET, Second Edition. Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives. Include a nested selection structure in pseudocode and in a flowchart Code a nested selection structure

arlen
Download Presentation

Chapter 5: More on the Selection Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic .NET, Second Edition

  2. Nested, If/ElseIf/Else, and Case Selection StructuresLesson A Objectives • Include a nested selection structure in pseudocode and in a flowchart • Code a nested selection structure • Desk-check an algorithm • Recognize common logic errors in selection structures Programming with Microsoft Visual Basic .NET, Second Edition

  3. Nested, If/ElseIf/Else, and Case Selection StructuresLesson A Objectives (continued) • Code an If/ElseIf/Else selection structure • Include a Case selection structure in pseudocode and in a flowchart • Code a Case selection structure • Write code that uses the Is, TypeOf…Is, and Like comparison operators Programming with Microsoft Visual Basic .NET, Second Edition

  4. Nested Selection Structures • When a selection structure’s true path or its false path contains another selection structure, the inner selection structure is referred to as a nested selection structure Programming with Microsoft Visual Basic .NET, Second Edition

  5. Nested Selection Structures (continued) Programming with Microsoft Visual Basic .NET, Second Edition

  6. Nested Selection Structures (continued) Part of Figure 5-3: Pseudocode showing the nested selection structure in the true path Programming with Microsoft Visual Basic .NET, Second Edition

  7. Nested Selection Structures (continued) Part of Figure 5-5: Pseudocode showing the nested selection structure in the false path Programming with Microsoft Visual Basic .NET, Second Edition

  8. Logic Errors in Selection Structures • Common logic errors with selection structures: • Using a logical operator when a nested selection structure is needed • Reversing the primary and secondary decisions • Using an unnecessary nested selection structure Programming with Microsoft Visual Basic .NET, Second Edition

  9. Logic Errors in Selection Structures (continued) • An algorithm is the set of step-by-step instructions that accomplish a task • Desk-checking means that you use sample data to walk through each of the steps in the algorithm manually, just as if you were the computer • Desk-checking is also called hand-tracing Programming with Microsoft Visual Basic .NET, Second Edition

  10. Logic Errors in Selection Structures (continued) Figure 5-7: A correct algorithm for the vacation procedure Programming with Microsoft Visual Basic .NET, Second Edition

  11. Logic Errors in Selection Structures (continued) Figure 5-8: Results of desk-checking the correct algorithm shown in Figure 5-7 Programming with Microsoft Visual Basic .NET, Second Edition

  12. Using a Logical Operator Rather Than a Nested Selection Structure Figure 5-9: Correct algorithm and an incorrect algorithm containing the first logic error Programming with Microsoft Visual Basic .NET, Second Edition

  13. Reversing the Primary and Secondary Decisions Figure 5-11: Correct algorithm and an incorrect algorithm containing the second logic error Programming with Microsoft Visual Basic .NET, Second Edition

  14. Using an Unnecessary Nested Selection Structure Figure 5-13: Correct algorithm and an inefficient algorithm containing the third logic error Programming with Microsoft Visual Basic .NET, Second Edition

  15. The If/ElseIf/Else Form of the Selection Structure • Figure 5-16 shows two versions of the Visual Basic .NET code for the grade procedure Figure 5-15: Letter grades and messages Programming with Microsoft Visual Basic .NET, Second Edition

  16. The If/ElseIf/Else Form of the Selection Structure (continued) Figure 5-16: Two versions of the Visual Basic .NET code for the grade procedure Programming with Microsoft Visual Basic .NET, Second Edition

  17. The Case Form of the Selection Structure Figure 5-18: Syntax and an example of the Select Case statement Programming with Microsoft Visual Basic .NET, Second Edition

  18. Desk-Checking the Grade Procedure Figure 5-19: Results of desk-checking the grade procedure shown in Figure 5-18 Programming with Microsoft Visual Basic .NET, Second Edition

  19. Using To and Is in an ExpressionList • To keyword: specifies a range of minimum and maximum values Case 1 To 5 • Is keyword: specifies a range of values when you know only one value, either the minimum or the maximum Case Is > 10 Programming with Microsoft Visual Basic .NET, Second Edition

  20. The Is, TypeOf…Is, and Like Comparison Operators • The Is operator determines whether two object references refer to the same object • An object reference is a memory address within the computer’s internal memory; it indicates where in memory the object is stored Programming with Microsoft Visual Basic .NET, Second Edition

  21. The Is, TypeOf…Is, and Like Comparison Operators (continued) • The TypeOf…Is operator determines whether an object is a specified type • The Like operator uses pattern matching to determine if one string is equal to another string Programming with Microsoft Visual Basic .NET, Second Edition

  22. The Is Comparison Operator • Use the Is operator to determine whether two object references refer to the same object • An object reference is a memory address within the computer’s internal memory • Syntax: objectReference1 Is objectReference2 • The Is operator evaluates to True if: objectReference1 is the same as objectReference2; otherwise it evaluates to False Programming with Microsoft Visual Basic .NET, Second Edition

  23. The TypeOf…Is Comparison Operator • Use the TypeOf…Is operator to determine whether an object is a specified type • Syntax: TypeOf object Is objectType • The TypeOf…Is operator evaluates to True if the type of object is the same as objectType; otherwise it evaluates to False Programming with Microsoft Visual Basic .NET, Second Edition

  24. The Like Comparison Operator • The Like operator allows you to use pattern matching to determine whether one string is equal to another string • Syntax: string Like pattern • Both string and pattern must be String expressions • pattern can contain one or more of the pattern-matching characters • The Like operator evaluates to True if string matches pattern; otherwise it evaluates to False Programming with Microsoft Visual Basic .NET, Second Edition

  25. The Like Comparison Operator (continued) Figure 5-24: Pattern-matching characters Programming with Microsoft Visual Basic .NET, Second Edition

  26. The Math Practice ApplicationLesson B Objectives • Include a group of radio buttons in an interface • Designate a default radio button • Include a check box in an interface • Create a user-defined Sub procedure • Generate random numbers using the Random object and the Random.Next method Programming with Microsoft Visual Basic .NET, Second Edition

  27. The Math Practice ApplicationLesson B Objectives (continued) • Call a user-defined Sub procedure • Invoke a radio button control’s Click event procedure from code • Process code when a form is first loaded into the computer’s memory Programming with Microsoft Visual Basic .NET, Second Edition

  28. Completing the User Interface • The application should display the addition or subtraction problem on the screen, then allow the student to enter the answer, and then verify that the answer is correct • If the student’s answer is not correct, the application should give the student as many chances as necessary to answer the problem correctly Programming with Microsoft Visual Basic .NET, Second Edition

  29. Completing the User Interface (continued) Figure 5-28: Sketch of the Math Practice application’s user interface Programming with Microsoft Visual Basic .NET, Second Edition

  30. Adding a Radio Button to the Form • RadioButton control: • Limits the user to one choice in a group of options • Should have a unique access key, which allows the user to select the button using the keyboard Programming with Microsoft Visual Basic .NET, Second Edition

  31. Adding a Radio Button to the Form (continued) • The selected button is called the default radio button • The selected button is either the radio button that represents the user’s most likely choice or the first radio button in the group • Designate a radio button as the default radio button by setting the button’s Checked property to the Boolean value True Programming with Microsoft Visual Basic .NET, Second Edition

  32. Adding a Check Box Control to the Form • Checkbox controls: used for adding a check box control to the interface • Use check box controls to allow the user to select any number of choices from a group of one or more independent and nonexclusive choices • Any number of check boxes on a form can be selected at the same time Programming with Microsoft Visual Basic .NET, Second Edition

  33. Coding the Math Practice Application • The Click event procedures for seven of the controls and the Load event for the MathForm need to be coded • In this lesson, you code all but the Click event procedures for the uiExitButton control and the uiCheckAnswerButton and uiSummaryCheckBox controls (which you code in Lesson C) Programming with Microsoft Visual Basic .NET, Second Edition

  34. Creating a User-Defined Sub Procedure • A user-defined Sub procedure is a collection of code that can be invoked from one or more places in an application • Enter Private Sub SubName() just above the End Class statement • The End Sub will automatically be generated Programming with Microsoft Visual Basic .NET, Second Edition

  35. Creating a User-Defined Sub Procedure (continued) • The rules for naming a user-defined Sub procedure are the same as for naming variables • Sub procedure names should be entered using Pascal case • Capitalize the first letter in the name and the first letter of each subsequent word in the name Programming with Microsoft Visual Basic .NET, Second Edition

  36. Generating Random Numbers • Visual Studio .NET provides a pseudo-random number generator • Produces a sequence of numbers that meet certain statistical requirements for randomness • Dim GeneratorRandom As New Random() • Methods • Next(minValue, maxValue) • NextDouble() Programming with Microsoft Visual Basic .NET, Second Edition

  37. Coding the uiGrade1RadioButton and uiGrade2RadioButton Click Event Procedures • Use the Visual Basic .NET Call statement to call (invoke) a user-defined Sub procedure • Syntax: Call procedurename([argumentlist]) • The square brackets in the syntax indicate that the argumentlist is optional Programming with Microsoft Visual Basic .NET, Second Edition

  38. Coding the uiAdditionRadioButton and uiSubtractionRadioButton Click Event Procedures • When the user clicks the uiAdditionRadioButton control or the uiSubtractionRadioButton control, the control’s Click event procedure should: • Display the appropriate mathematical operator (either a plus sign or a minus sign) in the uiOperatorPictureBox control • Generate and display two random numbers in the uiNum1Label and uiNum2Label controls Programming with Microsoft Visual Basic .NET, Second Edition

  39. Coding the Form’s Load Event Procedure • Instructions entered in the form’s Load event procedure are processed when the application is started and the form is loaded into memory • To automatically display an addition problem when the Math Practice interface first appears, enter one of the following statements in the MathForm’s Load event procedure • Call GenerateAndDisplayNumbers() • Me.uiAdditionRadioButton.PerformClick() Programming with Microsoft Visual Basic .NET, Second Edition

  40. Completing the Math Practice ApplicationLesson C Objectives • Select the existing text in a text box control • Code a check box control’s Click event procedure • Display and hide a control Programming with Microsoft Visual Basic .NET, Second Edition

  41. Coding the uiCheckAnswerButton Click Event Procedure • Need to code the Click event procedures for the uiCheckAnswerButton and the uiDisplaySummaryCheckBox controls • Figure 5-49 shows the pseudocode for the uiCheckAnswerButton control’s Click event procedure Programming with Microsoft Visual Basic .NET, Second Edition

  42. Coding the uiCheckAnswerButton Click Event Procedure (continued) Figure 5-49: Pseudocode for the uiCheckAnswerButton control’s Click event procedure Programming with Microsoft Visual Basic .NET, Second Edition

  43. Coding the uiSummaryCheckBox Click Event Procedure • The uiSummaryCheckBox control’s Click event procedure is responsible for both displaying and hiding the uiSummaryGroupBox control • The procedure should: • Display the group box control when the user selects the check box • Hide the group box control when the user deselects the check box Programming with Microsoft Visual Basic .NET, Second Edition

  44. Summary • To create a selection structure that evaluates both a primary and a secondary decision, place (nest) the selection structure for the secondary decision within either the true path or false path of the selection structure for the primary decision • To code a multiple-path (or extended) selection structure, use either the If…Then…Else statement or the Select Case statement Programming with Microsoft Visual Basic .NET, Second Edition

  45. Summary (continued) • To limit the user to only one choice in a group of two or more related and mutually exclusive choices, add a radio button control • To allow the user to select any number of choices from a group of one or more independent and nonexclusive choices, add a check box control • To call (invoke) a user-defined Sub procedure, use the Call statement Programming with Microsoft Visual Basic .NET, Second Edition

  46. Summary (continued) • To process code when the form is loaded into memory, enter the code in the form’s Load event procedure • To display or hide a control, set the control’s Visible property • To code a check box control’s Click event procedure, use a selection structure to determine whether the check box was either selected or deselected by the user Programming with Microsoft Visual Basic .NET, Second Edition

More Related