1 / 11

Logical Operators

Logical Operators. Used with Boolean expressions Not – makes a False expression True and vice versa And – will yield a True if and only if both expressions are True Or – will yield a True if at least one of both expressions are True. Example. To test if n falls between 2 and 5:

mills
Download Presentation

Logical Operators

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. Logical Operators • Used with Boolean expressions • Not – makes a False expression True and vice versa • And – will yield a True if and only if both expressions are True • Or – will yield a True if at least one of both expressions are True Chapter 5

  2. Example To test if n falls between 2 and 5: (2 < n ) And ( n < 5 ) Chapter 5

  3. Syntax error The following is NOT a correct way to test if n falls between 2 and 5: (2 < n < 5 ) Chapter 5

  4. Example n = 4, answ = “Y” Are the following expressions true or false? Not (n < 6) (answ = "Y") Or (answ = "y") (answ = "Y") And (answ = "y") Not(answ = "y") Chapter 5

  5. Order of Operations The order of operations for evaluating Boolean expressions is: • Arithmetic operators • Relational operators • Logical operators Chapter 5

  6. Arithmetic Order of Operations • Parenthesis • Exponentiation • Division and multiplication • Addition and subtraction Chapter 5

  7. Relational Order of Operations They all have the same precedence Chapter 5

  8. Logical Order of Operations • Not • And • Or Chapter 5

  9. Condition • A condition is an expression involving relational and/or logical operators • Result of the condition is Boolean – that is, True or False Chapter 5

  10. Common Error in Boolean Expressions • A common error is to replace the condition Not ( 2 < 3 ) by the condition ( 2 > 3 ) • The correct replacement is ( 2 >= 3 ) because >= is the opposite of <, just as <= is the opposite of > Chapter 5

  11. Boolean Variable A variable declared with a statement of the form Dim var As Boolean is said to have Boolean data type. It can assume just the two values True and False. Example: Dim boolVar As Boolean boolVar = 2 < 6 txtBox.Text = boolVar displays True in the text box. Chapter 5

More Related