570 likes | 581 Vues
Selection Structure. Use to make a decision or comparison and then, based on the result of that decision or comparison, to select one of two paths The condition must result in either a true (yes) or false (no) answer
E N D
Selection Structure • Use to make a decision or comparison and then, based on the result of that decision or comparison, to select one of two paths • The condition must result in either a true (yes) or false (no) answer • If the condition is true, the program performs one set of tasks. If the condition is false, there may or may not be a different set of tasks to perform Tutorial 4
Flowchart Symbols • start/stop oval • process rectangle • input/output parallelogram • selection/repetition diamond • symbols are connected by flowlines Tutorial 4
Selection Structure Flowcharts F T T F Tutorial 4
If condition is true Then perform these tasks End If Perform these tasks whether condition is true or false If condition is true then perform these tasks Else perform these tasks End If Perform these tasks whether condition is true or false Selection Structure Pseudocode Tutorial 4
If..Then…Else Statement IfconditionThen [instructions when the condition is true] [Else [instructions when the condition is false]] End If Tutorial 4
= > >= < <= <> Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to Relational Operators These operators are evaluated from left to right, and are evaluated after any mathematical operators Tutorial 4
10 + 3 < 5 * 2 5 * 2 is evaluated first, giving 10 10 + 3 is evaluated second, giving 13 13 < 10 is evaluated last, giving false 7 > 3 * 4 / 2 3 * 4 is evaluated first, giving 12 12 / 2 is evaluated second, giving 6 7 > 6 is evaluated last, giving true Expressions Containing Relational Operators All expressions containing a relational operator will result in either a true or false answer only Tutorial 4
Write a condition that checks if the value stored in the intNum variable is greater than 123 intNum > 123 Write a condition that checks if the value stored in the strName variable is “Mary Smith” UCase(strName) = “MARY SMITH” Examples of Relational Operators used in the condition Tutorial 4
UCase Function • Syntax: UCase(string) • In most programming languages, string comparisons are case sensitive--in other words, the letter “A” is not the same as the letter “a” • Returns the uppercase equivalent of string • Can be used on either side of a comparison, but only on the right side of an assignment Tutorial 4
Not And Or Reverses the truth value of condition; false becomes true and true becomes false All conditions connected by the And operator must be true for the compound condition to be true Only one of the conditions connected by the Or operator needs to be true for the compound condition to be true Logical Operators These operators are evaluated after any mathematical and relational operators. The order of precedence is Not, And, Or Tutorial 4
Truth Table for the Not Operator Tutorial 4
Truth Table for the And Operator Tutorial 4
Truth Table for the Or Operator Tutorial 4
3 > 2 And 6 > 5 3 > 2 is evaluated first, giving true 6 > 5 is evaluated second, giving true true And true is evaluated last, giving true 10 < 25 And 6 > 5 + 1 5 + 1 is evaluated first, giving 6 10 < 25 is evaluated second, giving true 6 > 6 is evaluated third, giving false true And false is evaluated last, giving false Expressions Containing the And Logical Operator Tutorial 4
8 = 4 * 2 Or 7 < 5 4 * 2 is evaluated first, giving 8 8 = 8 is evaluated second, giving true 7 > 5 is evaluated third, giving false true Or false is evaluated last, giving true Expression Containing the Or Logical Operator All expressions containing a relational operator will result in either a true or false answer only Tutorial 4
Evaluation of Expressions Containing Logical Operators • If you use the And operator to combine two conditions, Visual Basic does not evaluate the second condition if the first condition is false • If you use the Or operator to combine two conditions, Visual Basic does not evaluate the second condition if the first condition is true Tutorial 4
Example of Logical Operators used in the condition • To pass a course, a student must have an average test score of at least 75 and an average project score of at least 35. Write the condition using the variables sngTest and sngProj sngTest >= 75 And sngProj >= 35 Tutorial 4
Example of Logical Operators used in the condition • Only people living in the state of Michigan who are over 65 years old receive a discount. Write the condition using the variables strState and intAge UCase(strState) = “MICHIGAN” And intAge > 65 Tutorial 4
Example of Logical Operators used in the condition • Only employees with job codes of 34 and 67 will receive a raise. Write the condition using the variable intCode intCode = 34 Or intCode = 67 Tutorial 4
Nested Selection Structure • A nested selection structure is one in which either the true path or the false path includes yet another selection structure • Any of the statements within either the true or false path of one selection structure may be another selection structure Tutorial 4
Nested If in the true path If condition1 Then [instructions when condition1 is true] If condition2 Then [instructions when both condition1 and condition2 are true] [Else [instructions when condition1 is true and condition2 is false]] End If Else [instructions when condition1 is false]] End If Tutorial 4
Nested If in the false path If condition1 Then [instructions when condition1 is true] Else If condition2 Then [instructions when condition1 is false and condition2 is true] [Else [instructions when both condition1 and condition2 are false]] End If End If Tutorial 4
Nested If Example 1 Write a selection structure that assigns a sales tax rate to the sngTax variable. The tax rate is determined by the state code stored in the intCode variable. Codes of 1 and 3 represent a 4% rate; a code of 2 represents a 5% rate. All other codes represent a 2% rate Tutorial 4
Nested If Example 1 If intCode = 1 Or intCode = 3 Then sngTax = .04 Else If intCode = 2 Then sngTax = .05 Else sngTax = .02 End If End If Tutorial 4
Nested If Example 2 Write a selection structure that assigns a bonus to the sngBonus variable. The bonus is determined by the salesperson’s code (intCode) and, in some cases, by the sales amount (sngSales). If the code is 1 and the salesperson sold at least $10,000, then the bonus is $500; otherwise these salespeople receive $200. If the code is 2 and the salesperson sold at least $20,000, then the bonus is $600; otherwise these salespeople receive $550. All others receive $150. Tutorial 4
Nested If Example 2 If intCode = 1 Then If sngSales >= 10000 Then sngBonus = 500 Else sngBonus = 200 End If Else If intCode = 2 Then If sngSales >= 20000 Then sngBonus = 600 Else sngBonus = 550 Else sngBonus = 150 End If End If Tutorial 4
Nested If Example 2 If intCode = 1 And sngSales >= 10000 Then sngBonus = 500 Else If intCode = 1 And sngSales < 10000 Then sngBonus = 200 Else If intCode = 2 And sngSales >= 20000 Then sngBonus = 600 Else If intCode = 2 And sngSales < 20000 Then sngBonus = 550 Else sngBonus = 150 End If End If End If End If Tutorial 4
Case Form of the Selection Structure • Referred to as the extended selection structure • Easier than the nested If to write and understand • Typically used when a selection structure has several paths from which to choose Tutorial 4
Select Case Statement Select Case testexpression [Case expressionlist1 [instructions for the first Case]] [Case expressionlist2 [instructions for the second Case]] [Case expressionlistn [instructions for the nth Case]] [Case Else [instructions for when the testexpression does not match any of the expressionlists]] End Select Tutorial 4
To and Is Keywords • Use the To keyword to specify a range of values when you know both the minimum and maximum values • Use the Is keyword to specify a range of values when you know only one value, either the minimum or the maximum Tutorial 4
Select Case Example 1 Write a selection structure that assigns a sales tax rate to the sngTax variable. The tax rate is determined by the state code stored in the intCode variable. Codes of 1 and 3 represent a 4% rate; a code of 2 represents a 5% rate. All other codes represent a 2% rate. Tutorial 4
Select Case Example 1 Select Case intCode Case 1, 3 sngTax = .04 Case 2 sngTax = .05 Case Else sngTax = .02 End Select Tutorial 4
Select Case Example 2 Write a selection structure that assigns a bonus to the sngBonus variable. The bonus is determined by the salesperson’s code (intCode) and, in some cases, by the sales amount (sngSales). If the code is 1 and the salesperson sold at least $10,000, then the bonus is $500; otherwise these salespeople receive $200. If the code is 2 and the salesperson sold at least $20,000, then the bonus is $600; otherwise these salespeople receive $550. All others receive $150. Tutorial 4
Select Case Example 2 Select Case intCode Case 1 Select Case sngSales Case Is >= 10000 sngBonus = 500 Case Else sngBonus = 200 End Select Case 2 Select Case sngSales Case Is >= 20000 sngBonus = 600 Case Else sngBonus = 550 End Select Case Else sngBonus = 150 End Select Tutorial 4
Select Case Example 2 Select Case True Case intCode = 1 And sngSales >= 10000 sngBonus = 500 Case intCode = 1 And sngSales < 10000 sngBonus = 200 Case intCode = 2 And sngSales >= 20000 sngBonus = 600 Case intCode = 2 And sngSales < 20000 sngBonus = 550 Case Else sngBonus = 150 End Select Tutorial 4
Option Buttons • Used in situations where you want to limit the user to only one of two or more related and mutually exclusive choices • Only one in a group can be selected (on) at any one time • When selected, an option button’s Value property contains the Boolean value True; otherwise it contains the Boolean value False Tutorial 4
Option Buttons • Minimum number in an interface is two • Recommended maximum number is seven • Use sentence capitalization for the caption • Assign a unique access key • A default button should be selected when the interface first appears Tutorial 4
Option Buttons • You must use a frame control if you want the interface to contain more than one group of option buttons • Set the TabIndex property of the option buttons in each group so that the user can use the up and down arrow keys to select another button in the group Tutorial 4
Frame Control • Acts as a container for other controls • Used to visually separate controls from one another • The frame and the controls contained within the frame are treated as one unit • You must use a frame control if you want to have more than one group of option buttons • Use sentence capitalization for the optional caption Tutorial 4
Check Box • Used in situations where you want to allow the user to select any number of choices from one or more independent and non-exclusive choices • Any number of check boxes can be selected at any one time • When selected, a check box’s Value property contains the number 1 (vbChecked). When unselected, it contains the number 0 (vbUnchecked) Tutorial 4
Check Box • Use sentence capitalization for the check box’s Caption • Assign a unique access to each check box • You also can use the spacebar to select/deselect a check box that has the focus Tutorial 4
Randomize Statement and the Rnd Function • The Randomize statement initializes Visual Basic’s random-number generator • The Rnd function generates random decimal numbers within the 0 to 1 range, including 0 but not including 1 Tutorial 4
Rnd Function • To generate random decimal numbers in a range other than 0 to 1: (upperbound - lowerbound + 1) * Rnd + lowerbound • To generate random integers: Int((upperbound - lowerbound + 1) * Rnd + lowerbound) • lowerbound is the lowest integer portion of the range • upperbound is the highest integer portion of the range Tutorial 4
User-defined Sub Procedure • A collection of code that can be invoked from one or more places in a program • Can receive variables or constants, called arguments, that you send (pass) to it • The arguments, if any, are listed inside the parentheses following the procedure’s name • You use the Call statement, whose syntax is Callname [(argumentlist)], to invoke a user-defined sub procedure Tutorial 4
Swapping • To swap the contents of two variables: • Assign the first variable’s value to a temporary variable • Assign the second variable’s value to the first variable • Assign the temporary variable’s value to the second variable Tutorial 4
Local vs Static Variables • A local variable is defined in an event procedure, and it is removed from memory when that event procedure ends • A static variable also is defined in an event procedure, but it retains its value when that event procedure ends • A static variable is a special type of local variable Tutorial 4
LoadPicture Function • Use to display (clear) a graphic in (from) a form, picture box, or image control • Syntax: LoadPicture([stringexpression]) • stringexpression is the location and name of a graphics file, and it is enclosed in quotation marks • LoadPicture() clears the graphic from the control Tutorial 4
MsgBox Function • Displays one of Visual Basic’s predefined dialog boxes, which contains a message, one or more command buttons, and an icon • After displaying the dialog box, the MsgBox function waits for the user to choose a button, then returns a value that indicates which button the user selected Tutorial 4
MsgBox Function • Syntax: MsgBox(prompt[, buttons][, title][,helpfile, context]) • prompt is the message you want displayed • buttons is a numeric expression that specifies the number and type of buttons, the icon, the default button, and the modality • title is a string expression displayed in the title bar Tutorial 4
Modality • Application modal • Default modality; user must respond to the dialog box’s message before he or she can continue working in the current application; the user still can access other applications • System modal • All applications are suspended until the user responds to the dialog box’s message Tutorial 4