Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic 2010 5 th Edition."— Presentation transcript:

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

2 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

3 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

4 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

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

6 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

7 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

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

9 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

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

11 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

12 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

13 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

14 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

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

16 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

17 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

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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

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

28 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

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

30 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

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

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

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

34 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

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

36 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

37 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

38 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

39 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

40 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

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

42 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

43 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


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

Similar presentations


Ads by Google