1 / 17

Topics

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”

hbutler
Download Presentation

Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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?

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

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

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

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

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

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

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

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

  11. 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 __________ __________

  12. 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)

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

  14. 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)

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

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

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

More Related