Chapter 5: More on the Selection Structure

60
Programming with Microsoft Visual Basic 2012 Chapter 5: More on the Selection Structure

description

Chapter 5: More on the Selection Structure. Previewing the Modified Covington Resort Application. Figure 5-2 Recalculated amounts shown in the interface. Figure 5-1 Interface showing the calculated amounts. Lesson A Objectives. After studying Lesson A, you should be able to: - PowerPoint PPT Presentation

Transcript of Chapter 5: More on the Selection Structure

Page 1: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012

Chapter 5: More on the Selection Structure

Page 2: Chapter 5: More on the Selection Structure

Previewing the Modified Covington Resort Application

Programming with Microsoft Visual Basic 2012 2

Figure 5-1 Interface showing the calculated amounts Figure 5-2 Recalculated amounts shown in the interface

Page 3: Chapter 5: More on the Selection Structure

Lesson A Objectives

After studying Lesson A, you should be able to:• 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• Include a multiple-alternative selection structure in

pseudocode and in a flowchart• Code a multiple-alternative selection structure

Programming with Microsoft Visual Basic 2012 3

Page 4: Chapter 5: More on the Selection Structure

• Nested selection structure– When either a selection structure’s true path or its false

path contains another selection structure– The inner selection is called a nested selection

Programming with Microsoft Visual Basic 2012 4

Nested Selection Structures

Figure 5-4 A problem that requires a nested selection structure

Page 5: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 5

Nested Selection Structures (cont.)

Figure 5-5 A problem that requires two nested selection structures

Page 6: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 6

Flowcharting a Nested Selection Structure

Figure 5-6 Problem specification and a correct solution for the voter eligibility problem

Page 7: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 7

Flowcharting a Nested Selection Structure (cont.)

Figure 5-7 Another correct solution for the voter eligibility problem

Page 8: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 8

Coding a Nested Selection Structure

Figure 5-8 Code for the flowcharts in Figures 5-6 and 5-7

Page 9: Chapter 5: More on the Selection Structure

• Four common errors:1. Using a compound condition rather than a nested

selection structure2. Reversing the decisions in the outer and nested

selection structures3. Using an unnecessary nested selection structure4. Including an unnecessary comparison in a condition

Programming with Microsoft Visual Basic 2012 9

Logic Errors in Selection Structures

Page 10: Chapter 5: More on the Selection Structure

• Algorithm– The step-by-step instructions for accomplishing a task

• Desk-checking– The process of reviewing the algorithm while seated at

your desk rather than in front of a computer– Also called hand-tracing

• You use a pencil and paper to follow the algorithm’s instructions by hand

• Desk-check to make sure you do not miss any instructions, and that existing instructions are correct and in the proper order

Programming with Microsoft Visual Basic 2012 10

Logic Errors in Selection Structures (cont.)

Page 11: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 11

Logic Errors in Selection Structures (cont.)

Figure 5-11 Sample data and expected results for the algorithm shown in Figure 5-10

Figure 5-10 A correct algorithm for the golf fee procedure

Page 12: Chapter 5: More on the Selection Structure

Logic Errors in Selection Structures (cont.)

First Logic Error: Using a Compound Condition Rather Than a Nested Selection Structure

Programming with Microsoft Visual Basic 2012 12

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

Page 13: Chapter 5: More on the Selection Structure

Logic Errors in Selection Structures (cont.)

First Logic Error: Using a Compound Condition Rather Than a Nested Selection Structure (cont.)

Programming with Microsoft Visual Basic 2012 13

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

Figure 5-13 Results of desk-checking the incorrect algorithm from Figure 5-12

Page 14: Chapter 5: More on the Selection Structure

Logic Errors in Selection Structures (cont.)

Second Logic Error: Reversing the Outer and Nested Decisions

Programming with Microsoft Visual Basic 2012 14

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

Figure 5-15 Results of desk-checking the incorrect algorithm from Figure 5-14

Page 15: Chapter 5: More on the Selection Structure

Logic Errors in Selection Structures (cont.)

Third Logic Error: Using an Unnecessary Nested Selection Structure

Programming with Microsoft Visual Basic 2012 15

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

Figure 5-17 Results of desk-checking the inefficient algorithm from Figure 5-16

Page 16: Chapter 5: More on the Selection Structure

Logic Errors in Selection Structures (cont.)

Fourth Logic Error: Including an Unnecessary Comparison in a Condition

Programming with Microsoft Visual Basic 2012 16

Figure 5-18 Problem specification, a correct algorithm, and an inefficient algorithm

Figure 5-19 Results of desk-checking the algorithms from Figure 5-18

Page 17: Chapter 5: More on the Selection Structure

Multiple-Alternative Selection Structures

Programming with Microsoft Visual Basic 2012 17

• When you need to choose from several different options, use a multiple-alternative selection structure– Also called an extended selection structure

Figure 5-20 Problem specification for the Allen High School problem

Page 18: Chapter 5: More on the Selection Structure

Multiple-Alternative Selection Structures (cont.)

