Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.

Similar presentations


Presentation on theme: "Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition."— Presentation transcript:

1 Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition

2 2 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 Desk-check an algorithm Recognize common logic errors in selection structures

3 3 Programming with Microsoft Visual Basic 2005, Third Edition Nested, If/ElseIf/Else, and Case Selection Structures Lesson 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

4 4 Programming with Microsoft Visual Basic 2005, Third Edition Previewing the Completed Application Go to Run command on Windows Start menu Browse to the VB2005\Chap05 folder Open the Math.exe file The Math Practice user interface appears

5 5 Programming with Microsoft Visual Basic 2005, Third Edition Previewing the Completed Application (continued) Figure 5-1: Math Practice application’s user interface

6 6 Programming with Microsoft Visual Basic 2005, Third Edition Nested Selection Structures Selection structure review –Chooses true or false path based on a condition Nested selection structure –Lies on true or false path of outer selection structure –Also called an inner selection structure Primary decision: made by outer structure Secondary decision: made by inner structure

7 7 Programming with Microsoft Visual Basic 2005, Third Edition Nested Selection Structures (continued) Figure 5-3: Pseudocode showing the nested selection structure in the true path

8 8 Programming with Microsoft Visual Basic 2005, Third Edition Nested Selection Structures (continued) Figure 5-4: Flowchart showing the nested selection structure in the true path

9 9 Programming with Microsoft Visual Basic 2005, Third Edition Logic Errors in Selection Structures Common logic errors with selection structures –Using a logical operator instead of nested structure –Reversing the primary and secondary decisions –Using an unnecessary nested selection structure Algorithm: set of instructions that perform a task Desk-checking (hand-tracing) –Using sample data to manually run through algorithm

10 10 Programming with Microsoft Visual Basic 2005, Third Edition Logic Errors in Selection Structures (continued) Figure 5-10: A correct algorithm for the bonus procedure

11 11 Programming with Microsoft Visual Basic 2005, Third Edition Logic Errors in Selection Structures (continued) Figure 5-11: Results of desk-checking the correct algorithm shown in Figure 5-10

12 12 Programming with Microsoft Visual Basic 2005, Third Edition Using a Logical Operator 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 with incorrect algorithm –Extra bonus depends on a compound condition –Types other than X receive unintended bonus Solution: redesign algorithm with nested structures –Extra bonus decision depends on sales type decision

13 13 Programming with Microsoft Visual Basic 2005, Third Edition Using a Logical Operator Rather Than a Nested Selection Structure (continued) Figure 5-11: Correct algorithm and an incorrect algorithm containing the first logic error

14 14 Programming with Microsoft Visual Basic 2005, Third Edition Reversing the Primary and Secondary Decisions Refer to prior bonus scenario Problem with incorrect algorithm –Reverses primary and secondary decisions –All salespeople with sales >= 10000 get extra bonus Solution: reverse primary and secondary decisions –Extra bonus decision depends on sales type decision Reminder: desk-check algorithm with sample data

15 15 Programming with Microsoft Visual Basic 2005, Third Edition Reversing the Primary and Secondary Decisions (continued) Figure 5-14: Correct algorithm and an incorrect algorithm containing the second logic error

16 16 Programming with Microsoft Visual Basic 2005, Third Edition Using an Unnecessary Nested Selection Structure Refer to prior bonus scenario Problem with incorrect algorithm –Adds redundant selection structure on inner false path –Extra code adds processing time, reduces readability Solution: eliminate second nested structure

17 17 Programming with Microsoft Visual Basic 2005, Third Edition Using an Unnecessary Nested Selection Structure (continued) Figure 5-16: Correct algorithm and an inefficient algorithm containing the third logic error

18 18 Programming with Microsoft Visual Basic 2005, Third Edition The If/ElseIf/Else Selection Structure Extended (multiple path) selection structures –Designed to handle complex nesting patterns –Examples: If/ElseIf/Else and Case If/ElseIf/Else –Allows for selection among more than two paths –Provides a more readable version of selection logic Example: display message based on grades A – F

19 19 Programming with Microsoft Visual Basic 2005, Third Edition The If/ElseIf/Else Selection Structure (continued) Figure 5-18: Two versions of xDisplayMsgButton’s Click event procedure (continued)

20 20 Programming with Microsoft Visual Basic 2005, Third Edition The If/ElseIf/Else Selection Structure (continued) Figure 5-18: Two versions of xDisplayMsgButton’s Click event procedure

21 21 Programming with Microsoft Visual Basic 2005, Third Edition The Case Selection Structure Alternative to If/ElseIf/Else multiple path selection Select Case statement –Begins with Select Case, ends with End Select clause –Each case represents a different instruction path –Optional Case Else clause is the default case –selectorExpression is evaluated to determine path –Each case, except Case Else, has an expressionList How the computer determines the instruction path –selectorExpression value matches expressionList value

