New Mexico Computer Science For All Booleans and Logic Maureen Psaila-Dombrowski

Preview:

Citation preview

New Mexico Computer Science For All

Booleans and Logic

Maureen Psaila-Dombrowski

Boolean•Boolean - EASY

Can have one of two values ▫true or false▫on or off▫1 or 0

•Boolean Expressions Evaluate to be true or false

•Boolean Operators Operators that are used with Booleans

•Boolean Math/Algebra or Boolean Logic Uses Booleans and Boolean operators Used in computers

Boolean Expressions•A simple use of Boolean concept

•Expressions that evaluate to True or False

•They use comparison operators (=, <, >, <= or >=)

Compare two sides – numbers, variables, expressions

Ask a specific question – Is the expression TRUE or FALSE

?

•You have used them in math before. 24 + 13 = ? Addition 3724 + 13 = 37 ? Boolean true24 + 13 = 40 ? Boolean false

Boolean Expressions (continued)

Expression(Comparison

Operator) Question Asked

L = R The left equals the right, True or False?

3 + 5 = 8 3 + 5 equals 8, True or False? TRUE

L < R The left is less than the right, True or False?

16/4 < 3 16/4 is less than 3, True or False? FALSE

L > R The left is greater than the right, True or False?

50*100 > 50050*100 is greater than 500, True or False? TRUE

Boolean Operators

• Three basic ones: AND, OR , NOT Most other operators are based on these

• Compares Boolean values or expressions or change them

• Evaluates to a Boolean (True or False, On or Off, 1 or 0)

• Rules for each operator are different

BooleanValue

OrExpression

(True or False)

BooleanOperator

BooleanValue

OrExpression

(True or False)

Boolean Value

(True or False)

Boolean Logic•Also know as Boolean Algebra or Boolean

Math

•What is it?▫Only two values.▫The Math of comparing and manipulating

Booleans

•So what?▫Most important principle of modern computers.

•Developed in the 1850s by George Boole▫A founder of computer science

• Boolean Logic Expression: A AND B• For A AND B to be true BOTH A AND B

must be true (Both Sides Must Be True)• AND Truth Table:

Boolean Logic (continued)

ANDA

Boolean Expression

Boolean Operator

BBoolean

Expression

Evaluates to

TRUE AND TRUE TRUE

TRUE AND FALSE FALSE

FALSE AND TRUE FALSE

FALSE AND FALSE FALSE

Boolean Logic

Lets look at an example: Marbles

Boolean Logic

Lets look at an example: Marbles

Blue? = falseRed? = true

Marble? = true

Boolean LogicWe can group the marbles:• Is there a set of Marble? = true

Marble? = true

Boolean Logic

Blue? = true

Red? = true

We can also group the marbles:• The marble is Blue? = true ?• The marble is Red? = true ?

Boolean Logic

Blue? = true

Red? = true

Is there a set:(Marble? = true) AND (Blue? = true) is true

Is there a set: (Blue? = true) AND (Red? = true) is true?

Blue? = true Red? = true

Boolean Logic

Find the set with (Blue? = true) AND (Red? = true)Find the set with (Blue? = true) AND (Green? = true) Find the set with (Purple? = true) AND (Green? = true)

• For A OR B to be true EITHER A OR B can be true (Only One Side Needs to be True)

• OR Truth Table:

Boolean Logic (continued)

ORA

Boolean Expression

Boolean Operator

BBoolean

Expression

Evaluates to

TRUE OR TRUE TRUE

TRUE OR FALSE TRUE

FALSE OR TRUE TRUE

FALSE OR FALSE FALSE

Blue? = true

Red? = true

Find the set with (Blue? = true) OR (Red? = true)

Boolean Logic (continued)

Blue? = true Red? = true

Find the set with (Blue? = true) OR (Red? = true)Find the set with (Blue? = true) OR (Green? = true)Find the set with (Purple? = true) OR (Green? = true)

Boolean Logic (continued)

Boolean Logic (continued)

NOTBoolean

Operator Boolean

ExpressionEvaluates

to

NOT TRUE FALSE

NOT FALSE TRUE

NOT is really easy – it just gives you the opposite value

Blue? = true

Red? = true

Find the set with (NOT Blue? = true)Find the set with (NOT Red? = true)Find the set with (NOT Purple? = true)

Boolean Logic (continued)

Blue? = true Red

Find the set with (NOT Red? = true)Find the set with (NOT Blue? = true)Find the set with (NOT Purple? = true)

Boolean Logic (continued)

•Order of Operation

1. Parenthesis2. NOT3. AND 4. OR

•Examples:

NOT true AND true false AND true FALSE

NOT ( false AND true ) NOT false TRUE

true OR false AND true true OR false TRUE

( true OR false ) AND true true AND true TRUE

Boolean Logic (continued)

Booleans in Computer Science

•Search Engines (Average User)▫Use Boolean Logic to limit: AND, OR, NOT▫Boolean Order of Operations holds

•Conditionals (Programmer)▫Boolean Expressions and Boolean Logic▫Used in Conditionals

Booleans in Computer Science•Digital Gates (Boolean Gates) in

Chips/Circuits (Hardware Engineer)

▫Fundamental building block of a computer▫Uses Booleans

True and False 1’s and 0’s▫Boolean Logic

To figure out if gate is open/closed

Summary •Booleans can have only two values •Boolean expressions

Use comparison operators (=, <, >) Evaluate to TRUE or FALSE

•Boolean Operators Three Basic Operators: AND, OR, NOT Compare Boolean values/expressions or

change them Evaluate to TRUE or FALSE

•Boolean Logic• Use Boolean Operators: AND, OR, NOT• The math of comparing and manipulating

Booleans.• Order of Operation: ( ), NOT, AND, OR

Recommended