Chapter 10 So Many Paths So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition

Embed Size (px)

DESCRIPTION

Which Way Should I Go? Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 3 Multiple-alternative selection structures Can choose from several alternatives Figure 10-1 Contains a problem specification that requires a multiple-alternative selection structure Figure 10-2 Shows two ways of coding the multiple-alternative selection structure from Figure 10-1 Figure 10-3 A third example of the multiple-alternative selection structure

Citation preview

Chapter 10 So Many Paths So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition Objectives Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 2 After studying Chapter 10, you should be able to: Code a multiple-alternative selection structure using If/ElseIf/Else Declare a variable using the String data type Convert a string to uppercase or lowercase Code a multiple-alternative selection structure using Select Case Include a radio button in an interface Which Way Should I Go? Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 3 Multiple-alternative selection structures Can choose from several alternatives Figure 10-1 Contains a problem specification that requires a multiple-alternative selection structure Figure 10-2 Shows two ways of coding the multiple-alternative selection structure from Figure 10-1 Figure 10-3 A third example of the multiple-alternative selection structure Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 4 Figure 10-1 Department code problem specification and algorithm Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 5 Figure 10-2 Two versions of the code for the multiple-alternative selection structure Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 6 Figure 10-3 Fitness for Good problem specification and algorithm Coding the Fitness for Good Application Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 7 Code algorithm shown in Figure 10-3 Start by opening the application Open the Code Editor window Follow instructions on pages String data type Stores alphanumeric text Can contain letters, numbers, and special characters Dont Be So Sensitive Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 8 Case sensitive Uppercase version of a letter not the same as lowercase Unicode Universal coding scheme for characters Assigns a unique numeric value to each character ToUpper method: Converts a string to uppercase ToLower method: Converts a string to lowercase Conversion is temporary in both methods Dont Be So Sensitive (contd.) Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 9 When using the ToUpper method in a comparison: Everything you are comparing should be in uppercase When using the ToLower method in a comparison: Everything you are comparing should be in lowercase Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 10 Figure 10-6 Syntax and examples of the ToUpper and ToLower methods Dont Be So Sensitive (contd.) Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 11 When processing the statement: strCode = txtCode.Text.ToUpper The computer first makes a temporary copy of the string entered in the txtCode control It then converts the copy to uppercase, storing the result in the strCode variable Finally, it removes the copy from its internal memory Whats the Next Case on the Docket? Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 12 Figure 10-8 shows: The Select Case statements syntax How to use the statement to code: A multiple-alternative selection structure that displays a message corresponding to a letter grade Select Case statement Begins with the keywords Select Case, followed by a selectorExpression selectorExpression Can contain any combination of variables, constants, methods, operators, or properties Whats the Next Case on the Docket? (contd.) Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition 13 Select Case Statement You can have as many Case clauses as necessary If the Select Case statement includes a Case Else clause: The Case Else clause must be the last clause in the statement Each of the individual Case clauses, except the Case Else clause, must contain an expressionList Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 14 Figure 10-8 Syntax and an example of the Select Case statement Using Select Case in the Fitness for Good Application Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 15 Code the application using the instructions on pages Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 16 Figure 10-9 btnDisplay controls Click event procedure Specifying a Range of Values in a Case Clauses Expression List Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 17 You can specify a range of values in a Case clauses expressionList You do this using either the keyword To or the keyword Is To keyword Used when you know both the upper and lower bounds of the range Is keyword Used when you know only one end of the range Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 18 Figure Example of using the To and Is keywords in a Case clause Coding the ABC Corporation Application Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 19 Follow the instructions on pages Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 20 Figure btnDisplayPrice controls Click event procedure Using Radio Buttons Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 21 If/ElseIf/Else and Case forms of the selection structure Often used when coding interfaces that contain radio buttons Radio button Created using the RadioButton tool in the toolbox Allows you to limit the user to only one choice in a group of two or more Default radio button Radio button that represents the users most likely choice or the first radio button in the group Coding the Gentry Supplies Application Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 22 Figure Gentry Supplies shipping chart Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 23 Figure Selection structures entered in the procedure Summary Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 24 Solutions to some problems require a multiple-alternative selection structure Coding a multiple-alternative selection structure Use either the If...Then...Else statement Or the Select Case statement String comparisons are case sensitive Convert to either uppercase or lowercase before comparing Each character on the computer keyboard Associated with a unique Unicode value Summary (contd.) Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 25 To or Is keywords Specify a range of values in Select Case statement Radio buttons Limit the user to one choice from a group of two or more For two groups of radio buttons in an interface: At least one must be placed within a container Boolean value stored in buttons Checked property Determines whether the radio button is selected (True) or unselected (False)