programming with microsoft visual basic 2008 fourth edition

62
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Five More on the Selection Structure

Upload: azana

Post on 25-Feb-2016

49 views

Category:

Documents


2 download

DESCRIPTION

Programming with Microsoft Visual Basic 2008 Fourth Edition. Chapter Five More on the Selection Structure. Previewing the Math Practice Application. Open Math.exe file The Math Practice application will be used by 1 st and 2 nd grade students . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Programming with Microsoft Visual Basic 2008

Fourth Edition

Chapter FiveMore on the Selection Structure

Page 2: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Previewing the Math Practice Application

• Open Math.exe file • The Math Practice application will be used by 1st

and 2nd grade students

Programming with Microsoft Visual Basic 2008, Fourth Edition 2

Page 3: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Previewing the Math Practice Application (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 3

Figure 5-1: Math Practice application’s user interface

Page 4: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Lesson A Objectives

After studying Lesson A, you should be able to:

• Include a nested selection structure in both pseudocode and a flowchart

• Code a nested selection structure

• Desk-check an algorithm

• Recognize common logic errors in selection structures

Programming with Microsoft Visual Basic 2008, Fourth Edition 4

Page 5: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Lesson A Objectives (continued)

• Include a multiple-path selection structure in both pseudocode and a flowchart

• Code a multiple-path selection structure using the If/ElseIf/Else and Case forms of the selection structure

Programming with Microsoft Visual Basic 2008, Fourth Edition 5

Page 6: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Nested Selection Structures

• Selection structure:– Chooses true or false path based on comparison

or decision• Nested selection structure:

– Selection structure that lies on true or false path of an outer selection structure

– Also called an inner selection structure• Two decisions are involved:

– Primary decision: Made by outer structure– Secondary decision: Made by inner structure

Programming with Microsoft Visual Basic 2008, Fourth Edition 6

Page 7: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Nested Selection Structures (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 7

Figure 5-5: Display Message button’s pseudocode with the nested selection structure in the true path

Page 8: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Programming with Microsoft Visual Basic 2008, Fourth Edition 8

Figure 5-6: Display Message button’s flowchart with the nested selection structure in the true path

Nested Selection Structures (continued)

Page 9: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Nested Selection Structures (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 9

Figure 5-7: Display Message button’s Click event with the nested selection structure in the true path

Page 10: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Nested Selection Structures (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 10

Figure 5-7: Display Message button’s Click event with the nested selection structure in the true path (continued)

Page 11: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Nested Selection Structures (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 11

Figure 5-8: Display Message button’s pseudocode with the nested selection structure in the false path

Page 12: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Programming with Microsoft Visual Basic 2008, Fourth Edition 12

Figure 5-9: Display Message button’s flowchart with the nested selection structure in the true path

Nested Selection Structures (continued)

Page 13: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Programming with Microsoft Visual Basic 2008, Fourth Edition 13

Figure 5-10: Display Message button’s Click event with the nested selection structure in the false path

Nested Selection Structures (continued)

Page 14: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Logic Errors in Selection Structures

• Common logic errors include:– Using compound condition instead of nested

structure – Reversing primary and secondary decisions– Using unnecessary nested selection structure

• Algorithm: Set of step-by-step instructions for accomplishing task

• Desk-checking (or hand-tracing): – Using pencil and paper to follow algorithm’s steps,

using sample data– Goal is to verify that algorithm is correct

Programming with Microsoft Visual Basic 2008, Fourth Edition 14

Page 15: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Logic Errors in Selection Structures (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 15

Figure 5-11: A correct algorithm for the bonus procedure

Page 16: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Logic Errors in Selection Structures (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 16

Figure 5-12: Results of desk-checking the correct algorithm

Page 17: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Using a Compound Condition Rather Than a Nested Selection Structure

• Bonus scenario:– Calculate bonus amount for all employee types– Give extra bonus to X contingent on sales amount – Display the bonus amount

• Problem when using compound condition: It does not indicate hierarchy in decision process

• Nested selection structure does enforce hierarchy of decisions – Extra bonus decision depends on sales type

decision

Programming with Microsoft Visual Basic 2008, Fourth Edition 17

Page 18: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Using a Compound Condition Rather Than a Nested Selection Structure

(continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 18

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

Page 19: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Using a Compound Condition Rather Than a Nested Selection Structure

(continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 19

Figure 5-14: Results of desk-checking the incorrect algorithm shown in Figure 5-13

Page 20: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Reversing the Primary and Secondary Decisions

• Incorrect algorithm reverses primary and secondary decisions– All salespeople with sales >= 10000 get extra

bonus– Amount of bonus depends on sales type

• Extra bonus decision should depend on sales type decision first

• Reminder: Desk-check your algorithm with sample data to verify its correctness

Programming with Microsoft Visual Basic 2008, Fourth Edition 20

Page 21: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Reversing the Primary and Secondary Decisions (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 21

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

Page 22: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Reversing the Primary and Secondary Decisions (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 22

Figure 5-16: Results of desk-checking the incorrect algorithm shown in Figure 5-15

Page 23: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Using an Unnecessary Nested Selection Structure

• Problem with incorrect algorithm:– Adds extra, redundant selection structure on inner

false path• Extra code reduces efficiency and readability• The redundant selection structure makes

decision that was already decided in prior selection structure

• Solution: Eliminate second nested structure

Programming with Microsoft Visual Basic 2008, Fourth Edition 23

Page 24: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Using an Unnecessary Nested Selection Structure (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 24

Figure 5-17: Correct algorithm and an incorrect algorithm containing the third logic error

Page 25: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Using an Unnecessary Nested Selection Structure (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 25

Figure 5-18: Results of desk-checking the inefficient algorithm shown in Figure 5-17

Page 26: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Multiple-Path Selection Structures

• Multiple-path (or extended) selection structures:– Designed to choose from several alternatives

• Condition in multiple-path selection structure does not require true/false answer– Requires expression whose value determines

which path is chosen• Example: Display message based on grades

ranging from A to F

Programming with Microsoft Visual Basic 2008, Fourth Edition 26

Page 27: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Multiple-Path Selection Structures (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 27

Figure 5-19: Letter grades and messages

Page 28: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Programming with Microsoft Visual Basic 2008, Fourth Edition 28

Figure 5-20: Pseudocode and flowchart showing a multiple-path selection structure

Multiple-Path Selection Structures (continued)

Page 29: Programming with  Microsoft Visual Basic 2008  Fourth Edition

The If/ElseIf/Else Form of the Selection Structure

• Allows for selection among more than two paths• Provides more convenient way of writing

multiple-path selection structure than using nested If/Else structures

Programming with Microsoft Visual Basic 2008, Fourth Edition 29

Page 30: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Programming with Microsoft Visual Basic 2008, Fourth Edition 30

Figure 5-21: Two versions of the code for the multiple-path selection structure

The If/ElseIf/Else Form of the Selection Structure (continued)

Page 31: Programming with  Microsoft Visual Basic 2008  Fourth Edition

The Case Selection Structure

• Simpler and clearer when there are many paths • Select Case statement:

– Begins with Select Case, ends with End Select– Each case represents a different instruction path– Optional Case Else clause handles all values not

covered by other Case paths– selectorExpression is evaluated to determine path– Each case, except Case Else, has expressionList

that is compared to selectorExpression

Programming with Microsoft Visual Basic 2008, Fourth Edition 31

Page 32: Programming with  Microsoft Visual Basic 2008  Fourth Edition

The Case Selection Structure (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 32

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

Page 33: Programming with  Microsoft Visual Basic 2008  Fourth Edition

The Case Selection Structure (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 33

Figure 5-23: Syntax and an example of the Select Case statement (continued)

Page 34: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Specifying a Range of Values in an ExpressionList

• To specify range of minimum and maximum values, use:– To keyword: Use if both upper and lower bounds

are known – Is keyword: Use if only upper or lower bound is

known• Example with To: Case 1 To 5• Example with Is: Case Is > 10• Relational operators used with Is: =, <, <=, >, >=,

<>

Programming with Microsoft Visual Basic 2008, Fourth Edition 34

Page 35: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Specifying a Range of Values in an ExpressionList (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 35

Figure 5-24: Example of using the To and Is keywords in a Select Case statement

Page 36: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Lesson A Summary

• Nested selection structures lies on true or false path of outer selection structure

• Desk-check: Validate algorithm by hand with sample data

• Multiple-path selection structures: Use If/ElseIf/Else statement or Select Case statement

• Use To and Is keywords to specify range of values in Case clause of Select Case statement

Programming with Microsoft Visual Basic 2008, Fourth Edition 36

Page 37: Programming with  Microsoft Visual Basic 2008  Fourth Edition

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• Create and call an independent Sub procedure• Generate random numbers

Programming with Microsoft Visual Basic 2008, Fourth Edition 37

Page 38: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Creating the Math Practice Application

• Objective: Practice addition and subtraction• Specifications:

– 1st grade students use numbers 1 through 10– 2nd grade students use numbers 10 through 99– Should not allow problems whose answers will be

negative numbers– Students should be able to check their answers – Extra attempts allowed when answer is incorrect – Application should track and display number of

correct and incorrect responses

Programming with Microsoft Visual Basic 2008, Fourth Edition 38

Page 39: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Adding a Radio Button to the Interface

• Radio Button control:– Limits user to one choice in group of options– Should be labeled so its purpose is understood – Should have unique keyboard access key

• RadioButton tool: Used to add radio button• Default radio button: Button initially selected • Each group of radio buttons must be placed within

container such as group box• Checked property: Set to True to designate default

radio button

Programming with Microsoft Visual Basic 2008, Fourth Edition 39

Page 40: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Adding a Radio Button to the Interface (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 40

Figure 5-27 Subtraction radio button added to the Operation group box

Page 41: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Adding a Check Box Control to the Interface

• Check box control:– Allows multiple selections among group of choices– Any number of check boxes can be selected at

once– Does not limit choices like radio button control– Should be labeled to indicate its purpose

• Checkbox tool: Used to add check box control• Lock controls, then set TabIndex after interface

design is complete

Programming with Microsoft Visual Basic 2008, Fourth Edition 41

Page 42: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Adding a Check Box Control to the Interface (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 42

Figure 5-28: Display summary check box added to the interface

Page 43: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Coding the Math Practice Application

• Procedures to code based on TOE chart: – Click event procedures for seven controls– Load event for form

• One task will be coded with an independent Sub procedure– Generating and displaying two random numbers

Programming with Microsoft Visual Basic 2008, Fourth Edition 43

Page 44: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Creating an Independent Sub Procedure

• Event procedure: Sub procedure associated with specific object and event– Processed when specific event occurs

• Independent Sub procedure: Processed only when called (invoked) from code– Can be invoked from one or more places in an

application– Not associated with specific control– Reduces amount of code, promotes modularity

Programming with Microsoft Visual Basic 2008, Fourth Edition 44

Page 45: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Creating an Independent Sub Procedure (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 45

Figure 5-31: Syntax and steps for creating an independent Sub procedure

Page 46: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Creating an Independent Sub Procedure (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 46

Figure 5-32: Pseudocode for the GenerateAndDisplayIntegers procedure

Page 47: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Generating Random Integers

• Pseudo-random number generator:– Device that produces sequence of pseudo-random

numbers – Pseudo-random: Values statistically resemble

randomness• To use pseudo-random number generator:

– Create Random object within procedure– Generate random integers with Random.Next

method

Programming with Microsoft Visual Basic 2008, Fourth Edition 47

Page 48: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Generating Random Integers (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 48

Figure 5-33: Syntax and examples of generating random integers

Page 49: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Generating Random Integers (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 49

Figure 5-33: Syntax and examples of generating random integers (continued)

Page 50: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Generating Random Integers (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 50

Figure 5-34: Random number generation code entered in the procedure

Page 51: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Generating Random Integers (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 51

Figure 5-35: Additional code entered in the procedure

Page 52: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Coding the Grade Radio Buttons’ Click Event Procedures

• Radio button controls should invoke GenerateAndDisplayNumbers procedure

• Call statement: Invokes independent procedure• Syntax:

Call procedureName([argumentList])– procedureName: Name of procedure to be invoked– argumentList: Optional list of arguments to pass

• Keyword Call is optional; can simply use procedure name

Programming with Microsoft Visual Basic 2008, Fourth Edition 52

Page 53: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Coding the Grade Radio Buttons’ Click Event Procedures (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 53

Figure 5-36: Examples of including the Call statement in the Click event procedures for the Grade radio buttons

Page 54: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Coding the Operation Radio Buttons’ Click Event Procedures

• Requirements for Click event procedures:– Display appropriate mathematical operator (+ or -) – Generate and display two random integers– Show addition problem by default when program

starts

Programming with Microsoft Visual Basic 2008, Fourth Edition 54

Page 55: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Coding the Form’s Load Event Procedure

• Form’s Load event procedure – Processed when application is started – Form is not displayed until its instructions are

processed• Two ways to initially display addition problem

– Call GenerateAndDisplayIntegers()– radAddition.PerformClick()– Enter either statement in Load event procedure

Programming with Microsoft Visual Basic 2008, Fourth Edition 55

Page 56: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Lesson B Summary• Radio button control: Limits user to only one choice

among group of choices• Check box control: Allows user to select multiple

choices from group of choices• Independent Sub procedures: Not linked to controls

and can be invoked anywhere in program • Use Random object to generate pseudo-random

numbers• Call statement: Used to call (invoke) independent

Sub procedure• Use object.PerformClick to invoke object’s Click event

Programming with Microsoft Visual Basic 2008, Fourth Edition 56

Page 57: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Lesson C Objectives

After studying Lesson C, you should be able to: • Code a check box’s Click event procedure• Show and hide a control

Programming with Microsoft Visual Basic 2008, Fourth Edition 57

Page 58: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Coding the Check Answer Button’s Click Event Procedure

• Study procedure’s pseudocode to determine what variables and named constants are needed

– Six Integer variables and one named constant are needed

– Two variables have static storage type

Programming with Microsoft Visual Basic 2008, Fourth Edition 58

Page 59: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Coding the Check Answer Button’s Click Event Procedure (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 59

Figure 5-43: Pseudocode for the btnCheckAnswer control’s Click event procedure

Page 60: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Coding the Check Answer Button’s Click Event Procedure (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition 60

Figure 5-44: Variables used by the btnCheckAnswer control’s Click event procedure

Page 61: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Coding the Display Summary Check Box’s Click Event Procedure

• chkSummary control’s Click event procedure displays and hides grpSummary control

• Event triggering display of group box control• When user selects check box, event is triggered,

showing group box and its contents• When user deselects check box, event is triggered,

hiding group box control• Checked property: Indicates if check box was

selected• Visible property: Indicates if control is shown

Programming with Microsoft Visual Basic 2008, Fourth Edition 61

Page 62: Programming with  Microsoft Visual Basic 2008  Fourth Edition

Lesson C Summary

• To show or hide control, set control’s Visible property

• Use selection structure to determine if check box was either selected or deselected by user

Programming with Microsoft Visual Basic 2008, Fourth Edition 62