15
1 CS 106 Computing Fundamentals II Chapter 32 “Boolean Expressions” Herbert G. Mayer, PSU CS Herbert G. Mayer, PSU CS Status 7/14/2013 Status 7/14/2013 Initial content copied verbatim from Initial content copied verbatim from CS 106 material developed by CS 106 material developed by CS professors: Cynthia Brown & Robert Martin CS professors: Cynthia Brown & Robert Martin

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

Embed Size (px)

Citation preview

Page 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

1

CS 106Computing Fundamentals II

Chapter 32“Boolean Expressions”

Herbert G. Mayer, PSU CSHerbert G. Mayer, PSU CSStatus 7/14/2013Status 7/14/2013

Initial content copied verbatim fromInitial content copied verbatim fromCS 106 material developed byCS 106 material developed by

CS professors: Cynthia Brown & Robert MartinCS professors: Cynthia Brown & Robert Martin

Page 2: 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

2

Syllabus Enlarging Our RepertoireEnlarging Our Repertoire Tests And ChoicesTests And Choices ExpressionsExpressions Boolean Data TypeBoolean Data Type Relational OperatorsRelational Operators Logical OperatorsLogical Operators AndAnd OrOr

Page 3: 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

3

Enlarging our Repertoire

• You know how to write VB statements that You know how to write VB statements that change the values of variables, including change the values of variables, including properties of objectsproperties of objects

• Our next type of statement is a Our next type of statement is a conditional: it allows us to change what conditional: it allows us to change what the program does based on the outcome of a the program does based on the outcome of a test, i.e. based on conditionstest, i.e. based on conditions

3

Page 4: 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

4

Tests and Choices

• To allow a program to make choices based To allow a program to make choices based on conditions we need to introduce a new on conditions we need to introduce a new programming constructprogramming construct

• But first we look at how we program the But first we look at how we program the tests that will determine which branch our tests that will determine which branch our program takesprogram takes

4

Page 5: 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

5

Expressions• A VBA expression computes a valueA VBA expression computes a value

• Arithmetic expressions include simple numbers Arithmetic expressions include simple numbers and expressions built with arithmetic and expressions built with arithmetic operators, with ( and ), and functions, such operators, with ( and ), and functions, such as as 44 varA + 12

• String expressions include simple strings and String expressions include simple strings and expressions built with the string operator & expressions built with the string operator & and possibly string functionsand possibly string functions “Hello, I’m a string.”

“Your cost is “ & FormatCurrency(iceCreamTotal)

5

Page 6: 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

6

Boolean Data Type

• A Boolean variable has one of two possible A Boolean variable has one of two possible values: values: True True and and FalseFalse

• A Boolean expression is an expression that A Boolean expression is an expression that can evaluate to can evaluate to TrueTrue or or FalseFalse

• Constants Constants True True and and FalseFalse are the simplest are the simplest Boolean expressionsBoolean expressions

• More interesting examples are created by, More interesting examples are created by, for example, comparing two thingsfor example, comparing two things

• Complex Boolean expressions are built up Complex Boolean expressions are built up out of simple ones using Boolean operationsout of simple ones using Boolean operations

6

Page 7: 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

7

Relational Operators

• Relational Operators are used to create Relational Operators are used to create simple Boolean expressionssimple Boolean expressions

• They include:They include:= equal?

< > not equal?

< less than?

<= less than or equal?

> greater than?

>= greater than or equal?

7

Page 8: 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

8

True - False Expressions

DimDim varA, varB, varC varA, varB, varC As DoubleAs Double

varA = 1varA = 1 ‘ .000 etc. is implied‘ .000 etc. is implied

varB = 2varB = 2

varC = 2varC = 2

varA < varB varA < varB ‘is true‘is true

varA <= varB varA <= varB ‘is true‘is true

varC < varB varC < varB ‘is false‘is false

varC <= varB varC <= varB ‘is true‘is true

varB = varC varB = varC ‘is true‘is true‘note dual role of ‘note dual role of = operator = operator

8

Page 9: 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

9

Logical Operators

• More complex Boolean expressions are built More complex Boolean expressions are built using Boolean operators. using Boolean operators.

• The most common Boolean operators are The most common Boolean operators are AndAnd, , OrOr, and , and NotNot

• Another Boolean operator is the exclusive Another Boolean operator is the exclusive or operation, or operation, XorXor

• NotNot reverses the truth of its argument: reverses the truth of its argument:

• Not (Not (TrueTrue) = ) = FalseFalse and vice-versa and vice-versa

9

Page 10: 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

10

And• The result of a boolean AND operation is The result of a boolean AND operation is

true if and only if both of its arguments true if and only if both of its arguments are trueare true

• This is called a truth table. The letters This is called a truth table. The letters X and Y represent Boolean expressions. We X and Y represent Boolean expressions. We have to look at all possible combinations have to look at all possible combinations of T and F for X and Yof T and F for X and Y

X Y X And Y

T T T

T F F

F T F

F F F

10

Page 11: 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

11

Or

Or yields true if at least one of its Or yields true if at least one of its arguments is truearguments is true

X Y X Or Y

T T T

T F T

F T T

F F F

11

Page 12: 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

12

More Complex Expressions

These can be worked out using truth tablesThese can be worked out using truth tables

Consider (Not X) Or (X and Y)Consider (Not X) Or (X and Y)

X Y Not X X And Y Not X Or (X And Y)

T T F T T

T F F F F

F T T F T

F F T F T

12

Page 13: 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

13

Another Example

Show Not (A And B) is logically equivalent to (Not A) Or (Not B)A B A And B Not (A And B)

T T T F

T F F T

F T F T

F F F T

A B Not A Not B (Not A) Or (Not B)

T T F F F

T F F T T

F T T F T

F F T T T

13

Page 14: 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

14

Some Pointers

• Some common relational expressions do not Some common relational expressions do not translate directly into VBA. For example, translate directly into VBA. For example, A < B <= C would be written as (A < B) A < B <= C would be written as (A < B) And And (B <= C)(B <= C)

• Be careful with Be careful with AndAnd and and OrOr. Both operands . Both operands of an of an AndAnd expression are evaluated even if expression are evaluated even if the first one is false, and both parts of the first one is false, and both parts of an an OrOr expression are evaluated even if the expression are evaluated even if the first one is truefirst one is true• example: ( X = 0 ) Or ( Y / X < 5 )

(could cause runtime error if X = 0)(could cause runtime error if X = 0)

14

Page 15: 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

15

Other Uses for Logic

• Logical expressions are a part of database Logical expressions are a part of database queries. If you will be using databases queries. If you will be using databases and writing queries, understanding how to and writing queries, understanding how to form logical expressions will be very form logical expressions will be very usefuluseful

• You can use logic to form advanced queries You can use logic to form advanced queries in Googlein Google

• We’ll limit ourselves to three operators: We’ll limit ourselves to three operators: AndAnd, , OrOr, and , and NotNot

15