Programming with Microsoft Visual Basic th Edition

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
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.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 9 Decisions, Decisions, Decisions.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
ITEC113 Algorithms and Programming Techniques
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
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 6: More on the Selection Structure
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter 8: String Manipulation
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.
Using the selection structure (Unit 7) Visual Basic for Applications.
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
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Chapter 6: The Repetition Structure
Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Chapter 5: More on the Selection Structure
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.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Controlling Program Flow with 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.
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.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
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:

Programming with Microsoft Visual Basic 2010 5th 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 1st and 2nd grade students

Previewing the Math Practice Application (cont’d.) Figure 5-1 Math Practice application’s user interface

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

Lesson A Objectives (cont’d.) Include a multiple-alternative selection structure in pseudocode and in a flowchart Code a multiple-alternative selection structure

Nested Selection Structures Used to make a decision and select the appropriate path Nested selection structure Selection structure that lies on true or false path of an outer selection structure Two decisions are involved Primary decision: Made by outer structure Secondary decision: Made by inner structure

Figure 5-3 Selection structures you might use today

The Voter Eligibility Application Figure 5-4 Problem specification for the Voter Eligibility application

The Voter Eligibility Application (cont’d.) Figure 5-5 Sample run of the Voter Eligibility application

Figure 5-6 Flowchart showing the nested selection structure in the true path

Figure 5-7 Flowchart showing the nested selection structure in the false path

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

Figure 5-8 Code for the flowcharts in Figures 5-6 and 5-7 (cont’d.)

Logic Errors in Selection Structures Common logic errors 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 a task

Logic Errors in Selection Structures (cont’d.) 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

Logic Errors in Selection Structures (cont’d.) Figure 5-9 A correct algorithm for the bonus procedure

Logic Errors in Selection Structures (cont’d.) Figure 5-10 Results of desk-checking the correct algorithm

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

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

Figure 5-12 Results of desk-checking the incorrect algorithm shown in Figure 5-11

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

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

Reversing the Primary and Secondary Decisions (cont’d.) Figure 5-14 Results of desk-checking the incorrect algorithm shown in Figure 5-13

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

Using an Unnecessary Nested Selection Structure (cont’d.) Figure 5-15 Correct algorithm and an incorrect algorithm containing the third logic error

Using an Unnecessary Nested Selection Structure (cont’d.) Figure 5-16 Results of desk-checking the inefficient algorithm shown in Figure 5-15

Multiple-Alternative Selection Structures Designed to choose from several alternatives Condition in multiple-alternative 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

Multiple-Alternative Selection Structures (cont’d.) Figure 5-17 Letter grades and messages

Figure 5-18 Pseudocode containing a multiple-alternative selection structure

Figure 5-19 Flowchart containing a multiple-alternative selection structure

Multiple-Alternative Selection Structures (cont’d.) Figure 5-20 Shows two versions of code for btnDisplay control’s Click event procedure Both versions use If…Then…Else statements Second version contains more convenient way of writing a multiple- alternative selection structure

Figure 5-20 Two versions of the code containing a multiple-alternative selection structure (continues)

Figure 5-20 Two versions of the code containing a multiple-alternative selection structure (cont’d.)

The Select Case Statement 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

Figure 5-22 Syntax and an example of the Select Case statement (continues)

Figure 5-22 Syntax and an example of the Select Case statement (cont’d

Specifying a Range of Values in a Case Clause To specify range of minimum and maximum values 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 Comparison operators used with Is = , < , <= , > , >= , <>

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

Lesson A Summary Nested selection structure lies on true or false path of outer selection structure Desk-check Validate algorithm by hand with sample data Multiple-alternative 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

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

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

Adding a Radio Button to the Form 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 Each group of radio buttons must be placed within container such as group box Default radio button: Button initially selected Set Checked property to True

Adding a Radio Button to the Form (cont’d.) Figure 5-29 Subtraction radio button added to the Operation group box

Adding a Check Box Control to the Form 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

Adding a Check Box Control to the Form (cont’d.) Figure 5-30 Display summary check box added to the form

Coding the Math Practice Application Procedures to code based on TOE chart Form’s load event procedure Click event procedures for seven controls One task will be coded with an independent Sub procedure Generating and displaying two random numbers

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

Figure 5-33 Independent Sub procedure syntax, example, and steps

Figure 5-34 Pseudocode for the GenerateAndDisplayIntegers procedure

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

Figure 5-35 Syntax and examples of generating random integers

Figure 5-36 Random number generation code entered in the procedure

Figure 5-37 Additional comments and code entered in the procedure

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

Figure 5-38 Syntax and examples of the Call statement

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

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

Lesson B Summary Radio button control Check box control Limits user to only one choice among group of choices Check box control Allows user to select multiple choices in a group Independent Sub procedures Not linked to controls and can be invoked anywhere in program Use Random object to generate pseudo-random numbers

Lesson B Summary (cont’d.) Call statement Used to call (invoke) independent Sub procedure Use object.PerformClick() to invoke object’s Click event

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

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

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

Figure 5-45 Memory locations used by the btnCheckAnswer control’s Click event procedure

Coding the Display Summary Check Box’s Click Event Procedure chkSummary control’s Click event procedure Displays and hides grpSummary 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 whether check box is selected Visible property indicates whether control is shown

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