Chapter Four The Selection Structure Programming with Microsoft Visual Basic 2010 5 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.
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Chapter 11: Classes and Objects
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
Selection Logic Building decision-making into programs.
1.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
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.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
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?
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 8 Dental Payment Application Introducing CheckBox es and Message Dialogs.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Chapter 6: The Repetition Structure
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
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.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Chapter 15 I’m on the Inside; You’re on the Outside (Nested Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Four The Selection Structure.
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.
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.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
Chapter 4: Decisions and Conditions
Chapter 11: Testing, Testing…1, 2, 3
Microsoft Visual Basic 2008: Reloaded Third Edition
Chapter 4: Decisions and Conditions
Chapter 3: Using Variables and Constants
The Selection Structure
Chapter 4: The Selection Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
An Introduction to Programming with C++ Fifth Edition
Making Decisions in a Program
Microsoft Visual Basic 2005: Reloaded Second Edition
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
Chapter 5: The Selection Structure
Presentation transcript:

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

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 頁

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 頁

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

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

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

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

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

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

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 頁

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

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)

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 頁

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

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

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

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

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

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

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

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