Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Four The Selection Structure Programming with Microsoft Visual Basic 2010 5 th Edition.

Similar presentations


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

1 Chapter Four The Selection Structure Programming with Microsoft Visual Basic 2010 5 th Edition

2 Programming with Microsoft Visual Basic 2010, 5 th Edition 2 Figure 4-2 Monthly payment amount shown in the interface Figure 4-1 Message box Previewing Monthly Payment Calculator Application Monthly Payment Calculator uses selection structure  實作程式的主測試規格在 191 頁

3 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson A Objectives 3 After studying Lesson A, you should be able to: Write pseudocode for the selection structure Write an If...Then...Else statement Include comparison operators and logical operators in a selection structure’s condition Change the case of a string Determine the success of the TryParse method

4 Programming with Microsoft Visual Basic 2010, 5 th Edition Making Decisions in a Program 4 Three basic control structures Sequence Selection Repetition All procedures in an application are written using one or more of these structures

5 Programming with Microsoft Visual Basic 2010, 5 th Edition Making Decisions in a Program (cont’d.) 5 Selection structure Also called a decision structure Chooses one of two paths based on a condition Condition Decision expression evaluating to true or false Example of a selection structure: If employee works over 40 hours, add overtime pay

6 Programming with Microsoft Visual Basic 2010, 5 th Edition Making Decisions in a Program (cont’d.) 6 Single-alternative ( 單選擇 ) selection structure A set of tasks performed only when condition is true Dual-alternative ( 雙選擇 ) selection structure True path A set of tasks performed if condition is true False path A set of tasks performed if condition is false If and end if Denotes selection structure’s beginning and end Else denotes beginning of false path example

7 Programming with Microsoft Visual Basic 2010, 5 th Edition Making Decisions in a Program (cont’d.) 7 Figure 4-3 Selection structures you might use today

8 Programming with Microsoft Visual Basic 2010, 5 th Edition Coding Single-Alternative and Dual- Alternative Selection Structures 8 If…Then…Else statement Used to code single and dual-alternative selection structures Statement block A set of statements in each path Single example Dual example

9 Programming with Microsoft Visual Basic 2010, 5 th Edition 9 Figure 4-11 Syntax and examples of the If…Then…Else statement (continues)

10 Programming with Microsoft Visual Basic 2010, 5 th Edition 10 Figure 4-11 Syntax and examples of the If…Then…Else statement (cont’d.)

11 Programming with Microsoft Visual Basic 2010, 5 th Edition Comparison Operators 11 Comparison operators Used to compare two values Always result in a True or False value Rules for comparison operators They do not have an order of precedence They are evaluated from left to right They are evaluated after any arithmetic operators in an expression operators

12 Programming with Microsoft Visual Basic 2010, 5 th Edition 12 Figure 4-12 Listing and examples of commonly used comparison operators

13 Programming with Microsoft Visual Basic 2010, 5 th Edition Logical Operators 13 Logical operators Used to create compound conditions Expressions evaluate to a Boolean value True or False Six logical operators in Visual Basic Not, And, AndAlso, Or, OrElse, Xor precedence

14 Programming with Microsoft Visual Basic 2010, 5 th Edition 14 Figure 4-23 Listing and examples of logical operators (continues) example

15 Programming with Microsoft Visual Basic 2010, 5 th Edition 15 Figure 4-23 Listing and examples of logical operators (cont’d.)

16 Programming with Microsoft Visual Basic 2010, 5 th Edition Short-Circuit Evaluation of Logical Operators 16 Short-circuit evaluation Bypasses the evaluation of a condition in a compound condition when outcome can be determined without it Short-circuit operators: AndAlso, OrElse Example: If state = "TN" AndAlso sales > $5000 Then… If state is not TN, no need to evaluate sales > $5000

17 Programming with Microsoft Visual Basic 2010, 5 th Edition 17 Figure 4-24 Truth tables for the logical operators

18 Programming with Microsoft Visual Basic 2010, 5 th Edition Converting a String to Uppercase or Lowercase 18 String comparisons are case sensitive CharacterCasing property of text box: Three case values: Normal (default), Upper, Lower ToUpper method: Converts string to uppercase ToLower method: Converts string to lowercase example

19 Programming with Microsoft Visual Basic 2010, 5 th Edition 19 Figure 4-29 Syntax and examples of the ToUpper and ToLower methods (continues)

20 Programming with Microsoft Visual Basic 2010, 5 th Edition Comparing Boolean Values 20 Boolean variable: Contains either True or False Naming convention: “Is” denotes Boolean type Example: blnIsInsured When testing for a True value, it is not necessary to include the “= True” example

21 Programming with Microsoft Visual Basic 2010, 5 th Edition Comparing Boolean Values (cont’d.) 21 Figure 4-32 Examples of using Boolean values in a condition

22 Programming with Microsoft Visual Basic 2010, 5 th Edition Determine if a string is converted to a number 22 TryParse method returns a numeric value after converting a “numerical string”, or 0 if it cannot be converted TryParse also returns a Boolean value indicating success or failure of the conversion attempt Use Boolean value returned by TryParse method in an If…Then…Else statement example

23 Programming with Microsoft Visual Basic 2010, 5 th Edition 23 Figure 4-33 Syntax and example of using the Boolean value returned by the TryParse method

24 Programming with Microsoft Visual Basic 2010, 5 th Edition Summary of Operators 24 Precedence of logical operators Evaluated after any arithmetic or comparison operators in the expression Precedence order Arithmetic  concatenation  comparison  logical operators

