Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 41 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.

Similar presentations


Presentation on theme: "Tutorial 41 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."— Presentation transcript:

1 Tutorial 41 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

2 Tutorial 42 Flowchart Symbols start/stop oval process rectangle input/output parallelogram selection/repetition diamond symbols are connected by flowlines

3 Tutorial 43 Selection Structure Flowcharts T F TF

4 Tutorial 44 Selection Structure Pseudocode 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

5 Tutorial 45 If..Then…Else Statement If condition Then [instructions when the condition is true] [Else [instructions when the condition is false]] End If

6 Tutorial 46 Relational Operators = > >= < <= <> Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to These operators are evaluated from left to right, and are evaluated after any mathematical operators

7 Tutorial 47 Expressions Containing Relational Operators 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 All expressions containing a relational operator will result in either a true or false answer only

8 Tutorial 48 Examples of Relational Operators used in the condition 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”

9 Tutorial 49 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

10 Tutorial 410 Logical Operators 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 These operators are evaluated after any mathematical and relational operators. The order of precedence is Not, And, Or

11 Tutorial 411 Truth Table for the Not Operator

12 Tutorial 412 Truth Table for the And Operator

13 Tutorial 413 Truth Table for the Or Operator

14 Tutorial 414 Expressions Containing the And Logical Operator 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 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

15 Tutorial 415 Expression Containing the Or Logical Operator 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 All expressions containing a relational operator will result in either a true or false answer only

16 Tutorial 416 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

17 Tutorial 417 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

18 Tutorial 418 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

19 Tutorial 419 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

20 Tutorial 420 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

21 Tutorial 421 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

22 Tutorial 422 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

23 Tutorial 423 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

24 Tutorial 424 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

25 Tutorial 425 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.

26 Tutorial 426 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

27 Tutorial 427 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

28 Tutorial 428 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

29 Tutorial 429 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

30 Tutorial 430 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

31 Tutorial 431 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.

32 Tutorial 432 Select Case Example 1 Select Case intCode Case 1, 3 sngTax =.04 Case 2 sngTax =.05 Case Else sngTax =.02 End Select

33 Tutorial 433 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.

34 Tutorial 434 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

35 Tutorial 435 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

36 Tutorial 436 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

37 Tutorial 437 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

38 Tutorial 438 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

39 Tutorial 439 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

40 Tutorial 440 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)

41 Tutorial 441 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

42 Tutorial 442 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

43 Tutorial 443 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

44 Tutorial 444 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 Call name [(argumentlist)], to invoke a user-defined sub procedure

45 Tutorial 445 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

46 Tutorial 446 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

47 Tutorial 447 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

48 Tutorial 448 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

49 Tutorial 449 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

50 Tutorial 450 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

51 Tutorial 451 buttons Argument ConstantValueDescription vbOKOnly0Display OK button only. vbOKCancel1Display OK and Cancel buttons. vbAbortRetryIgnore2Display Abort, Retry, and Ignore buttons. vbYesNoCancel3Display Yes, No, and Cancel buttons. vbYesNo4Display Yes and No buttons. vbRetryCancel5Display Retry and Cancel buttons. vbCritical16Display Critical Message icon. vbQuestion32Display Warning Query icon. vbExclamation48Display Warning Message icon. vbInformation64Display Information Message icon. vbDefaultButton10First button is default. vbDefaultButton2256Second button is default. vbDefaultButton3512Third button is default. vbDefaultButton4768Fourth button is default. vbApplicationModal0Application modal; the user must respond to the message box before continuing work in the current application. vbSystemModal4096System modal; all applications are suspended until the user responds to the message box.

52 Tutorial 452 Return Values ConstantValueDescription vbOK1OK vbCancel2Cancel vbAbort3Abort vbRetry4Retry vbIgnore5Ignore vbYes6Yes vbNo7No

53 Tutorial 453 SelStart Property of a Text Box Tells Visual Basic where to position the insertion point Syntax: object.SelStart [ = index] The first position is position (index) 0

54 Tutorial 454 SelLength Property of a Text Box Tells Visual Basic how many characters to select Syntax: object.SelLength [ = number]

55 Tutorial 455 Len Function You can use the Len function to determine the number of characters contained in a text box Syntax: Len(textbox.Text)

56 Tutorial 456 Highlighting (selecting) Existing Text Use the following two lines of code: textbox.SelStart = 0 textbox.SelLength = Len(textbox.Text) Enter the lines of code in the text box’s GotFocus event, which occurs when an object receives the focus

57 Tutorial 457 Debugging Technique You can use Visual Basic’s Print method to verify the contents of the application’s variables as the application is running.


Download ppt "Tutorial 41 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."

Similar presentations


Ads by Google