Programming with Microsoft Visual Basic 2012 18

Figure 5-21 Pseudocode and flowchart containing a multiple-alternative selection structure

Page 19: Chapter 5: More on the Selection Structure

Multiple-Alternative Selection Structures (cont.)

Programming with Microsoft Visual Basic 2012 19

Figure 5-22 Two versions of the code containing a multiple-alternative selection structure

Figure 5-23 Excellent message shown in the interface

Page 20: Chapter 5: More on the Selection Structure

The Select Case Statement

Programming with Microsoft Visual Basic 2012 20

• Select Case statement– Used when a multiple-alternative selection structure has

many paths from which to choose– It’s simpler and clearer to code the selection structure

rather than several If…Then…Else statements

Page 21: Chapter 5: More on the Selection Structure

The Select Case Statement (cont.)

Programming with Microsoft Visual Basic 2012 21

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

Page 22: Chapter 5: More on the Selection Structure

The Select Case Statement (cont.)

Programming with Microsoft Visual Basic 2012 22

Specifying a Range of Values in a Case Clause

Figure 5-25 Syntax and an example of specifying a range of values

Page 23: Chapter 5: More on the Selection Structure

Lesson A Summary

• To create a selection structure that evaluates both a primary and a secondary decision:– Place (nest) the secondary decision’s selection structure

within either the true or false path of the primary decision’s selection structure

• To verify that an algorithm works correctly:– Desk-check (hand-trace) the algorithm

Programming with Microsoft Visual Basic 2012 23

Page 24: Chapter 5: More on the Selection Structure

Lesson A Summary (cont.)

• To code a multiple-alternative selection structure:– Use either If…Then…Else statements or the Select Case

statement• To specify a range of values in a Select Case statement’s

Case clause:– Use the To keyword when you know both the upper and

lower values in the range– Use the Is keyword when you know only one end of the

range• The Is keyword is used in combination with one of the

following comparison operators: =, <, <=, >, >=, <>

Programming with Microsoft Visual Basic 2012 24

Page 25: Chapter 5: More on the Selection Structure

Lesson B Objectives

After studying Lesson B, you should be able to:• Include a group of radio buttons in an interface• Designate a default radio button• Include a check box in an interface• Compare Boolean values

Programming with Microsoft Visual Basic 2012 25

Page 26: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 26

Modifying the Covington Resort Application

Figure 5-36 Revised TOE chart for the Covington Resort application (continues)

Page 27: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 27

Modifying the Covington Resort Application (cont.)

Figure 5-36 Revised TOE chart for the Covington Resort application

(continued)

Page 28: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 28

Modifying the Covington Resort Application (cont.)

Figure 5-37 Partially completed interface for the Covington Resort application

Page 29: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 29

Modifying the Covington Resort Application (cont.)

Adding a Radio Button to the Interface• Radio button

– Limits the user to only one choice from a group of two or more related but mutually exclusive choices

– The three-character ID for a radio button’s name is rad

Figure 5-38 Atrium radio button added to the View group box

Page 30: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 30

Modifying the Covington Resort Application (cont.)

Adding a Radio Button to the Interface (cont.)• The automatically selected radio button is called the

default radio button– Represents the user’s most likely choice – The first button in the group

• Checked property– Set to True to designate the default radio button

Page 31: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 31

Modifying the Covington Resort Application (cont.)

Adding a Check Box to the Interface• Unlike radio buttons, check boxes provide one or more

independent and nonexclusive items from which the user can choose

• Any number of check boxes on a form can be selected at the same time

• The three-character ID for a check box’s name is chk

Figure 5-39 Vehicle parking fee check box added to the interface

Page 32: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 32

Modifying the Covington Resort Application (cont.)

Figure 5-40 Correct TabIndex values

Page 33: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 33

Modifying the Calculate Button’s Code

Figure 5-41 Modified pseudocode for the btnCalc_Click procedure

Page 34: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 34

Modifying the Calculate Button’s Code (cont.)

Figure 5-42 Modified list of named constants and variables

Page 35: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 35

Modifying the Calculate Button’s Code (cont.)

Figure 5-44 Variables added to the procedure

Figure 5-43 Named constants added to the procedure

Page 36: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 36

Modifying the Calculate Button’s Code (cont.)

Figure 5-45 Examples of comparing Boolean values (continues)

Comparing Boolean Values

Page 37: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 37

Modifying the Calculate Button’s Code (cont.)

Figure 5-45 Examples of comparing Boolean values

(continued)

Page 38: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 38

Modifying the ClearLabels Procedure

Figure 5-50 ClearLabels procedure

• CheckedChanged event– Occurs when the value in a control’s Checked property

changes

Page 39: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 39

Figure 5-51 Covington Resort

application’s code at the end of Lesson B

(continues)

Modifying the ClearLabels Procedure (cont.)

Page 40: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 40

Modifying the ClearLabels Procedure (cont.)

(continued)

Figure 5-51 Covington Resort

application’s code at the end of Lesson B

(continues)

Page 41: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 41

(continued)

