Logical operators 20/09/2018. The unit pulse. Background

Similar documents
Console input 26/09/2018. Background. Background

The call stack and recursion and parameters revisited

Insertion sort 20/11/2018. The idea. Example

Linked Lists 28/11/2018. Nodes with member functions. The need for a linked list class

Templates 12/11/2018. Redundancy. Redundancy

The structured programming theorem

Anatomy of a program 06/09/2018. Pre-processor directives. In this presentation, we will: Define the components of a program

Pushing at the back 28/11/2018. Problem. Our linked list class

Pointer arithmetic 20/11/2018. Pointer arithmetic. Pointer arithmetic

Selection sort 20/11/2018. The idea. Example

Polymorphism 02/12/2018. Member functions. Member functions

Main memory 05/10/2018. Main memory. Main memory

Dynamic memory allocation

Member functions 21/11/2018. Accessing member variables. Accessing member variables

Integer primitive data types

Throwing exceptions 02/12/2018. Throwing objects. Exceptions

Binary and hexadecimal numbers

Strings 20/11/2018. a.k.a. character arrays. Strings. Strings

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

3. Logical Values. Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation

EXPRESSIONS AND ASSIGNMENT CITS1001

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

Information Science 1

Boolean algebra. June 17, Howard Huang 1

4. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

Flow Control. So Far: Writing simple statements that get executed one after another.

BOOLEAN ALGEBRA AND CIRCUITS

COMP combinational logic 1 Jan. 18, 2016

Switching Circuits and Logic Design Prof. Indranil Sengupta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

Chapter 3: Operators, Expressions and Type Conversion

Circuit analysis summary

Unit 3. Operators. School of Science and Technology INTRODUCTION

Midterms Save the Dates!

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

Conditions, logical expressions, and selection. Introduction to control structures

L-System Fractal Generator: Language Reference Manual

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Programming Lecture 3

Software and Hardware

Outline. What Digit? => Number System. Decimal (base 10) Significant Digits. Lect 03 Number System, Gates, Boolean Algebra. CS221: Digital Design

Exercise 3 Logical Operators, Branching, Loops. Informatik I für Mathematiker und Physiker (HS 2015) Yeara Kozlov

Control Structures: The IF statement!

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Announcements. Homework 0 out! Coming soon: 1. remote access 2. programming instructions 3. lab 1 (graded!) this week

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

Program construction in C++ for Scientific Computing

Binary Representation. Jerry Cain CS 106AJ October 29, 2018 slides courtesy of Eric Roberts

Operators & Expressions

Mathematical Logic Prof. Arindama Singh Department of Mathematics Indian Institute of Technology, Madras. Lecture - 9 Normal Forms

Logic Design: Part 2

CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

LECTURE 3 C++ Basics Part 2

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

CS E1: Section 1: Counting in Binary

6.1 Combinational Circuits. George Boole ( ) Claude Shannon ( )

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Dr. Chuck Cartledge. 10 June 2015

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Review. EECS Components and Design Techniques for Digital Systems. Lec 05 Boolean Logic 9/4-04. Seq. Circuit Behavior. Outline.

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

(Refer Slide Time 6:48)

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Designing Computer Systems Boolean Algebra

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

CSE 142 Su 04 Computer Programming 1 - Java. Objects

(Refer Slide Time 3:31)

Introduction to Computer Architecture

Introduction to Boolean Algebra

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Lecture 4 CSE July 1992

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Boolean Algebra A B A AND B = A*B A B A OR B = A+B

Chapter 6 Primitive types

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Introduction to Boolean Algebra

CSCI Wednesdays: 1:25-2:15 Keller Thursdays: 4:00-4:50 Akerman 211

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lecture (04) Boolean Algebra and Logic Gates

Lecture (04) Boolean Algebra and Logic Gates By: Dr. Ahmed ElShafee

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Binary Trees Case-studies

Operators. Java operators are classified into three categories:

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CS242 COMPUTER PROGRAMMING

Arithmetic and Bitwise Operations on Binary Data

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Expression and Operator

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Week 2 Introduction to Computer and Algorithm (Part 2)

Module -7. Karnaugh Maps

Transcription:

ECE 150 Fundamentals of Programming Outline In this lesson, we will: See the need for asking if more than one condition is satisfied The unit pulse function Describe the binary logical AND and OR operators Introduce truth tables Describe chaining numerous logical expressions Describe short-circuit evaluation Describe the unary logical negation (NOT) Give the definition of a logical expression Douglas Wilhelm Harder, M.Math. Prof. Hiren Patel, Ph.D. hiren.patel@uwaterloo.ca dwharder@uwaterloo.ca 018 by Douglas Wilhelm Harder and Hiren Patel. Some rights reserved. 3 4 Background The unit pulse We have seen six comparison operators Three complementary pairs ==!= < >= > <= Problem: What if more than one condition is required? What if two conditions result in the same consequent? What if we require that a condition must be false? Suppose we want to implement the function: 0 x 0 def x 0 unit x 1 x 0 and x1 x 0 x This function has an integral (area under the curve) equal to 1 1

5 6 The unit pulse The unit pulse We could implement this function as follows: Can we implement this using two consequents and one alternative? double unit( double x ); double unit( double x ); double unit( double x ) { if ( x < 0.0 ) { return 0.0; else if ( x == 0.0 ) { return 0.5; else if ( x < 1.0 ) { return 1.0; else if ( x == 1.0 ) { return 0.5; return 0.0; 0 x 0 def x 0 unit x 1 x 0 and x1 x 0 x double unit( double x ) { if ( Condition A ) { return 0.0; else if ( Condition B ) { return 0.5; return 1.0; 0 x 0 def x 0 unit x 1 x 0 and x1 x 0 x 7 8 In English, we would simply say that the result is 0 if either x < 0 or x > 1, 0.5 if either x = 0 or x = 1, and 1 if both x > 0 and x < 1 In C++, there are two logical binary operators They take two Boolean values (bool) and return a Boolean value The OR operator returns true if either operands is true The AND operator && returns true if both operators are true Consequent Conditions C++ 0.0 x 0 or x (x < 0) (x > 1) 0.5 x 0 or x (x == 0) (x == 1) 1.0 x 0 and x (x > 0) && (x < 1)

9 Maximum of three 10 Thus, we may implement the function as: #include <cassert> double unit( double x ); double unit( double x ) { if ( (x < 0) (x > 1) ) { return 0.0; else if ( (0 == x) (1 == x) ) { return 0.5; assert( (x > 0) && (x < 1) ); return 1.0; We can now implement the maximum-of-three function as follows: double max( double x, double y, double z ); double max( double x, double y, double z ) { if ( (x >= y) && (x >= z) ) { // 'x' is the maximum if it is greater than or equal // to 'y' and greater than or equal to 'z' return x; else if ( y >= z ) { // Now, 'y' is the maximum if 'y' is greater than or // equal to 'z' // - if 'y' was not greater than 'x', the first // condition would have been true return y; return z; Truth tables 11 Truth tables 1 We know that the logical OR operator is true if either operand is true It is false if both operands are false We know that the logical AND operator && is true if both operand are true It is false if either operands is false To display this visually, we use a truth table In elementary school, you saw addition and multiplication tables: Given two operands, the table gave the result of the operation + 0 1 3 4 5 6 7 8 9 0 0 1 3 4 5 6 7 8 9 1 1 3 4 5 6 7 8 9 10 3 4 5 6 7 8 9 10 11 3 3 4 5 6 7 8 9 10 11 1 4 4 5 6 7 8 9 10 11 1 13 5 5 6 7 8 9 10 11 1 13 14 6 6 7 8 9 10 11 1 13 14 15 7 7 8 9 10 11 1 13 14 15 16 8 8 9 10 11 1 13 14 15 16 17 9 9 10 11 1 13 14 15 16 17 18 0 1 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 0 1 0 1 3 4 5 6 7 8 9 0 4 6 8 10 1 14 16 18 3 0 3 6 9 1 15 18 1 4 7 4 0 4 8 1 16 0 4 8 3 36 5 0 5 10 15 0 5 30 35 40 45 6 0 6 1 18 4 30 36 4 48 54 7 0 7 14 1 8 35 4 49 56 63 8 0 8 16 4 3 40 48 56 64 7 9 0 9 18 7 36 45 54 63 7 81 3

Truth tables 13 Truth tables 14 With only two possible values of the operands, these truth tables are much simpler: An alternate form is to consider all values of the operands: && true false true true false false false false true false true true true false true false x y x && y x y true true true true true false false true false false false false false true false true A reminder 15 16 Multiple conditions that may be true Just to remind you, however, the result of a logical operator is simply 0 or 1: // All these print '1': std::cout << ((3 < 4) && (4 < 5) ) << std::endl; std::cout << ((6 > 1) (4 <= 5) ) << std::endl; std::cout << ((3 == 3) (5 > 0) ) << std::endl; std::cout << ((3 <= 4) (6 >= 15)) << std::endl; If you have four conditions, any of which need be true, parentheses are not necessary: if ( (1st-cond) (nd-cond) (3rd-cond) (4th-cond) ) { // Do something... Like addition, logical OR is associative: (a + b) + c == a + (b + c) (a b) c == a (b c) If any one condition is true, then (1st-cond) (nd-cond) (3rd-cond) (4th-cond) evaluates to true If all conditions are false, the logical expression evaluates to false 4

17 Multiple conditions that must be true Multiple conditions 18 If you have four conditions, all of which must be true, you need only parenthesize the operands if ( (1st-cond) && (nd-cond) && (3rd-cond) && (4th-cond) ) { // Do something... Like addition and multiplication, both logical OR and logical AND are associative If all conditions are true, then (1st-cond) && (nd-cond) && (3rd-cond) && (4th-cond) evaluates to true If even one condition is false, the logical expression evaluates to false Note that you may combine both logical operators, but you must be clear what you mean: (x == 0) ((x <= ) && (x >= 1)) is very different from ((x == 0) (x <= )) && (x >= 1) The first is true if x is 0 or x is in the closed interval [1, ] The second is true only if x is in the closed interval [1, ] If you leave it as (x == 0) (x <= ) && (x >= 1) the compiler will decide, and programmers will be left guessing Short-circuit evaluation 19 Short-circuit evaluation 0 Consider these logical expressions: (x < -10) (x > 10) (x < -10) ((x > -1) && (x < 1)) (x > 10) Suppose that 'x' has the value -100 The first comparison operation returns true Is there is any reason to even bother testing the others? No: the result of true any-other-conditions must be true This is referred to as short-circuit evaluation Suppose now that 'x' has the value 0: (x < -10) (x > 10) (x < -10) ((x > -1) && (x < 1)) (x > 10) The first condition is false, and In the first example, (x > 10) is false and it is the last condition, so the expression is false In the second example, ((x > -1) && (x < 1)) is true, so the entire logical expression is true There is no need at this point to evaluate (x > 10) Even though it is false, the entire expression is still true 5

Short-circuit evaluation 1 Short-circuit evaluation Similarly, consider (x > -10) && (x < 10) (x > -10) && ((x < -1) (x > 1)) && (x < 10) Suppose that 'x' has the value -100 The first comparison operation returns false Is there any reason to even bother testing the others? No: the result of false && any-other-conditions must be false Suppose now that 'x' has the value 0: (x > -10) && (x < 10) (x > -10) && ((x < -1) (x > 1)) && (x < 10) The first condition is true, and In the first example, (x < 10) is true and it is the last condition, so the expression is true In the second example, ((x < -1) (x > 1)) is false, so the entire logical expression is false There is no need at this point to evaluate (x < 10) Even though it is true, the entire expression is still false Short-circuit evaluation 3 4 These functions have equivalent logical expressions: int f( int x ) { if ( ((x >= -1) && (x <= 1)) (x > 10) (x < -10) ) { return -1; return 1; int g( int x ) { if ( (x < -10) (x > 10) ((x <= 1) && (x >= -1)) ) { return -1; return 1; When do they stop evaluating when the argument passed is: -1-5 -1 7 15 Suppose we want to print a message if some number is not divisible by 13: int print_good_luck( int n ); int print_good_luck( int n ) { if ( is_divisible( n, 13 ) ) { // Do nothing std::cout << "Good choice!" << std::endl; 6

5 6 Verbally, you would simply say: If n is not divisible by 13, We can do this in C++ by using the unary logical NOT operator!: int print_good_luck( int n ); If a Boolean value is true, its negation is false, and vice versa x!x true false false true int print_good_luck( int n ) { if (!is_divisible( n, 13 ) ) { std::cout << "Good choice!" << std::endl; An alternative is: if ( is_divisible( n, 13 ) == false ) { 7 8 The following Boolean-valued statements are equivalent 1 : x is not equal to 1 It is not true that x is equal to 1 (x!= 1)!(x == 1) x is greater than 0 It is not true that x is less than or equal to 0 (x > 0)!(x <= 0) x is between 1 and 1 It is not true that x is less than 1 or greater than 1 (x >= -1) && (x <= 1)!((x < -1) (x > 1)) x y when divided by has a It is not true that x y when divided by remainder of 0 has a remainder of 1 ((x - y) % ) == 0!(((x - y) % ) == 1) The behavior of these two conditional statements are equivalent: if ( some-condition ) { // Do something // Do something completely different if (!some-condition ) { // Do something completely different // Do something 1 If the operands are the same, the result is the same. 7

9 30 Logical expression Once again, all the unary logical NOT operator does is change the value of true (that is, 1) to false (0) and vice versa void check( int n ) { std::cout <<!(n == 0) << std::endl; std::cout << (n!= 0) << std::endl; std::cout <<!(n >= 1) << std::endl; std::cout << (n < 1) << std::endl; std::cout <<!((n == 0) (n >= 1)) << std::endl; std::cout << ((n!= 0) && (n < 1)) << std::endl; Up to this point, we may now say a logical expression is either: 1. A Boolean literal: true or false representing 1 and 0, respectively. A call to a function that returns a bool: calling bool function_name( ); 3. A variable (e.g., a parameter) that is declared to be of type bool: int find(, bool is_sorted, ); 3. A comparison operator with its two operands: x < 0 or (x + y)/ == 1 4. The negation of a logical expression:!(logical-expression) 5. The chaining of two or more logical expressions with a logical OR: (logic-1) (logic-) (logic-3) 7. The chaining of two or more logical expressions with a logical AND: (logic-1) && (logic-) && (logic-3) Logical expression 31 Functional completeness 3 We can now make the following statements: A logical expression will always evaluate to either 1 (i.e., true) or 0 (i.e., false) Any logical expression can be a condition of a conditional statement Any logical expression can be returned by a function that has a return type of bool Any logical expression can be passed to a function as an argument where the corresponding parameter has the type bool Question: Why do we have these four logical operators AND, OR and NOT? Suppose we have two conditions E.g., x > 0 and y <= 1 Based on combinations of these statements E.g., the first being true and the second being false, or the first being false and the second being true we may want to execute a body of statements and execute a different body of statements otherwise These operators are sufficient to allow us to reasonably easily encode all such combinations More of this will be covered in your courses on digital logic and digital circuits 8

false!p && Q!(P Q)!P P &&!Q (P &&!Q) (!P && Q)!Q!(P && Q) P && Q Q (P && Q) (!P &&!Q)!P Q P P Q P!Q true Functional completeness 33 Decision making 34 Suppose we may have two logical expressions P and Q: Either of these may be either true or false We may wish to perform some operation based on any combination Why do we include these operators? The literal logical values true and false together with operations such as AND, OR and NOT define Boolean logic In 1938, Claude Shannon wrote his master s thesis where he demonstrated that the behavior of relays can be modelled by Boolean logic P Q T T F F F F F F F F T T T T T T T T T F F F F F T T T T F F F F T T T T F F F F T T F F T T F F T T F F T T F T F T F T F T F T F T F T F T F T A relay is a switch that can be turned on or off Usually with an electromagnet Transistors are excellent solid-state approximations of switches Their behavior can still be modelled by Boolean logic One final aside 35 Summary 36 In Claude Shannon s master s thesis, written in 1937 when he was 1-years old, he demonstrated that Boolean algebra was sufficient to construct any logical, numerical relationship He founded information theory Shannon's maxim: The enemy knows the system He also invented the ultimate machine: Following this lesson, you now: Understand that two or more conditions can be chained together With a logical AND (&&), all must be true for the result to be true With a logical OR ( ), one must be true for the result to be true Are familiarized with truth tables Understand the idea of short-circuit evaluation As soon as one condition is false in a chain of logical ANDs, we re done: the result must be false As soon as one condition is true in a chain of logical ORs, we re done: the result must be true Understand that logical negation switches between true and false Know the definition of a logical expression Every logical expression can be a condition of a conditional statement 9

References 37 Colophon 38 [1] No references? These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 7, 018 by Douglas Wilhelm Harder. Please see https://www.rbg.ca/ for more information. Disclaimer 39 These slides are provided for the ECE 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended. 10