25 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson A Summary 25 Single and dual-alternative selection structures Use If...Then...Else statement Use comparison operators to compare two values Use logical operators to create a compound condition Use text box’s CharacterCasing property to change text to upper- or lowercase Use ToUpper and ToLower to temporarily modify the case of a string Use Boolean return value of TryParse method to determine whether a string was successfully converted to a numeric value Arithmetic operators are evaluated first, then comparison operators, and finally logical operators

26 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Objectives 26 After studying Lesson B, you should be able to: Group objects using a GroupBox control Calculate a periodic payment using the Financial.Pmt method Create a message box using the MessageBox.Show method Determine the value returned by a message box

27 Programming with Microsoft Visual Basic 2010, 5 th Edition Creating the Monthly Payment Calculator Application 27 Program requirement Calculate monthly payment on car loan Application needs The loan amount (principal) The annual percentage rate (APR) of interest The life of the loan (term) in years

28 Programming with Microsoft Visual Basic 2010, 5 th Edition Creating the Monthly Payment Calculator Application (cont’d.) 28 GroupBox tool: Group box: Container control for other controls Used to add group box control to interface Group box control provides: Visual separation of related controls Ability to manage the grouped controls by manipulating the group box control Lock controls to ensure that they are not moved Be sure to set TabIndex after placement of controls  實作練習 231~233 頁

29 Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the Monthly Payment Calculator Application 29 Procedures required according to TOE chart Click event procedure code for the two buttons Code for TextChanged, KeyPress, and Enter events for text boxes Procedures that are already coded btnExit Click event and TextChanged events for the text boxes Procedure to code in Lesson B btnCalc button’s Click event procedure Calculate monthly payment amount Display result in lblPayment control TOE

30 Programming with Microsoft Visual Basic 2010, 5 th Edition 30 Figure 4-42 TOE chart for the Monthly Payment Calculator application

31 Programming with Microsoft Visual Basic 2010, 5 th Edition Pseudocode of Monthly Payment Calculator 31 Figure 4-43 Pseudocode for the btnCalc control’s Click event procedure

32 Programming with Microsoft Visual Basic 2010, 5 th Edition 32 Figure 4-44 Partially completed Click event procedure  實作練習 234~236 頁

33 Programming with Microsoft Visual Basic 2010, 5 th Edition Using the Financial.Pmt Method 33 Financial.Pmt method Calculates periodic payment on a loan or investment Must ensure that interest rate and number of periods are expressed in same units (months or years) Convert annual interest rate to monthly rate by dividing by 12 Convert annual term to monthly term by multiplying by 12 Syntax & example

34 Programming with Microsoft Visual Basic 2010, 5 th Edition 34 Figure 4-45 Basic syntax and examples of the Financial.Pmt method

35 Programming with Microsoft Visual Basic 2010, 5 th Edition Using the Financial.Pmt Method (cont’d.) 35 Figure 4-46 Selection structure’s true path coded in the procedure  實作練習 237 頁

36 Programming with Microsoft Visual Basic 2010, 5 th Edition The MessageBox.Show Method 36 MessageBox.show method Displays message box with text message, caption, button(s), and icon Use sentence capitalization for text message Use book title capitalization for caption Icons Exclamation or question: Indicates user must make a decision before continuing Information: Indicates informational message Stop: Indicates serious problem return value & example

37 Programming with Microsoft Visual Basic 2010, 5 th Edition The MessageBox.Show Method 37 Figure 4-50 Values returned by the MessageBox.Show method (continues)

38 Programming with Microsoft Visual Basic 2010, 5 th Edition The MessageBox.Show Method (cont’d.) 38 Figure 4-50 Values returned by the MessageBox.Show method (cont’d.)  實作練習 241~243 頁

39 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Summary 39 Group box: A container control that treats its contents as one unit Use GroupBox tool to add a group box Use Financial.Pmt method to calculate loan or investment payments MessageBox.Show method displays message box with text, one or more buttons, and icon

40 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson C Objectives 40 After studying Lesson C, you should be able to: Prevent the entry of unwanted characters in a text box Select the existing text in a text box

41 Programming with Microsoft Visual Basic 2010, 5 th Edition KeyPress Event 41 KeyPress event Occurs when key is pressed while a control has focus Character corresponding to pressed key is sent to KeyPress event’s e parameter KeyPress event can be used to prevent users from entering inappropriate characters Use e parameter’s KeyChar property to determine pressed key Use Handled property to cancel key if needed example

42 Programming with Microsoft Visual Basic 2010, 5 th Edition 42 Figure 4-57 Examples of using the KeyChar and Handled properties in the KeyPress event procedure

43 Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the KeyPress Event Procedures 43 Figure 4-58 CancelKeys procedure  實作練習 248, 250~251 頁

44 Programming with Microsoft Visual Basic 2010, 5 th Edition Enter Event Procedures 44 Enter event Occurs when text box receives focus If text is selected, user can replace existing text by pressing key Can use Enter event to select all of text SelectAll method Selects all text contained in text box Add to each text box’s Enter event procedure  實作練習 251~254 頁 syntax & example

45 Programming with Microsoft Visual Basic 2010, 5 th Edition SelectAll method 45 Figure 4-59 Syntax and an example of the SelectAll method

46 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson C Summary 46 KeyPress event occurs when user presses key Use KeyPress event to cancel unwanted key pressed by user Use SelectAll method to select all contents of text box Enter event occurs when text box receives focus Use Enter event to process code when control receives focus


Download ppt "Chapter Four The Selection Structure Programming with Microsoft Visual Basic 2010 5 th Edition."

Similar presentations


Ads by Google