Bool operators and, or, not

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Logic Gates.
If Statements & Relational Operators Programming.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4.
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.
Computer Science 101 Boolean Algebra. What’s next? A new type of algebra – Helps us A new type of algebra – Helps us With logical reasoningWith logical.
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
XOR and XNOR Logic Gates. XOR Function Output Y is TRUE if input A OR input B are TRUE Exclusively, else it is FALSE. Logic Symbol  Description  Truth.
Copyright © Curt Hill Truth Tables A way to show Boolean Operations.
Decision Structures and Boolean Logic
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
Decisions in Python Bools and simple if statements.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Thinking Mathematically
Relational and Boolean Operators CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
Circuits & Boolean Expressions. A ABC BC ABC C B A Example # 1: Boolean Expression: Develop a Boolean expression from a circuit.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
CS305j Introduction to Computing More Conditional Execution 1 Topic 13 More Conditional Execution " Great dancers are not great because of their technique;
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Rational Expressions relational operators logical operators order of precedence.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Control Structures I Chapter 3
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
The Ohio State University
Propositional Logic (a.k.a. Sentential Logic)
CPS120 Introduction to Computer Science
Unit 1 Logical operators.
Rational Expressions. relational operators. logical operators
More important details More fun Part 3
A mechanism for deciding whether an action should be taken
Logic Gates.
Data Types, Identifiers, and Expressions
Topics The if Statement The if-else Statement Comparing Strings
4-1 LOGIC OPERATIONS In Chapter 3 we discussed the fact that data inside a computer is stored as patterns of bits. Logic operations refer to those operations.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Boolean logic Taken from notes by Dr. Neil Moore
Introduction To Robot Decision Making
Topics The if Statement The if-else Statement Comparing Strings
Logical Operators & Truth Tables.
Selection CIS 40 – Introduction to Programming in Python
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Bools and simple if statements
Relational Operators Operator Meaning < Less than > Greater than
Types, Truth, and Expressions (Part 2)
DESICION TABLE Decision tables are precise and compact way to model complicated logic. Decision table is useful when input and output data can be.
Computer Science 210 Computer Organization
Boolean Logic In today’s lesson we will look at:
4 Categorical Propositions
Logic Gates.
De Morgan’s laws presentation
GCSE Computer Science – Logic Gates & Boolean Expressions
Introduction To Robot Decision Making
Expressions.
Boolean Expressions to Make Comparisons
Boolean logic Taken from notes by Dr. Neil Moore
ECS15 boolean.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
Herbert G. Mayer, PSU CS Status 7/19/2015
Truth tables Mrs. Palmer.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Relational and Logical Operators
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Relational and Logical Operators
Lecture 9: Implementing Complex Logic
Types, Truth, and Expressions
Presentation transcript:

Bool operators and, or, not Decisions Bool operators and, or, not

Boolean operators In Python, not, and and or are Boolean or logical operators Note that is NOT the same as the relational operators Boolean operators operate ONLY on Bool values and produce Bool values and and or are used to combine two Bools into one not is a unary operator, it reverses a Bool to the opposite value Their priority is lower than the relational operators Their individual priorities are not followed by and followed by or. The truth tables on the next slide show how they operate, their semantics.

Truth Tables P Q P and Q P or Q T F P not P T F

Examples Given that x is 5, y is 9.2, z is 0 x + 5 > y * 3 and y > z works out as x + 5 > 27.6 and y > z (* done first) 10 > 27.6 and y > z (+ done next) False and y > z (relationals done left to right) False and True (second relational done) False (result of and)

Cautions In most languages the expression 5 < y < 10 does not mean what you would think. In Python it does – it is True if y is between 5 and 10. It is the same as saying 5 < y and y < 10 The expression x == 5 or 6 does NOT mean what you would think. In fact it is considered always True in Python. Why? 6 after the or operator is considered a separate value – it is NOT compared to the x. The or operator needs a Bool value there, so it forces (“coerces”) the 6 value to be True (anything not zero is True). And from the truth table for or, you can see that anything or True is True! To have the interpreter see it correctly, you write it as x == 5 or x == 6

Always True or Always False You can easily write expressions which are always True or always False. This is an error. The interpreter will not tell you about them. Always True: x > 5 or x < 8 Always False: y > 10 and y < 0 Always True: y != 10 or y != 5