Copyright © Texas Education Agency, 2013 1
Computer Programming
Boolean Logic
What is Boolean Logic?
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 2
Boolean logic is a system for determining the truth of a statement (or expression) based on whether certain variables are true or false.
For example: In the statement, “I will go for a walk if it is sunny,” the decision is based on whether it is true that it is ‘sunny.’
Copyright © Texas Education Agency, 2013 2
Historical Note
George Boole (1815-1864) was an English mathematician who devised a system of symbols and equations to represent logical decisions.
Boolean Algebra was not widely used until the invention of computers. Boolean logic expressions are used to design electronic circuits and to represent decision-making in software
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 3Copyright © Texas Education Agency, 2013 3
How is Boolean Logic used?
A combination of conditions, variables, and operators are used to determine if an expression is True or False.
It can also be used to determine which elements can be members of a given set.
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 4Copyright © Texas Education Agency, 2013 4
How are these categories different?
Brown-eyed AND Male
Brown-eyed OR Male
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 5Copyright © Texas Education Agency, 2013 5
Who would be in these groups?
NOT male AND wearing jeans
NOT <16
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 6Copyright © Texas Education Agency, 2013 6
Like Mathematics, Boolean logic has operators
Mathematic Operators + - x / =
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 7Copyright © Texas Education Agency, 2013 7
Boolean Operators
&& ( AND )
|| ( OR )
! ( NOT )
Cheese || Pepperoni && !Anchovies
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 8Copyright © Texas Education Agency, 2013 8
Like Math, Boolean logic has an order of operations
Mathematic Order of Operations is PEMDAS Parentheses first Exponents next Multiplication and Division Addition and Subtraction
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 9Copyright © Texas Education Agency, 2013 9
Order of Boolean Operations
Parentheses are not operators, but indicate grouping, consider these first.
NOT (!) must be considered next.
AND ( && ) is considered next.
OR ( || ) is considered last.
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 10Copyright © Texas Education Agency, 2013 10
!Tall && Blonde || Rich
1. Consider the NOT first. !Tall is short.
2. Consider the AND next. !Tall and Blonde, in other words, Short and Blonde.
3. Consider the OR next.
This person can be either Short and Blonde, or they can be Rich. They can also be Short, Blonde, and Rich.
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 11Copyright © Texas Education Agency, 2013 11
Order of Operations
Parentheses first, then NOT (!) , then && , then ||
!Tall && Blonde || Rich
!(Tall && Blonde) || Rich
!Tall && !Blonde || Rich
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 12Copyright © Texas Education Agency, 2013 12
Venn Diagrams
Venn Diagrams can be used to visualize Boolean expressions.
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 13Copyright © Texas Education Agency, 2013 13
Venn Diagram
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 14
A B
A && B
Copyright © Texas Education Agency, 2013 14
Where is !A
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 15
A B
Copyright © Texas Education Agency, 2013 15
Where is A || B ?
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 16
A B
Copyright © Texas Education Agency, 2013 16
Truth Tables
Truth tables are used to evaluate possible combinations of variables and operators.
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 17Copyright © Texas Education Agency, 2013 17
A || B (A OR B)
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 18
A B A || B
True True True
True False True
False True True
False False False
Copyright © Texas Education Agency, 2013 18
A && B (A AND B)
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 19
A B A && B
True True True
True False False
False True False
False False False
Copyright © Texas Education Agency, 2013 19
!A (NOT A)
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 20
A !A
True False
False True
Copyright © Texas Education Agency, 2013 20
!A || B (NOT A OR B)
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 21
A B !A || B
True True True
True False False
False True True
False False True
Copyright © Texas Education Agency, 2013 21
Check for Understanding
Determining the results of a Boolean expression is known as evaluating it.
All Boolean expressions evaluate to either True or False
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 22Copyright © Texas Education Agency, 2013 22
Evaluate: What would be printed, A or B?
boolean blonde = false;
if ( blonde)
System.out.println(“A”);
if ( !blonde)
System.out.println(“B”);
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 23Copyright © Texas Education Agency, 2013 23
Evaluate: What would be printed, A or B?
boolean tall = false;
boolean male = true;
if ( male && tall)
System.out.println(“A”);
if ( male || tall)
System.out.println(“B”);
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 24Copyright © Texas Education Agency, 2013 24
Evaluate: What would be printed, A or B?
boolean x = false;
boolean y = true;
if ( x && y)
System.out.println(“A”);
if ( !x && y)
System.out.println(“B”);
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 25Copyright © Texas Education Agency, 2013 25
Shade in A || !B
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 26
A B
Copyright © Texas Education Agency, 2013 26
Shade in A && !B
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 27
A B
Copyright © Texas Education Agency, 2013 27
Shade in !A && !B
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 28
A B
Copyright © Texas Education Agency, 2013 28
Will it print?
boolean x = true;
boolean y = false;
if ( x && !y)
System.out.print(“A”);
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 29Copyright © Texas Education Agency, 2013 29
Will it print?
boolean x = true;
boolean y = false;
if (! x || y)
System.out.print(“A”);
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 30Copyright © Texas Education Agency, 2013 30
With what you know…
Can you evaluate a boolean expression?
© UNT in partnership with TEA IT: [Computer Programming] - [Boolean Logic] 31Copyright © Texas Education Agency, 2013 31