22 22 Programming with Microsoft Visual Basic 2005, Third Edition The Case Selection Structure (continued) Figure 5-21: Flowchart showing the Case selection structure

23 23 Programming with Microsoft Visual Basic 2005, Third Edition The Case Selection Structure (continued) Figure 5-22: Syntax and an example of the Select Case statement (continued)

24 24 Programming with Microsoft Visual Basic 2005, Third Edition The Case Selection Structure (continued) Figure 5-22: Syntax and an example of the Select Case statement

25 25 Programming with Microsoft Visual Basic 2005, Third Edition Desk-Checking the xDisplayMsgButton’s Click Event Procedure Desk check with the letters C, F, and X Chief actions –Compare selectorExpression to expressionList values –If a match is found, process Case instruction –If no match is found, process Case Else instruction Results –First two letters, C and F, match regular cases –Last letter, X, matches default Case Else

26 26 Programming with Microsoft Visual Basic 2005, Third Edition Desk-Checking the xDisplayMsgButton’s Click Event Procedure (continued) Figure 5-23: Results of desk-checking the xDisplayMsgButton’s Click event procedure shown in Figure 5-22

27 27 Programming with Microsoft Visual Basic 2005, Third Edition Specifying a Range of Values in an ExpressionList Specifying range of minimum and maximum values –To keyword: use if 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: =,, >=, <>

28 28 Programming with Microsoft Visual Basic 2005, Third Edition The Is, TypeOf…Is, and Like Comparison Operators Is operator –Determines if two object references are the same TypeOf...Is –Determines whether an object is a specified type Like –Determines whether two strings are equal

29 29 Programming with Microsoft Visual Basic 2005, Third Edition The Is Comparison Operator Is operator –Determines if two object references are the same Object reference –Memory address within computer’s internal memory Syntax: objectReference1 Is objectReference2 –Evaluates to True if object references are the same –Otherwise evaluates to False

30 30 Programming with Microsoft Visual Basic 2005, Third Edition The TypeOf…Is Comparison Operator TypeOf…Is operator –Determines whether an object is a specified type Syntax: TypeOf object Is objectType –Evaluates to True if type of object equals objectType –Otherwise evaluates to False

31 31 Programming with Microsoft Visual Basic 2005, Third Edition The TypeOf…Is Comparison Operator (continued) Figure 5-29: Syntax and an example of the TypeOf...Is operator

32 32 Programming with Microsoft Visual Basic 2005, Third Edition The Like Comparison Operator Like operator –Determines whether one string is equal to another –Uses pattern matching algorithms Syntax: string Like pattern –string and pattern must be String expressions –pattern stores multiple pattern-matching characters –Evaluates to True if string matches pattern –Otherwise evaluates to False

33 33 Programming with Microsoft Visual Basic 2005, Third Edition The Like Comparison Operator (continued) Figure 5-31: Syntax and examples of the Like operator (continued)

34 34 Programming with Microsoft Visual Basic 2005, Third Edition The Like Comparison Operator (continued) Figure 5-31: Syntax and examples of the Like operator

35 35 Programming with Microsoft Visual Basic 2005, Third Edition Summary – Lesson A A nested selection structures lies on a true or false path of outer selection structure Desk-check: validate an algorithm with sample data Multiple-path selection structures: If/ElseIf/Else and Case To and Is: keywords that specify a range of values in a Case clause of a Select Case statement Special comparison operators: Is, TypeOf...Is, Like

36 36 Programming with Microsoft Visual Basic 2005, Third Edition The Math Practice Application 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 and call an independent Sub procedure

37 37 Programming with Microsoft Visual Basic 2005, Third Edition The Math Practice Application Lesson B Objectives (continued) Generate random numbers Invoke a radio button’s Click event procedure from code

38 38 Programming with Microsoft Visual Basic 2005, Third Edition Completing the User Interface Objective: practice addition and subtraction Specifications –First grade students use numbers 1 through 10 –Second grade students use numbers 10 through 100 –Students should be able to check their answers –Extra attempts allowed when answer is incorrect –Application should track and display responses

39 39 Programming with Microsoft Visual Basic 2005, Third Edition Completing the User Interface (continued) Figure 5-36: Sketch of the Math Practice application’s user interface

40 40 Programming with Microsoft Visual Basic 2005, Third Edition Adding a Radio Button to the Form RadioButton control –Limits the user to one choice in a group of options –Should be labeled so its purpose is understood –Should have a unique keyboard access key RadioButton tool: used to add a radio button Default radio button: button initially selected Checked property: designates default radio button

41 41 Programming with Microsoft Visual Basic 2005, Third Edition Adding a Radio Button to the Form (continued) Figure 5-38: Subtraction radio button added to the interface

