Download presentation
Presentation is loading. Please wait.
Published byCharles Randall Modified over 9 years ago
1
1 CS 106 Computing Fundamentals II Chapter 32 “Boolean Expressions” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed by CS professors: Cynthia Brown & Robert Martin
2
2 Syllabus Enlarging Our Repertoire Enlarging Our Repertoire Tests And Choices Tests And Choices Expressions Expressions Boolean Data Type Boolean Data Type Relational Operators Relational Operators Logical Operators Logical Operators And And Or Or
3
3 Enlarging our Repertoire You know how to write VB statements that change the values of variables, including properties of objectsYou 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, i.e. based on conditionsOur next type of statement is a conditional: it allows us to change what the program does based on the outcome of a test, i.e. based on conditions 3
4
4 Tests and Choices To allow a program to make choices based on conditions we need to introduce a new programming constructTo 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 takesBut first we look at how we program the tests that will determine which branch our program takes 4
5
5 Expressions A VBA expression computes a valueA VBA expression computes a value Arithmetic expressions include simple numbers and expressions built with arithmetic operators, with ( and ), and functions, such asArithmetic expressions include simple numbers and expressions built with arithmetic operators, with ( and ), and functions, such as 44 varA + 12 String expressions include simple strings and expressions built with the string operator & and possibly string functionsString 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) 5
6
6 Boolean Data Type A Boolean variable has one of two possible values: True and FalseA Boolean variable has one of two possible values: True and False A Boolean expression is an expression that can evaluate to True or FalseA Boolean expression is an expression that can evaluate to True or False Constants True and False are the simplest Boolean expressionsConstants True and False are the simplest Boolean expressions More interesting examples are created by, for example, comparing two thingsMore interesting examples are created by, for example, comparing two things Complex Boolean expressions are built up out of simple ones using Boolean operationsComplex Boolean expressions are built up out of simple ones using Boolean operations 6
7
7 Relational Operators Relational Operators are used to create simple Boolean expressionsRelational Operators are used to create simple Boolean expressions They include:They include: =equal? not equal? <less than? <=less than or equal? >greater than? >= greater than or equal? 7
8
8 True - False Expressions Dim varA, varB, varC As Double varA = 1‘.000 etc. is implied 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 = operator 8
9
9 Logical Operators More complex Boolean expressions are built using Boolean operators.More complex Boolean expressions are built using Boolean operators. The most common Boolean operators are And, Or, and NotThe most common Boolean operators are And, Or, and Not Another Boolean operator is the exclusive or operation, XorAnother Boolean operator is the exclusive or operation, Xor Not reverses the truth of its argument:Not reverses the truth of its argument: Not (True) = False and vice-versaNot (True) = False and vice-versa 9
10
10 And The result of a boolean AND operation is true if and only if both of its arguments are trueThe result of a boolean AND operation is true if and only if both of 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 YThis 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 10
11
11 Or Or yields true if at least one of its arguments is true XYX Or Y TTT TFT FTT FFF 11
12
12 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 12
13
13 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 13
14
14 Some Pointers Some common relational expressions do not translate directly into VBA. For example, A < B <= C would be written as (A < B) And (B <= C)Some common relational expressions do not translate directly into VBA. For example, A < B <= C would be written as (A < B) And (B <= C) Be careful with And and Or. Both operands 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 trueBe careful with And and Or. Both operands 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 example: ( X = 0 ) Or ( Y / X < 5 ) (could cause runtime error if X = 0) 14
15
15 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 usefulLogical 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 GoogleYou can use logic to form advanced queries in Google We’ll limit ourselves to three operators: And, Or, and NotWe’ll limit ourselves to three operators: And, Or, and Not 15
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.