17
An Introduction to Programming Using Alice Boolean Logic

Ch 5 boolean logic

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Ch 5 boolean logic

An Introduction to Programming Using Alice

Boolean Logic

Page 2: Ch 5 boolean logic

An Introduction to Programming Using Alice

George Boole

In1854, George Boole published “An investigation into the Laws of Thought, on which are founded the Mathematical Theories of Logic and Probabilities.”

Boole outlined a system of logic and a corresponding algebraic language dealing with true and false values.

100th Anniversary Edition

Page 3: Ch 5 boolean logic

An Introduction to Programming Using Alice

Boolean Logic

Boolean logic is a form of mathematics in which the only values used are true and false.

Boolean logic is the basis of all modern computing.

There are three basic operations in Boolean logic – AND, OR, and NOT.

100th Anniversary Edition

Page 4: Ch 5 boolean logic

An Introduction to Programming Using Alice

The AND Operation

The AND operation is a binary operation, meaning that it needs two operands.

c = a AND b

Both a and b must be true for the result to be true.

ANDb

T F

aT T F

F F F

Page 5: Ch 5 boolean logic

An Introduction to Programming Using Alice

The OR Operation

The OR operation is also a binary operation with two operands.

c = a OR b

If either a OR b is true, then the result is true.

ORb

T F

aT T T

F T F

Page 6: Ch 5 boolean logic

An Introduction to Programming Using Alice

The NOT Operation

The NOT operation is a unary operation with only one operand.

c = NOT (a)

It simply reverses the true or false value of the operand.

NOT

aT F

F T

Page 7: Ch 5 boolean logic

An Introduction to Programming Using Alice

Logical Conditions

Logical comparisons that are either true or false are most often used as the basis for the true and false values in Boolean logic.

They are often used for simple conditions in branching and looping instructions.

If (hours > 40)

pay overtime

If (age < 12)

stay in the back seat

While (count 10)

print count increment count

Page 8: Ch 5 boolean logic

An Introduction to Programming Using Alice

Logical Comparison Operators

Condition In Math In Programming

A equals B A = B A = B or A == B

A is not equal to B A B A<> B or A != B

A is less than B A < B A < B

A is greater than B A > B A > B

A is less than or equal to B A B A <= B

A is greater than or equal to B A B A >= B

Six different comparison operators are used in mathematics and computer programming.

Page 9: Ch 5 boolean logic

An Introduction to Programming Using Alice

Compound Conditions

Compound Boolean

conditions can be created

using the Boolean AND,

OR and NOT operations

in branching and looping

instructions.

If ( (hours > 40) AND (type = hourly) )

pay overtime

If ( (age < 12) OR (height < 42 in.) )

stay in the back seat

While ( (count <= 10) AND NOT (status = away) )

print name.count increment count

Page 10: Ch 5 boolean logic

An Introduction to Programming with Alice

Boolean Logic Boolean Logic in Alice in Alice

Page 11: Ch 5 boolean logic

Boolean FunctionsBoolean Functions

Functions in Alice that return Functions in Alice that return true or false values are true or false values are called Boolean functions.called Boolean functions.

Alice has many such built-in Alice has many such built-in functions, such as these two functions, such as these two groups of basic Boolean groups of basic Boolean logic functions and logic functions and comparison functions.comparison functions.

Page 12: Ch 5 boolean logic

They can be found on the world’s functions tab

Page 13: Ch 5 boolean logic

They are used inside branching and looping instruction tiles

Page 14: Ch 5 boolean logic

Boolean FunctionsBoolean Functions

The Boolean comparison The Boolean comparison functions can be used to functions can be used to create simple true or false create simple true or false conditions.conditions.

Page 15: Ch 5 boolean logic

Boolean FunctionsBoolean Functions

The Boolean logic functions The Boolean logic functions can be used to create can be used to create compound conditions.compound conditions.

Page 16: Ch 5 boolean logic

Boolean FunctionsBoolean Functions

Alice has many other Alice has many other world-level and object-world-level and object-level fuctions that return level fuctions that return true or false values, such true or false values, such as the Boolean proximity as the Boolean proximity functions shown here on functions shown here on aliceLiddel’s functions tab aliceLiddel’s functions tab in the details area.in the details area.

Page 17: Ch 5 boolean logic

Boolean FunctionsBoolean Functions

They can be used anywhere that a Boolean They can be used anywhere that a Boolean value is needed, such as in this If/Else value is needed, such as in this If/Else command.command.