when are decisions used?

23
© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem- Solving Approach 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

Upload: schuyler

Post on 05-Jan-2016

33 views

Category:

Documents


1 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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

Page 2: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Simple DecisionsIf condition Then

‘ statement(s)End If

If condition Then‘ statement(s) for

trueElse

‘ statement(s) for falseEnd If

Condition ?TrueFalse

Steps to processif condition istrue

Condition ?TrueFalse

Steps to processif condition istrue

Steps to processif condition isfalse

Page 3: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Relational OperatorsUsed to compare two values in a simple condition

Symbol Relation Example Expression

> greater than StdGrade > AverageEmpName > "Harriger"

< less than Sales < Anticipated

= equal to FirstName = “Alka”Score = 100

<> not equal to Qualify <> “yes”

>= greater thanor equal to

WeeklyHours >= 40

<= less than orequal to

TotalPoints <= 59.9

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)

Page 4: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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

Page 5: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Logical OperatorsUsed to form compound (multiple) conditions

Oper’tr Usage Truth Table Sample Expressions

Not(evaluated1st)

Reverses the condition(true -> false and viceversa)

a Not aT FF T

Not x < 10

And(evaluated2nd)

If both conditions aretrue, the result is true

a b (a And b)T T TT F FF T FF F F

x >= 10 And x <=20

Or(evaluated3rd)

If either condition istrue, the result is true

a b (a Or b)T T TT F TF T TF F F

x < 10 Or y < 10

Page 6: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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

Page 7: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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 spouseIf txtStatus.Text = “single” AND _

txtSpouse.Text <> “” Then

‘ do error processing - spouse given for single person

End If

Page 8: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Types of Validation Checks Existence Check Type Check Range Check Reasonableness Check Code Check Consistency Check

Page 9: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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

Page 10: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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

Page 11: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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

Page 12: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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

Page 13: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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

Page 14: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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

Page 15: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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

Page 16: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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

Page 17: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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

Page 18: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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)

Page 19: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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

Page 20: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Data Validation Example 1Private 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

Page 21: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Data Validation Example 2Private 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

Page 22: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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

Page 23: When are decisions used?

© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

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