1 / 46

# Chapter 5: More on the Selection Structure - PowerPoint PPT Presentation

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

Related searches for Chapter 5: More on the Selection Structure

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Chapter 5: More on the Selection Structure' - arlen

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic .NET, Second Edition

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

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

• 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

Programming with Microsoft Visual Basic .NET, Second Edition

Part of Figure 5-3: Pseudocode showing the nested selection structure in the true path

Programming with Microsoft Visual Basic .NET, Second Edition

Part of Figure 5-5: Pseudocode showing the nested selection structure in the false path

Programming with Microsoft Visual Basic .NET, Second Edition

• 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

• 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

Figure 5-7: A correct algorithm for the vacation procedure

Programming with Microsoft Visual Basic .NET, Second Edition

Figure 5-8: Results of desk-checking the correct algorithm shown in Figure 5-7

Programming with Microsoft Visual Basic .NET, Second Edition

Figure 5-9: Correct algorithm and an incorrect algorithm containing the first logic error

Programming with Microsoft Visual Basic .NET, Second Edition

Figure 5-11: Correct algorithm and an incorrect algorithm containing the second logic error

Programming with Microsoft Visual Basic .NET, Second Edition

Figure 5-13: Correct algorithm and an inefficient algorithm containing the third logic error

Programming with Microsoft Visual Basic .NET, Second Edition

• 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

Figure 5-16: Two versions of the Visual Basic .NET code for the grade procedure

Programming with Microsoft Visual Basic .NET, Second Edition

The Case Form of the Selection Structure (continued)

Figure 5-18: Syntax and an example of the Select Case statement

Programming with Microsoft Visual Basic .NET, Second Edition

Figure 5-19: Results of desk-checking the grade procedure shown in Figure 5-18

Programming with Microsoft Visual Basic .NET, Second Edition

Using To and Is in an (continued)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

• 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

• 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

The Is Comparison Operator (continued)

• 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

The TypeOf…Is Comparison Operator (continued)

• 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

The Like Comparison Operator (continued)

• 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

The Like Comparison Operator (continued) (continued)

Figure 5-24: Pattern-matching characters

Programming with Microsoft Visual Basic .NET, Second Edition

The Math Practice Application (continued)Lesson 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

The Math Practice Application (continued)Lesson 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

Completing the User Interface (continued)

• 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

Completing the User Interface (continued) (continued)

Figure 5-28: Sketch of the Math Practice application’s user interface

Programming with Microsoft Visual Basic .NET, Second Edition

• 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

• 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

Adding a Check Box Control to the Form (continued)

• 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

Coding the Math Practice Application (continued)

• 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

Creating a User-Defined Sub Procedure (continued)

• 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

• 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

Generating Random Numbers (continued)

• 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

• 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

• 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

• 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()

Programming with Microsoft Visual Basic .NET, Second Edition

Completing the Math Practice Application uiSubtractionRadioButton Click Event ProceduresLesson 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

• 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

Figure 5-49: Pseudocode for the uiCheckAnswerButton control’s Click event procedure

Programming with Microsoft Visual Basic .NET, Second Edition

• 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

Summary (continued)

• 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

Summary (continued) (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

Summary (continued) (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