Modifying the ClearLabels Procedure (cont.)

Figure 5-51 Covington Resort

application’s code at the end of Lesson B

(continues)

Page 42: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 42

(continued)

Modifying the ClearLabels Procedure (cont.)

Figure 5-51 Covington Resort

application’s code at the end of Lesson B

(continues)

Page 43: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 43

(continued)

Modifying the ClearLabels Procedure (cont.)

Figure 5-51 Covington Resort

application’s code at the end of Lesson B

(continues)

Page 44: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 44

(continued)

Modifying the ClearLabels Procedure (cont.)

Figure 5-51 Covington Resort

application’s code at the end of Lesson B

Page 45: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 45

Lesson B Summary

• To limit the user to only one choice in a group of two or more related but mutually exclusive choices:– Use the RadioButton tool to add two or more radio buttons to

the form – To include two groups of radio buttons on a form, at least one

of the groups must be placed within a container, such as a group box

• To allow the user to select any number of choices from a group of one or more independent and nonexclusive choices:– Use the CheckBox tool to add one or more check box controls

to the form

Page 46: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 46

Lesson B Summary (cont.)

• To determine whether a radio button or check box is selected or unselected:– Use the Checked property of the radio button or check

box– The property will contain the Boolean value True if the

control is selected; otherwise, it will contain the Boolean value False

• To process code when the value in the Checked property of a radio button or check box changes:– Enter the code in the radio button’s or check box’s

CheckedChanged event procedure

Page 47: Chapter 5: More on the Selection Structure

Lesson C Objectives

After studying Lesson C, you should be able to:• Determine the success of the TryParse method• Generate random numbers• Show and hide a control while an application is running

Programming with Microsoft Visual Basic 2012 47

Page 48: Chapter 5: More on the Selection Structure

Using the TryParse Method for Data Validation

• TryParse method – Converts a string to a number of a specific data type– With successful conversion:

• The TryParse method stores the number in the variable specified in the method’s NumericVariableName argument

– With unsuccessful conversion:• TryParse stores the number 0 in the variable

Programming with Microsoft Visual Basic 2012 48

Page 49: Chapter 5: More on the Selection Structure

Using the TryParse Method for Data Validation (cont.)

Programming with Microsoft Visual Basic 2012 49

Figure 5-58 Syntax and an example of using the Boolean value returned by the TryParse method

Page 50: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 50

Figure 5-59 Sample run of the original Click event procedure

Using the TryParse Method for Data Validation (cont.)

Page 51: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 51

Figure 5-60 Modified btnCalc_Click procedure

Using the TryParse Method for Data Validation (cont.)

Page 52: Chapter 5: More on the Selection Structure

Generating Random Integers

Programming with Microsoft Visual Basic 2012 52

• Pseudo-random number generator– A device that produces a sequence of numbers that meet

certain statistical requirements for randomness– Create a Random object by declaring it in a Dim

statement– Use the Random.Next method to generate random

integers

Page 53: Chapter 5: More on the Selection Structure

Generating Random Integers (cont.)

Programming with Microsoft Visual Basic 2012 53

Figure 5-61 Syntax and examples of generating random integers

Page 54: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 54

Figure 5-62 Roll ‘Em Game application’s interface

Figure 5-63 Pseudocode for the Roll the Dice button’s Click event procedure

Generating Random Integers (cont.)

Page 55: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 55

Figure 5-64 Result of clicking the Roll the Dice button

Generating Random Integers (cont.)

Page 56: Chapter 5: More on the Selection Structure

Showing and Hiding a Control

Programming with Microsoft Visual Basic 2012 56

• Hide objects by changing their Visible property from True to False

Figure 5-65 Resized form Figure 5-66 Interface with six of the picture boxes hidden

Page 57: Chapter 5: More on the Selection Structure

Showing and Hiding a Control (cont.)

Programming with Microsoft Visual Basic 2012 57

Figure 5-67 Roll the Dice button’s Click event procedure

Page 58: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 58

Lesson C Summary

• To determine whether the TryParse method converted a string to a number of the specified data type:– Use the syntax booleanVariable =

dataType.TryParse(string, numericVariableName)– The TryParse method returns the Boolean value True

when the string can be converted to the numeric dataType

– Otherwise, it returns the Boolean value False

Page 59: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 59

Lesson C Summary (cont.)

• To generate random integers:– Create a Random object to represent the pseudo-random

number generator• The syntax for creating a Random object is

Dim randomObjectName As New Random– Use the Random.Next method to generate a random integer

• The method’s syntax is randomObjectName.Next(minValue, maxValue)

• The Random.Next method returns an integer that is greater than or equal to minValue, but less than maxValue

• The Random.Next method’s return value is assigned to a variable

Page 60: Chapter 5: More on the Selection Structure

Programming with Microsoft Visual Basic 2012 60

Lesson C Summary (cont.)

• To show or hide a control while an application is running:– Set the control’s Visible property to the Boolean value

True to show the control during runtime– Set the control’s Visible property to the Boolean value

False to hide the control during runtime