Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 106 Logic and Conditionals (Boolean Expressions)

Similar presentations


Presentation on theme: "CS 106 Logic and Conditionals (Boolean Expressions)"— Presentation transcript:

1 CS 106 Logic and Conditionals (Boolean Expressions)

2 Enlarging our Repertoire So far we know how to write VB statements that change the values of variables, including properties of objects Our next type of statement is a conditional: it allows us to change what the program does based on the outcome of a test 2

3 Tests and Choices To allow a program to make choices based on conditions we need to introduce a new programming construct But first we look at how we program the tests that will determine which branch our program takes 3

4 Expressions An expression in VBA is a chunk of code that evaluates to a value Arithmetic expressions include simple numbers and expressions built with arithmetic operators and functions, such as 44 varA + 12 String expressions include simple strings and expressions built with the string operator & and possibly string functions “Hello, I’m a string.” “Your cost is “ & FormatCurrency(iceCreamTotal) 4

5 The Boolean Data Type (named for George Boole) A Boolean variable is one that has two possible values: True and False A Boolean expression is an expression that can evaluate to True or False The constants True and False are the simplest Boolean expressions More interesting examples are created by, for example, comparing two things Complex Boolean expressions are built up out of simple ones using Boolean operations 5

6 Relational Operators Relational Operators are used to create simple Boolean expressions They include: =equal? not equal? <less than? <=less than or equal? >greater than? >= greater than or equal? 6

7 Examples of True/False Expressions Dim varA, varB, varC As Double varA = 1 varB = 2 varC = 2 varA < varB is true varA <= varB is true varC < varB is false varC <= varB is true varB = varC is true‘note dual role of the symbol = 7

8 Logical Operators More complex Boolean expressions are built using Boolean operators. The most common Boolean operators are And, Or, and Not Other Boolean operators, such as exclusive or, are available if needed Not reverses the truth of its argument: Not (True) = False and vice-versa 8

9 And And is true if and only if both its arguments are true This is called a truth table. The letters X and Y represent Boolean expressions. We have to look at all possible combinations of T and F for X and Y XYX And Y TTT TFF FTF FFF 9

10 Or Or is true if and only if at least one of its arguments is true. This differs from the usual meaning of “or” (this or is more like and/or) XYX Or Y TTT TFT FTT FFF 10

11 More Complex Expressions These can be worked out using truth tables Consider (Not X) Or (X and Y) XYNot XX And YNot X Or (X And Y) TTFTT TFFFF FTTFT FFTFT 11

12 Another Example Show Not (A And B) is logically equivalent to (Not A) Or (Not B) ABA And BNot (A And B) TTTF TFFT FTFT FFFT ABNot ANot B(Not A) Or (Not B) TTFFF TFFTT FTTFT FFTTT 12

13 A Couple of Pointers Some math/relational expressions do not translate directly. For example, A < B <= C would be written as (A < B) And (B <= C) Be careful with And and Or. Normally both parts of an And expression are evaluated even if the first one is false, and both parts of an Or expression are evaluated even if the first one is true. Standard example: (X = 0) Or (Y/X <5) (could cause runtime error if X = 0) 13

14 Other Uses for Logic Logical expressions are a part of database queries. If you will be using databases and writing queries, understanding how to form logical expressions will be very useful. You can use logic to form advanced queries in Google We’ll limit ourselves to three operators: And, Or, and Not. They are sufficient to write any logical expression, though not always in the simplest way. If you want to use others, feel free, but make sure you understand their truth tables. 14


Download ppt "CS 106 Logic and Conditionals (Boolean Expressions)"

Similar presentations


Ads by Google