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

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
1.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Chapter 11: Classes and Objects
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
1.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
Chapter 7: Sub and Function Procedures
Chapter 6: More on the Selection Structure
C++ for Engineers and Scientists Third Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Chapter Four The Selection Structure
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 12: How Long Can This Go On?
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Selection Structure If... Then.. Else Case. Selection Structure Use to make a decision or comparison and then, based on the result of that decision or.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Working with option button, check box, and list box controls Visual Basic for Applications 13.
Chapter 6: The Repetition Structure
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
1.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Controlling Program Flow with Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Visual Basic.NET BASICS Lesson 9 Nested If Statements and Radio Buttons.
Course ILT Using complex selection structures Unit objectives Include radio buttons and check boxes in an interface, create and call a user- defined Sub.
An Introduction to Programming with C++1 More on the Selection Structure Tutorial 7.
More on the Selection Structure
The Selection Structure
Chapter 4: The Selection Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
Making Decisions in a Program
Objectives After studying this chapter, you should be able to:
CIS 16 Application Development Programming with Visual Basic
Microsoft Visual Basic 2005: Reloaded Second Edition
Presentation transcript:

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

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

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 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 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 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 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 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 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 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 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 >= 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 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 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 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 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 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 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 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 Programming with Microsoft Visual Basic 2005, Third Edition The Case Selection Structure (continued) Figure 5-21: Flowchart showing the Case selection structure

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 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 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 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 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 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 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 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 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 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 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 Programming with Microsoft Visual Basic 2005, Third Edition The Like Comparison Operator (continued) Figure 5-31: Syntax and examples of the Like operator

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 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 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 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 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 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 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 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 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 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 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 Programming with Microsoft Visual Basic 2005, Third Edition Creating an Independent Sub Procedure (continued) Figure 5-42: GenerateAndDisplayNumbers procedure header and footer

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 Programming with Microsoft Visual Basic 2005, Third Edition Generating Random Numbers (continued) Figure 5-45: Random number generation code entered in the procedure

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

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 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 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 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 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 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 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