170 likes | 177 Vues
Topics. Decision Making in Code Logical Tests & Truth Logical Expressions Logical Operators & Precedence Range Testing The Not Operator Boolean Properties. “Home computers are being called upon to perform many new functions, including the consumption of homework formerly eaten by the dog”
E N D
Topics • Decision Making in Code • Logical Tests & Truth • Logical Expressions • Logical Operators & Precedence • Range Testing • The Not Operator • Boolean Properties “Home computers are being called upon to perform many new functions, including the consumption of homework formerly eaten by the dog” Doug Larson
Decision Making Overview • Decision making in code is one of the three fundamental programming constructs • Sequence • Selection or Choice • Iteration • Allows program to execute designated code when certain conditions are met If dblTotal <= 5000 Then sglTax = dblTotal * .06 Else sglTax = (5000 * .06) + (sglTotal - 5000) * .05 End If What policy is implemented in this code?
Beauty is Truth, Truth Beauty (John Keats) • Programming often depends on testing a condition to control program behavior • Whether specific code will execute • How many times a loop executes • Condition testing is accomplished by evaluation whether an expression is True or False • True and False are literal Boolean values • Dim blnProgrammingIsEasy as BooleanblnProgrammingIsEasy = True • Logical tests are performed using rules of logic and precedence
Logical Tests • Logical tests always evaluate to a result of either True or False • In the test If dblTotal <= 5000 Then the current value of the variable dblTotal is tested to see if it is less than or equal to the literal value 5000 • The entire expression dblTotal <= 5000 will be replaced with True or False depending on the value of dblTotal • If the expression is True the following code will execute
Logical Expressions & Evaluation • Logical expressions are tests • Made with logical operators • Evaluate to True or False • Logical operators < Less than <= Less than or equal to = Equal to > Greater than >= Greater than or equal to <> Not equal to Require two characters to write
Logical Expressions & Evaluation (cont.) • Evaluating numeric expressions • If intAge <= 100 Then • If intAge <= Val(txtAge.Text) Then • Either term in the test may be provided by a • Literal • Variable • Function result • Constant • Boolean property • Calculation result • Dissimilar numeric data types can be compared but the results may be unexpected • Best to convert before comparing
Logical Expressions & Evaluation (cont.) • Evaluating string data types • String evaluations are made according to the ASCII codes of each value in the strings • “abc” is less than “abc “ • “ABC” is less than “abc” • “abcd” is greater than “abc” • UCase(“abc”) is equal to “ABC” • “123” is less than “234” • “45” is greater than “123” Space ???
Logical Expressions & Evaluation (cont.) • Logical operators • And works on multiple conditions and requires both tests to be true for the overall test to be true If intX >= 3 And intX <= 8 Then • Or will have the overall test true if either test is true If intX < 3 Or intX > 8 Then • Not reverses the test result to which it is applied If Not intX = 2 Then The line continuation character can be useful when constructing complex logical tests
Logical Expressions & Evaluation (cont.) • Compound tests with the “And” operator If x >= 3 And x <= 8 Then First Second x Test Test Overall 2 False True False 3 True True True 10 True False False
Logical Expressions & Evaluation (cont.) • Compound tests with the “Or” operator If x >= 3 Or x <= 8 Then First Second x Test Test Overall 2 False True True 3 True True True 10 True False True If x <= 3 Or x >= 8 Then 5 False False False
Logical Expressions & Evaluation (cont.) • Precedence of Logical Operators • Not • And • Or • Left to right in case of ties x >= 0 And x < 3 Or x > 8 And x <= 10 __________ Or __________ __________
Logical Expressions & Evaluation (cont.) • Parentheses can suppress natural evaluation order • Natural evaluation sequence (x >= 0 And x < 3) Or x = 10 • Changing the precedence of evaluation x >= 0 And (x < 3 Or x = 10) • Use parentheses to visually group tests (x >= 0) Or (x <> y And x < 3) Or (x = 10) • Use parentheses to group tests for the Not operator Not (x = y Or y < 5)
Range Testing • It is common to test a value against a range • Value in a range • intAge >= 18 And intAge <= 30 • Value not in a range • intAge < 18 And intAge > 30 • Not (intAge >= 18 And intAge <= 30) • Value in a complex range • intAge >= 18 And intAge <= 30 Or intAge > 60 • Pay careful attention to the inequality operators and end point values when testing ranges • Avoid overlap of range segments
Range Testing (cont.) • You cannot do this: • 18 <= intAge <= 30 • Test must be performed as two independent tests of intAge with an appropriate operator (And or Or) • Range tests can be combined with other tests • If intAge < 18 _ And sglTotHoursWorked > 20 _ And (Now.Month < 6 _ Or Now.Month > 8)
The Not Operator • Not reverses the Boolean status of whatever immediately follows it • Not True = False – Not False = True • Not 4 <= 5 = Not True = False • 4 <= 5 And Not 5 > 4 = True And Not True = True And False = False • Parentheses can extend the scope of Not • Not (4 < 5 Or 5 < 4) = Not (True Or False) = Not (True) = False • Not (4 < 5 Or Not 5 < 4) = Not (True Or Not False) = Not (True Or True) = Not (True) = False
Boolean Properties • Logical tests must evaluate to True or False • Many properties are Boolean in that they have values of True or False • The following tests are equivalent • If chkIsValid.Checked = True Then • If chkIsValid.Checked Then • The same applies to Boolean variables and constants • If blnIsValid = True Then • If blnIsValid Then
Exercises • Write out using the logical operators but English expressions for values the logic to determine if: • You passed all of your classes last semester • You passed any of your classes last semester • You are ready to graduate this semester • Evaluate the following: • True And False OR False Or Not False • (True And False) OR False Or Not False