Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic 2010 5 th Edition.

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

Chapter 6: The Repetition Structure
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.
1.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
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.
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
Using the Select Case Statement and the MsgBox Function (Unit 8)
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.
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 FIVE Specifying Alternate Courses of Action: Selection Statements.
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 Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
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 13 Do It, Then Ask Permission (Posttest Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
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 Fourteen Access Databases and SQL 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.
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 Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition

Programming with Microsoft Visual Basic 2010, 5 th Edition Previewing the Math Practice Application Figure 5-1 Math Practice application’s user interface Open Math.exe file The Math Practice application will be used by 1 st and 2 nd grade students ( 實作程式的測試規格在 262~263 頁 ) ( 程式的實作規格在投影片 21 頁 ) 2

Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson A Objectives After studying Lesson A, you should be able to: Include a nested selection structure in pseudocode Code a nested selection structure Recognize common logic errors in selection structures Code a multiple-alternative selection structure 3

Programming with Microsoft Visual Basic 2010, 5 th Edition Nested Selection Structures Nested selection structure An inner 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 example 4

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-3 Selection structures you might use today 5

Programming with Microsoft Visual Basic 2010, 5 th Edition 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 6

Programming with Microsoft Visual Basic 2010, 5 th Edition Multiple-Alternative Selection Structures Multiple-alternative selection structures Designed to choose from several alternatives Condition in multiple-alternative selection structure does not require true/false answer Requires an expression whose value determines which path is chosen Example: Display message based on grades ranging from A to F example 7

Programming with Microsoft Visual Basic 2010, 5 th Edition Example of Multiple-Alternative Selection Structures Figure 5-17 Letter grades and messages 8

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-18 Pseudocode containing a multiple-alternative selection structure flowchart Example of Multiple-Alternative Selection Structures 9

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-19 Flowchart containing a multiple-alternative selection structure 10

Programming with Microsoft Visual Basic 2010, 5 th Edition Program Codes of Multiple-Alternative Selection Structures 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 1 st Version 2 nd Version 11

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-20 Two versions of the code containing a multiple-alternative selection structure (continues) 12

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-20 Two versions of the code containing a multiple-alternative selection structure (cont’d.) 13

Programming with Microsoft Visual Basic 2010, 5 th Edition The Select Case Statement Simpler and clearer than IF-THEN-ELSE statement 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 syntax 14

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-22 Syntax and an example of the Select Case statement (continues) example 15

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-22 Syntax and an example of the Select Case statement (cont’d.) IF example 16

Programming with Microsoft Visual Basic 2010, 5 th Edition Specifying a Range of Values in a Case Clause To specify range of minimum and maximum values To keyword: Used if both upper and lower bounds are known Example : Case 1 To 5 Is keyword: Used if only upper or lower bound is known Example : Case Is > 10 Comparison operators are used with Is =,, >=, <> Statement example 17

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-23 Syntax and an example of specifying a range of values 18

Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson A Summary Nested selection structure lies on true or false path of outer selection structure 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 19

Programming with Microsoft Visual Basic 2010, 5 th 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 20

Programming with Microsoft Visual Basic 2010, 5 th Edition 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 are 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 UI 21

Programming with Microsoft Visual Basic 2010, 5 th Edition Adding a Radio Button to the Form Radio Button Limits user to one choice in a 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  實作練習 291~292 頁 example 22

Programming with Microsoft Visual Basic 2010, 5 th Edition Adding a Radio Button to the Form (cont’d.) Figure 5-29 Subtraction radio button added to the Operation group box 23

Programming with Microsoft Visual Basic 2010, 5 th Edition Adding a Check Box Control to the Form Check box Allows multiple selections among group of choices Any number of check boxes can be selected at once 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  實作練習 292~294 頁 UI 24

Programming with Microsoft Visual Basic 2010, 5 th Edition Adding a Check Box Control to the Form (cont’d.) Figure 5-30 Display summary check box added to the form 25

Programming with Microsoft Visual Basic 2010, 5 th Edition Generating Random Integers Pseudo-random number generator A mechanism 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 by Random.Next method 26

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-35 Syntax and examples of generating random integers 27

Programming with Microsoft Visual Basic 2010, 5 th Edition Creating an Independent Sub Procedure Event procedure A 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 Promotes modularity syntax & example 28

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-33 Independent Sub procedure syntax, example, and steps 29

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding 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 UI pseudocode 30

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-34 Pseudocode for the GenerateAndDisplayIntegers procedure 31

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-36 Random number generation code entered in the procedure 32

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-37 Additional comments and code entered in the procedure  實作練習 298~300 頁 33

Programming with Microsoft Visual Basic 2010, 5 th 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 Syntax & example 34

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-38 Syntax and examples of the Call statement  實作練習 302 頁 35

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the Operation Radio Buttons’ Click Event Procedures Requirements for Click event procedures Display appropriate icon of mathematical operator (+ or -) Generate and display two random integers Show addition problem by default when program starts  實作練習 303 頁 UI 36

Programming with Microsoft Visual Basic 2010, 5 th 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  實作練習 304 頁 37

Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Summary Radio button control Limits user to only one choice among a 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 Call statement Used to call (invoke) independent Sub procedure Use object.PerformClick() to invoke object’s Click event from a program 38

Programming with Microsoft Visual Basic 2010, 5 th 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 39

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the Check Answer Button’s Click Event Procedure Variables and named constants needed for the click event procedure of Check Answer button Six integer variables Two of them are static variables One named constant pseudocode 40

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 5-44 Pseudocode for the btnCheckAnswer control’s Click event procedure  實作練習 313~314 頁 41

Programming with Microsoft Visual Basic 2010, 5 th Edition 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  實作練習 315~316 頁 42

Programming with Microsoft Visual Basic 2010, 5 th 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 43