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.

Slides:



Advertisements
Similar presentations
1.
Advertisements

1.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
1.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
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.
WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS.
Chapter 3: Using Variables and Constants
Chapter Four The Selection Structure
Using the Select Case Statement and the MsgBox Function (Unit 8)
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
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?
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Java Programming, Second Edition Chapter Five Input and Selection.
Input, Output, and Processing
Selection Structure If... Then.. Else Case. Selection Structure Use to make a decision or comparison and then, based on the result of that decision or.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Working with option button, check box, and list box controls Visual Basic for Applications 13.
Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
Introduction to Programming with RAPTOR
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
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.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
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 Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
The Selection Structure
Chapter 4: The Selection Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005 BASICS
Making Decisions in a Program
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
Boolean Expressions to Make Comparisons
Chapter 5: The Selection Structure
Presentation transcript:

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

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

Tutorial 43 Selection Structure Flowcharts T F TF

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

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

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

Tutorial 47 Expressions Containing Relational Operators < 5 * 2 5 * 2 is evaluated first, giving is evaluated second, giving < 10 is evaluated last, giving false 7 > 3 * 4 / 2 3 * 4 is evaluated first, giving / 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

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”

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

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

Tutorial 411 Truth Table for the Not Operator

Tutorial 412 Truth Table for the And Operator

Tutorial 413 Truth Table for the Or Operator

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 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

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

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

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

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

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

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

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

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

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

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

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.

Tutorial 426 Nested If Example 2 If intCode = 1 Then If sngSales >= Then sngBonus = 500 Else sngBonus = 200 End If Else If intCode = 2 Then If sngSales >= Then sngBonus = 600 Else sngBonus = 550 Else sngBonus = 150 End If

Tutorial 427 Nested If Example 2 If intCode = 1 And sngSales >= Then sngBonus = 500 Else If intCode = 1 And sngSales < Then sngBonus = 200 Else If intCode = 2 And sngSales >= Then sngBonus = 600 Else If intCode = 2 And sngSales < Then sngBonus = 550 Else sngBonus = 150 End If

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

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

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

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.

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

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.

Tutorial 434 Select Case Example 2 Select Case intCode Case 1 Select Case sngSales Case Is >= sngBonus = 500 Case Else sngBonus = 200 End Select Case 2 Select Case sngSales Case Is >= sngBonus = 600 Case Else sngBonus = 550 End Select Case Else sngBonus = 150 End Select

Tutorial 435 Select Case Example 2 Select Case True Case intCode = 1 And sngSales >= sngBonus = 500 Case intCode = 1 And sngSales < sngBonus = 200 Case intCode = 2 And sngSales >= sngBonus = 600 Case intCode = 2 And sngSales < sngBonus = 550 Case Else sngBonus = 150 End Select

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

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

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

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

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)

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

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

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

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

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

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

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

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

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

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

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.

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

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

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

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)

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

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.