Booleans Lecture 26. Summary of previous lecture In the previous lecture, we have been learnt,  The if statement  The else statement  Cascaded if 

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Lecture 2 Dr Richard Reilly Dept. of Electronic & Electrical Engineering Room 153, Engineering Building To insert your company logo on this slide From.
1 CSE1301 Computer Programming Lecture 7 Booleans Linda M c Iver.
FIT Objectives By the end of this lecture, students should: understand selection statements understand nested selection understand tradeoff.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans.
Propositional Calculus Math Foundations of Computer Science.
1 Chapter Four Boolean Expressions and Control Statements.
Logic Gates Circuits to manipulate 0’s and 1’s. 0’s and 1’s used for numbers Also to make decisions within the computer. In that context, 1 corresponds.
Programming for GCSE Topic 3.3: Boolean Logic and Truth Tables
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Boolean Algebra – I. Outline  Introduction  Digital circuits  Boolean Algebra  Two-Valued Boolean Algebra  Boolean Algebra Postulates  Precedence.
Systems Architecture I1 Propositional Calculus Objective: To provide students with the concepts and techniques from propositional calculus so that they.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
1 Fundamentals of Computer Science Propositional Logic (Boolean Algebra)
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Decision Structures and Boolean Logic
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /05/2013 Lecture 4: Basics of Logic Design Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
April 6, 1998CS102-02Lecture 2-1 Java Operators CS Lecture 2-1 Being a Smooth Operator.
D75P 34R - HNC Computer Architecture Week 6 Boolean Logic © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise stated.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
4. Computer Maths and Logic 4.2 Boolean Logic Simplifying Boolean Expressions.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
Conditional Expressions
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Laws of Boolean Algebra Commutative Law Associative Law Distributive Law Identity Law De Morgan's Theorem.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
ECE DIGITAL LOGIC LECTURE 6: BOOLEAN ALGEBRA Assistant Prof. Fareena Saqib Florida Institute of Technology Fall 2016, 02/01/2016.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Yaohang Li.
Logic Simplification-Using Boolean Laws Logic Design Laboratory EE 2121 Lectures By Manesh T EE2121-In Charge
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Computer Architecture & Operations I
Computer Architecture & Operations I
Morgan Kaufmann Publishers
Revision of IO Streams printf scanf.
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
Selection (also known as Branching) Jumail Bin Taliba by
Boolean Algebra & De Morgan's Theorems
A mechanism for deciding whether an action should be taken
Topics The if Statement The if-else Statement Comparing Strings
Flow of Control.
Flow of Control.
Topics The if Statement The if-else Statement Comparing Strings
Propositional Calculus: Boolean Algebra and Simplification
Relational Operators Operator Meaning < Less than > Greater than
Bits and Bytes Boolean algebra Expressing in C
C Operators, Operands, Expressions & Statements
Flow of Control.
Introduction to Programming – 4 Operators
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Introduction to Computing Lecture 04: Booleans & Selection
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Controlling Program Flow
Presentation transcript:

Booleans Lecture 26

Summary of previous lecture In the previous lecture, we have been learnt,  The if statement  The else statement  Cascaded if  Nested if  Switch statement

Today’s Topics Boolean Type int as Boolean Boolean expressions Boolean Operators Precedence Common mistakes

Boolean A special “type” with only two values:  false and true. Used to implement conditions  for selection and looping in an algorithm Boolean expressions  represent statements which are either strictly true or strictly false We still have to cover

Boolean Algebra --- History George Boole,  Initially self-guided studies of languages and philosophy  With 16 assistant teacher at private school  With 20 opened a school and taught himself mathematics  Published in “Cambridge Mathematical Journal” with 24 The Mathematical Analysis of Logic was published in 1847  Casts logical reasoning in the form of algebra + is OR, * is AND 0 is FALSE, 1 is TRUE An Investigation of the Laws of Thought (1854) extends the algebra

Type int as Boolean In C, integers are used as Booleans Integer value 0 is false. Any non-zero integer value is true.

#include /* Test some Booleans. */ int main() { int whatever = 0; if (whatever) { printf( “ Is that true, John?\n ” ); } else { printf( “ No, Jane.\n ” ); } return 0; } Example: What is the output?

#include /* Test some Booleans. */ int main() { int whatever = 1; if (whatever) { printf( “ Is that true, John?\n ” ); } else { printf( “ No, Jane.\n ” ); } return 0; } Example: What is the output?

#include /* Test some Booleans. */ int main() { int whatever = -100; if (whatever) { printf( “ Is that true, John?\n ” ); } else { printf( “ No, Jane.\n ” ); } return 0; } Example: What is the output?

#include /* Test some Booleans. */ int main() { int whatever = ’ A ’ ; if (whatever) { printf( “ Is that true, John?\n ” ); } else { printf( “ No, Jane.\n ” ); } return 0; } Example: What is the output?

#include /* Test some Booleans. */ int main() { int whatever = 0.003; if (whatever) { printf( “ Is that true, John?\n ” ); } else { printf( “ No, Jane.\n ” ); } return 0; } Example: What is the output?

#include /* Test some Booleans. */ int main() { float whatever = 0.003; if (whatever) { printf( “ Is that true, John?\n ” ); } else { printf( “ No, Jane.\n ” ); } return 0; } Behavior is “undefined.”

