CPS120 Introduction to Computer Science

Download Report

Transcript CPS120 Introduction to Computer Science

CPS120 Introduction to
Computer Science
Boolean Logic
Decision Making In Computers
• A circuit quite simply allows one out of two
choices to be made depending on its inputs
• When decisions are made in a computer program,
they are simply the result of a computation in
which the final result is either TRUE or FALSE
• The value zero (0) is considered to be FALSE.
Any positive or negative value is considered to be
TRUE (usually represented by 1)
Using Logical Operators
• When complex decisions must be coded into an
algorithm, it may be necessary to "chain together"
a few relational expressions (that use relational
operators)
• This is done with logical operators (also called
Boolean operators.)
&&
||
!
is the logical AND operator
is the logical OR operator
is the logical NOT operator
Boolean Logic
• The term Boolean or simply BOOL is
named for a 19th century mathematician
named George Boole.
– Boole is recognized for applying concepts of
algebra to human logic.
– Boole moved concepts of human thought
process away from the metaphysical or abstract
logic of Aristotle to more tangible mathematical
principles.
I Know This Concept
• If you have ever taken a True or False test,
you have used Boolean logic.
– In the Boolean system an object can exist in
only one of two states, there is no third choice
• This is a central concept in programming.
What are the different Boolean
Operators?
• Depending on code being used the operators
come with different names and use.
– Some languages support more Boolean
Operators, some less.
– There are some basic standard operators.
• The result of any Boolean operation should
be a TRUE or FALSE
AND
• Two or more items must agree (be evaluated to the
same result) for the expression to be true.
–
–
–
–
–
–
1 AND 1 would evaluate to TRUE
0 AND 1 would evaluate to FALSE
1 AND 0 would evaluate to FALSE
0 AND 0 would evaluate to FALSE
1 AND 1 AND 1 would evaluate to TRUE
1 AND 1 AND 0 would evaluate to FALSE
OR
• One, both or more items must agree. If both
inputs are FALSE, the result it FALSE.
– 1 OR 1 would evaluate to TRUE
– 0 OR 1 would evaluate to TRUE
– 0 OR 0 would evaluate to FALSE
NOT
• Reverses the input. If TRUE is input, the
result id FALSE; if FALSE is input, the
result is TRUE.
– 1 would evaluate to FALSE
– 0 would evaluate to TRUE
XOR (eXclusive OR)
• Only one input may be TRUE, if both are TRUE
the entire result id FALSE.
– 1 XOR 1 would evaluate to FALSE
– 1 XOR 0 would evaluate to TRUE
– 0 XOR 1 would evaluate to TRUE
– 0 XOR 0 would evaluate to FALSE
NAND (Not AND)
• This basically negates AND:
– 1 NAND 1 would evaluate to FALSE
– 1 NAND 0 would evaluate to TRUE
– 0 NAND 0 would evaluate to TRUE
– 0 NAND 1 would evaluate to TRUE
Truth Tables
• Use this truth table to determine the results of the
logical operators. In this table, 1 represents TRUE
and 0 represents FALSE.
• Note that the ! symbol (the logical NOT operator)
changes a TRUE to a FALSE.
AND
A
0
0
1
1
B
0
1
0
1
A && B
0
0
0
1
OR
A
0
0
1
1
B
0
1
0
1
A || B
0
1
1
1
NOT
A
0
1
!A
1
0