42 42 Programming with Microsoft Visual Basic 2005, Third Edition Adding a Check Box Control to the Form Check box control –Allows multiple selection among group of choices –Any number of check boxes can be selected at once –Does not limit choices like a radio button control –Should be labeled to indicate its purpose Checkbox tool: used to add a check box control Lock controls, set TabIndex after interface complete

43 43 Programming with Microsoft Visual Basic 2005, Third Edition Adding a Check Box Control to the Form (continued) Figure 5-39: Display summary check box added to the interface

44 44 Programming with Microsoft Visual Basic 2005, Third Edition Coding the Math Practice Application Procedures to code –Click event procedures for seven of the controls –Load event for the MathForm Controls not coded in Lesson B –xExitButton control, xCheckAnswerButton, xSummaryCheckBox Task coded with an independent Sub procedure –Generating and displaying two random numbers

45 45 Programming with Microsoft Visual Basic 2005, Third Edition Creating an Independent Sub Procedure Event procedure –Associated with a specific object and event –Processed when a specific event occurs Independent Sub procedure –Invoked from one or more places in an application –Not associated with a specific control –Reduces amount of code, promotes modularity

46 46 Programming with Microsoft Visual Basic 2005, Third Edition Creating an Independent Sub Procedure (continued) Figure 5-42: GenerateAndDisplayNumbers procedure header and footer

47 47 Programming with Microsoft Visual Basic 2005, Third Edition Generating Random Numbers Pseudo-random number generator –Produces sequence of pseudo-random numbers –Pseudo-random: statistically resembling randomness Using the pseudo-random number generator –Create a Random object within a procedure –Generate random integers with Random.Next method

48 48 Programming with Microsoft Visual Basic 2005, Third Edition Generating Random Numbers (continued) Figure 5-45: Random number generation code entered in the procedure

49 49 Programming with Microsoft Visual Basic 2005, Third Edition Coding the xGrade1RadioButton and xGrade2RadioButton Click Event Procedures Controls will call GenerateAndDisplayNumbers Call statement: invokes independent procedure Call syntax: Call procedurename([argumentlist]) –procedurename: name of procedure to be invoked –argumentlist: optional list of arguments to pass

50 50 Programming with Microsoft Visual Basic 2005, Third Edition Coding the xGrade1RadioButton and xGrade2RadioButton Click Event Procedures (continued) Figure 5-48: Two examples of including the Call statement in the Click event procedures for the grade radio buttons

51 51 Programming with Microsoft Visual Basic 2005, Third Edition Coding the xAdditionRadioButton and xSubtractionRadioButton Click Event Procedures Requirements for Click event procedures –Display appropriate mathematical operator (+ or -) –Generate and display two random numbers

52 52 Programming with Microsoft Visual Basic 2005, Third Edition Coding the xAdditionRadioButton and xSubtractionRadioButton Click Event Procedures (continued) Figure 5-52: Completed ProcessOperationRadioButtons procedure

53 53 Programming with Microsoft Visual Basic 2005, Third Edition Coding the Form’s Load Event Procedure Form’s Load event procedure –Processed when the application is started –Form not displayed until instructions are processed Two ways to initially display addition problem –Call GenerateAndDisplayNumbers() –Me.xAdditionRadioButton.PerformClick() Enter either statement in the Load event procedure

54 54 Programming with Microsoft Visual Basic 2005, Third Edition Summary – Lesson B Radio button control: limits user to only one choice among a group of choices Check box control: allows user to select multiple choices from a group of choices Independent Sub procedures not linked to controls Random object: pseudo-random number generator Call statement: used to call (invoke) an independent Sub procedure

55 55 Programming with Microsoft Visual Basic 2005, Third Edition Completing the Math Practice Application Lesson C Objectives Code a check box’s Click event procedure Display and hide a control

56 56 Programming with Microsoft Visual Basic 2005, Third Edition Coding the xCheckAnswerButton Click Event Procedure First step: open the Math Practice application How to determine variables and named constants –Study procedure’s pseudocode Six Integer variables and one named constant are used –Two of the variables have a static storage type

57 57 Programming with Microsoft Visual Basic 2005, Third Edition Coding the xCheckAnswerButton Click Event Procedure (continued) Figure 5-55: Pseudocode for the xCheckAnswerButton control’s Click event procedure

58 58 Programming with Microsoft Visual Basic 2005, Third Edition Coding the xSummaryCheckBox Click Event Procedure xSummaryCheckBox’s Click event procedure –Displays and hides xSummaryGroupBox control Event triggering display of group box control –User selects the check box Event causing group box control to hide –User deselects the check box Checked property: indicates if check box selected

59 59 Programming with Microsoft Visual Basic 2005, Third Edition Summary – Lesson C To display or hide a control, set the control’s Visible property Use a selection structure to determine if a check box was either selected or deselected by the user


Download ppt "Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition."

Similar presentations


Ads by Google