Boolean Expressions...are either strictly true or strictly false.... can be evaluated to determine their true or falsehood. A Boolean expression which evaluates to true has integer value 1; otherwise, 0. Remember: any non-zero value is taken interpreted as true

Boolean Operators...are used in forming Boolean expressions....are also known as “logical” operators And ( && ) Or ( || ) Not ( ! ) Equality ( == ) Inequality ( != ) Comparison (, = )

And True only if both Boolean arguments are true. 1 && 2 1 && 0 && -1 healthy && wealthy && wise Examples:

And True only if both Boolean arguments are true. 1 && 2 1 && 0 && -1 healthy && wealthy && wise Examples: not to be confused with “bitwise AND” ( & )

Or True if either Boolean argument is true (or both are true). 1 || 2 11 || 0 || 1 good || bad || ugly Examples:

Or True only if either Boolean argument is true (or both are true). 1 || 2 11 || 0 || 1 good || bad || ugly Examples: not to be confused with “bitwise OR” ( | )

Not True only if the single Boolean argument is false. ! 1 ! 0 ! happy Examples:

Reminder: Gates AB AND Gate A AND B A OR B OR Gate

A 0 1 NOT A NOT Gate Reminder: Gates

Equality True only if both arguments have identical values. 1 == 2 1 == 0 42 == 42 truth == beauty Examples:

Equality True only if both arguments have identical values. 1 == 2 1 == 0 42 == 42 truth == beauty Examples: not to be confused with assignment ( = )

Inequality True only if arguments have different values. 1 != 2 1 != 0 42 != 42 truth != beauty Examples:

Comparison True only if the specified relationship holds 1 < 2 0 > 1 42 <= 42 age >= 18 Examples:

Short-circuiting A complex Boolean expression is only evaluated as far as necessary 1 || 2 0 || 1 || 2 1 && 0 && -1 Examples:

Precedence Highest to lowest:  Brackets  Not ( ! )  Comparison (, = )  Equality ( == )  Inequality ( != )  And ( && )  Or ( || ) Note: The assignment operator (=) is lower in order of precedence than Boolean / Logical operators.

Boolean Algebra NOT false = true NOT true = false true AND true = true true AND false = false true OR true = true true OR false = true false AND false = false false OR false = false if (0 || 0) printf("Does false OR false = true?"); if (0 && 0) printf("Does false AND false = true?");

Example: #include int main() { int age = 18; int haveMoney = 0; int haveCard = 1; float thirst = 0.31; int afterHours = 1; int result; result = age >= 18 && (haveMoney || haveCard) && thirst > 0.3 && ! afterHours; printf("%d\n", result); return 0; } bool.c

Common Mistakes Using = instead of ==

Example: #include /* Probably the most common C error. */ int main() { int score; scanf("%d", &score); if (score = 48 || score = 49) { printf("Almost!\n"); } return 0; } boolerr1.c

Example: #include /* Probably the most common C error. */ int main() { int score; scanf("%d", &score); if (score = 48 || score = 49) { printf("Almost!\n"); } return 0; } boolerr1.c Usual error message (if any): “LValue required...”

Example: #include /* Probably the most common C error. */ int main() { int score; scanf("%d", &score); if (score == 48 || score == 49) { printf("Almost!\n"); } return 0; } boolerr1.c

Common Mistakes Using = instead of == Multiple comparisons

Example: #include /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) { printf("Fail\n"); } return 0; } boolerr2.c

#include /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) { printf("Fail\n"); } return 0; } Example: boolerr2.c 0 or 1

#include /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) { printf("Fail\n"); } return 0; } Example: boolerr2.c 0 or 1 always 1

Example: #include /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score && score < 48 ) { printf("Fail\n"); } return 0; } boolerr2.c

True and False as Constants ‘C’ does not provide constants for TRUE/FALSE (other than 0 and 1) You can make use of the pre-processor Use  #define TRUE 1  #define FALSE 0 whenever you need such constants

Exclusive OR True if “at most one” of two alternatives is true False if neither is true False if both are true! Define! A xor B : Do you need brackets?

Two out of three… True if and only if exactly two of A,B,C are true Define! Is there a better way?

Simplifying & Checking Boolean Expressions Use Truth Tables Transform Expressions using Boolean Algebra ABA xor B AB A && !B || B && !A

Axioms of Boolean Algebra Commutative  (A && B)  (B && A)  (A || B)  (B || A) Associative  A && (B && C)  (A && B) && C  A || (B || C)  (A || B) || C

Axioms of Boolean Algebra (cont) Distributive  A && (B || C)  (A && B) || (A && C)  A || (B && C)  (A || B) && (A || C) … plus some “technicalities”

De Morgan’s Law From the axioms of Boolean algebra it follows that:  ! ( A || B )  !A && !B  !(A && B)  !A || !B You can use all rules above to simplify / verify expressions. Exercise: simplify ! ( A || !B ) && A

Conditional Expressions We can write an expression such that its value depends on a TRUTH value Condition ? Expr2 : Expr3  A ternary operator  For example: int z,a,b; … z = (a>b) ? a : b ;

More Examples ch1 = 'a'; ch2 = 'a'; printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1 printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 1 ch1 = 'd'; ch2 = 'f'; printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1 printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 1 ch1 = 'a'; ch2 = '\0'; printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1 printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 0

Summary Boolean Type int as Boolean Boolean expressions Boolean Operators Precedence Common mistakes Some Boolean algebra