Boolean logic Taken from notes by Dr. Neil Moore

Slides:



Advertisements
Similar presentations
TRUTH TABLES Section 1.3.
Advertisements

Logic & Critical Reasoning
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Boolean Algebra Computer Science AND (today is Monday) AND (it is raining) (today is Monday) AND (it is not raining) (today is Friday) AND (it is.
Logic: Connectives AND OR NOT P Q (P ^ Q) T F P Q (P v Q) T F P ~P T F
1 Section 1.2 Propositional Equivalences. 2 Equivalent Propositions Have the same truth table Can be used interchangeably For example, exclusive or and.
Propositional Logic Lecture 2: Sep 9. Conditional Statement If p then q p is called the hypothesis; q is called the conclusion “If your GPA is 4.0, then.
CSE 311 Foundations of Computing I Autumn 2011 Lecture 2 More Propositional Logic Application: Circuits Propositional Equivalence.
Propositional Logic 7/16/ Propositional Logic A proposition is a statement that is either true or false. We give propositions names such as p, q,
Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence.
3.2 – Truth Tables and Equivalent Statements
Propositional Logic.
Intro to Discrete Structures
Discrete Maths 2. Propositional Logic Objective
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Copyright © Curt Hill Truth Tables A way to show Boolean Operations.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
Logical Form and Logical Equivalence Lecture 2 Section 1.1 Fri, Jan 19, 2007.
HAWKES LEARNING Students Count. Success Matters. Copyright © 2015 by Hawkes Learning/Quant Systems, Inc. All rights reserved. Section 3.2 Truth Tables.
Boolean Algebra. Logical Statements A proposition that may or may not be true:  Today is Monday  Today is Sunday  It is raining.
Boolean Logic. Boolean Operators (T/F) xyx AND y FFF FTF TFF TTT xyx OR y FFF FTT TFT TTT xNOT x FT TF.
Chapter 3: Introduction to Logic. Logic Main goal: use logic to analyze arguments (claims) to see if they are valid or invalid. This is useful for math.
Chapter 8 – Symbolic Logic Professor D’Ascoli. Symbolic Logic Because the appraisal of arguments is made difficult by the peculiarities of natural language,
Propositional Logic. Propositions Any statement that is either True (T) or False (F) is a proposition Propositional variables: a variable that can assume.
Warmup Answer the following True/False questions in your head: I have brown hair AND I am wearing glasses I am male OR I am wearing sneakers I am NOT male.
How do I show that two compound propositions are logically equivalent?
6.1 Logic Logic is not only the foundation of mathematics, but also is important in numerous fields including law, medicine, and science. Although the.
Propositional Logic ITCS 2175 (Rosen Section 1.1, 1.2)
Logical Form and Logical Equivalence Lecture 1 Section 1.1 Wed, Jan 12, 2005.
Logical Form and Logical Equivalence M Logical Form Example 1 If the syntax is faulty or execution results in division by zero, then the program.
1 Propositional Logic Introduction. 2 What is propositional logic? Propositional Logic is concerned with propositions and their interrelationships. 
Boolean Logic.
Simplifying Boolean Expressions. Boolean Operators (T/F) xyx AND y FFF FTF TFF TTT xyx OR y FFF FTT TFT TTT xyx XOR y FFF FTT TFT TTF xNOT x FT TF.
TRUTH TABLES. Introduction The truth value of a statement is the classification as true or false which denoted by T or F. A truth table is a listing of.
CEC 220 Digital Circuit Design Boolean Algebra I Wed, Sept 2 CEC 220 Digital Circuit Design Slide 1 of 13.
Statement Forms and Material Equivalence Kareem Khalifa Department of Philosophy Middlebury College.
Joan Ridgway. If a proposition is not indeterminate then it is either true (T) or false (F). True and False are complementary events. For two propositions,
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Outline Logic Propositional Logic Well formed formula Truth table
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Mathematics for Computing Lecture 2: Computer Logic and Truth Tables Dr Andrew Purkiss-Trew Cancer Research UK
TRUTH TABLES Edited from the original by: Mimi Opkins CECS 100 Fall 2011 Thanks for the ppt.
Logic and Truth Tables Winter 2012 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University.
CS 115 Lecture 9 Augmented assignment; Boolean logic; random numbers Taken from notes by Dr. Neil Moore.
Propositional and predicate logic
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Section 3.2: Truth Tables for Negation, Conjunction, and Disjunction
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CS 115 Lecture Augmented assignment; Boolean logic; random numbers
Bool operators and, or, not
Lesson 9: "if-else-if" and Conditional Logic
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Boolean logic Taken from notes by Dr. Neil Moore
Intro to Logic Logical statements evaluate to one of two things:
Selection CIS 40 – Introduction to Programming in Python
Conditions and Ifs BIS1523 – Lecture 8.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
CS 115 Lecture 7 Augmented assignment; Boolean logic; random numbers
Types, Truth, and Expressions (Part 2)
Boolean Expressions to Make Comparisons
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
ECE 352 Digital System Fundamentals
Selection Statements Chapter 3.
Types, Truth, and Expressions
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions
Presentation transcript:

Boolean logic Taken from notes by Dr. Neil Moore CS 115 Lecture Boolean logic Taken from notes by Dr. Neil Moore

Boolean logic and logical operators There are three logical operators that let us combine Boolean expressions. They have lower precedence than the relational operators (<, >, …) not A: True if A is False, False if A is True A is any Boolean expression: if not is_finished: do_more_work() A and B: True if both A and B are True in_range = size >= 0 and size <= 100 A or B: True if either A or B is True or Both! if snow_inches > 6 or temperature < 0: print(“Class is cancelled”)

Complex Boolean expressions not has the highest precedence (but still lower than relational) and has the next highest or has the lowest of the three So not A or B and C or D means (((not A) or (B and C)) or D) People often forget the order of and and or operators It’s not a bad idea to always use parentheses when they are both in an expression not A or (B and C) or D

Truth tables The truth table is a tool for making sense of complex Boolean expressions. A B A and B True False A B A or B True False A not A True False

Truth tables A table has one row for each possible combination of values of True and False if there is one input, two rows (T, F) two inputs, four rows (TT, TF, FT, FF) three inputs, eight rows (TTT, TTF, TFT, TFF, FTT, FTF, FFT, FFF) A table has one column for each boolean expression Inputs: Boolean variables or comparisons (relational expressions) Intermediate results: The Boolean value of the expression for each not, and, or. Output: the Boolean value of the whole expression

A more complicated example not (not A or not B) A B not A not B not A or not B result True False

A more complicated example not (not A or not B) A B not A not B not A or not B result True False

A more complicated example not (not A or not B) A B not A not B not A or not B result True False

A more complicated example not (not A or not B) A B not A not B not A or not B result True False

De Morgan’s laws Two Forms: not (not A or not B) = A and B not (not A and not B) = A or B These can be useful for rewriting expressions to simplify them Can make your code easier to understand and faster to execute Examples: not (x > 5 and x < 10) is the same as not(x > 5) or not (x < 10) which is the same as x <= 5 or x >=10 not (y == x or z != 5 and p == q) is the same as not(y == x) and not (z != 5) or not (p == q) which is the same as y != x and z == 5 or p != q The opposite of < is >=, that is, a < b is False if a >= b is True The opposite of > is <= (don’t forget the =)

Be careful! It is easy to accidentally write an expression that is always True or always False Tautolology (always True) and contradiction (always False) Examples: if size >= 10 or size < 50: print(“in range”) What happens when size is 100? 20 ? 2? (Hint: all of those values result in True!) The or operator is True if EITHER comparison is True; the two comparisons cannot both be False at the same time! So this is a tautology (always True) if size < 10 and size > 100: print(“out of range”) The comparisons cannot both be True at the same time! At least one condition will be False So the message will never print – a contradiction (always False)

Be careful! Don’t trust the English language! Make a truth table if you are not sure “I want to run this if size < 10 and if size > 100” In logic, that should be an or operator, not an and operator: “Run this if size < 10 or size > 100” “if x is equal to 4 or 5…” Wrong: if x == 4 or 5: Tests must be written out explicitly Should be: “if x is equal to 4 or x is equal to 5” if x == 4 or x == 5:

Coercing other types to bools Why did the last example if x == 4 or 5: run at all? What does Python see it as? or is a boolean operator – it works on bools and returns a bool There is a bool from the x == 4, but the 5 is by itself! (x == 5 is NOT implied there!) Python needs a bool on the right of the or operator – how does it make the 5 a bool?? It forces (coerces) the 5 to be a bool according to the rules For numbers, any value but 0 is turned into True, 0 is False For strings, any string except the empty string is True, “” is False For lists, any list except the empty list is True, the empty list [] is False ALL graphics objects are True! So the expression above “x == 4 or 5” is ALWAYS TRUE because the 5 is coerced to True, and “anything or True” is always True. Tautology!

Coercing other types to bools This is NOT something you should rely on in your code – it is difficult for someone to read and understand, and it is very prone to bugs if you are not careful. Example: What does this condition mean? if not name: where name is a string not is a bool operator, so it must have a bool value to operate on name is a string, not a bool, so its value is coerced to a bool If the name is an empty string, then it’s coerced to False, so not name is the same as not False, which is True That condition (not name) is equivalent to if name == “”: But if name == “”: is a lot easier to understand (and not get backwards!)

How Indentation Really Matters! Are these two if structures the same? Do they give the same output all the time? if a > 12: if b < 50: print(“red”) else: print(“blue”) if a > 12: if b < 50: print(“red”) else: print(“blue”)