 Download Download Presentation When are decisions used?

# When are decisions used?

Download Presentation ## When are decisions used?

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. When are decisions used? • Give user greater control • Skip processing that doesn’t apply to user’s situation • Ex: A questionnaire that asks gender & pregnant • Select processing as requested by user • Ex: An investment program to find either the present value or future value • Data validation • Finding minimums and maximums

2. Simple Decisions False True Condition ? Steps to process if condition is true False True Condition ? Steps to process Steps to process if condition is if condition is false true If condition Then‘ statement(s)End If If condition Then‘ statement(s) for trueElse‘ statement(s) for falseEnd If

3. Relational Operators Used to compare two values in a simple condition Comparing Strings: ASCII Collating Sequence (tear card) Use caution when comparing the text property of text boxes (as a variant, it may be considered a string or number, depending upon what it is being compared to)

4. Generalized Approach forData Validation Function • Assume there will be a problem • Assign invalid return value to function • For each data value input by user, verify the correctness of the data prior to processing • If data is invalid, do the following: • Display message to user to specifying problem • Force user to re-enter data • Exit function • If all input checks were successful • Assign valid return value to function

5. Logical Operators Used to form compound (multiple) conditions

6. Logical Expressions for Validation • Check the invalid cases in the condition • Make sure that salary is between \$500 and \$2500: If Not (Salary >= 500 And Salary <= 2500) Then ‘ do error processing since outside range limits End If is equivalent to If Salary < 500 Or Salary > 2500 Then ‘ do error processing since outside range limits End If • When negating a complex condition, both the relational operators and logical operators must be reversed

7. Logical Expressions for Validation • Check the invalid cases in the condition • Make sure that age is between 21 and 75: If CInt(txtAge.Text) < 21 OR _ CInt(txtAge.Text) > 75 Then ‘ do error processing since outside range limits End If • Make sure single people have no spouse If txtStatus.Text = “single” AND _ txtSpouse.Text <> “” Then ‘ do error processing - spouse given for single person End If

8. Types of Validation Checks • Existence Check • Type Check • Range Check • Reasonableness Check • Code Check • Consistency Check

9. Existence Check • Used to verify that required data was input • AKA presence check • Compare the input value against the empty string. • Example: If last name is required input, use: If txtLast.Text = “” Then ‘ error processing End If

10. Type Check • Used to verify that the type of data input is correct • IsNumeric & IsDate functions • Verifies that input data consists only of valid numbers or legal dates • Example: If salary is a valid number: If Not IsNumeric(txtSalary.Text) Then ‘ error processing End If

11. Range Check • Used to verify that required data falls within valid limits • Compare the input value against the absolute end points • Example: If age must be positive: If CInt(txtAge.Text) <= 0 Then ‘ error processing End If

12. Reasonableness Check • Special case of range check • Used to verify that required data falls within expected limits • Example: If age is unlikely to be under 21: If CInt(txtAge.Text) < 21 Then ‘ error processing - warn user on possible problem End If

13. Code Check • Similar to range check • Look for specific matches (special codes) • Example: If the first two characters of employee ID must be 19: If Left(txtID.Text, 2) <> “19” Then ‘ error processing End If

14. Consistency Check • Used to verify that multiple input data does not establish invalid relationships • Examples: • Hospital bill processing program confirms that labor and delivery charges are for a female patient. • A single employee does not list a spouse. • A 12-year old cannot rent an adult video. • Multiple conditions checked in same condition

15. Displaying messages to user • MsgBox function • Displays modal dialog box to user • Waits for user to click button • Returns integer indicating which button was clicked

16. MsgBox Function • Variable = MsgBox(MsgString, TypeOfWindow) • MsgString is a customized string that appears in the dialog window • TypeOfWindow identifies the icons & buttons displayed • vbOKOnly + vbInformation • vbYesNo + vbQuestion + vbDefaultButton2 • Displays a message & returns value of button user pressed. • Example: Response = MsgBox(“Do you want to continue?”, _ vbYesNo + vbQuestion + vbDefaultButton2) If Response = vbYes Then ‘ continue processing Else ‘ terminate processing EndIf

17. MsgBox “Statement” • Displays a message, but does not keep track of which button user pressed. • Call MsgBox(MsgString, TypeOfWindow) • Example: If Not IsNumeric(txtAge.Text) Then Call MsgBox(“Age must be numeric. Please re-enter.”, _ vbOkOnly + vbInformation) EndIf

18. Force user to re-enter data • Change focus to erroneous textbox Call txtName.SetFocus • Highlight text in textbox txtName.SelStart = 0 txtName.SelLength = Len(txtName.Text)

19. When to do data validation? • Text box’s Change event • Triggered on every minute change • Text box’s LostFocus event • Triggered immediately upon moving the focus out of a text box • Cascading events • Calculate button’s Click event • Call data validation function & bypass calculate processing if return value indicates invalid input

20. Data Validation Example 1 Private Sub cmdCalc_Click() If DataVal Then ‘ do normal processing End If End Sub Private Function DataVal() As Boolean DataVal = False ‘ default return value assumes a problem If Not IsNumeric(txtHours.Text) Then Call MsgBox(“Hours must be numeric-Please re-enter.”, _ vbOkOnly + vbInformation) Call txtHours.SetFocus Exit Function End If ‘ repeat similar decision structure for remaining input text boxes DataVal = True ‘ change return value since no problems discovered End Function

21. Data Validation Example 2 Private Sub cmdCalc_Click() If DataVal = False Then Exit Sub End If ‘ do normal processing End Sub Private Function DataVal() As Boolean DataVal = False ‘ default return value assumes a problem If Not IsNumeric(txtHours.Text) Then Call MsgBox(“Hours must be numeric-Please re-enter.”, _ vbOkOnly + vbInformation) Call txtHours.SetFocus Exit Function End If ‘ repeat similar decision structure for remaining input text boxes DataVal = True ‘ change return value since no problems discovered End Function

22. Minimums and Maximums • Generalized procedure is similar to the counting/accumulating process • Use higher-scope variable • Initialize before processing - two choices • In Form_Load assign maximum to smallest possible value and minimum to largest possible value • During processing, assign minimum and maximum to the first value • This requires special steps to track when the first value is processed • During processing, compare to determine if there is a new minimum or maximum

23. Program Testing Guidelines • Adding selective structures to your program requires extra steps for thorough program testing • Construct sample input data with provisions for each of the following: • Execute all statements at least once • Execute all branches of conditions • Enter different lengths of string input • Enter various types of numeric input • Test common error situations • Repeat the steps in a different order