1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans.

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 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
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 CSE 20: Lecture 7 Boolean Algebra CK Cheng 4/21/2011.
Propositional Calculus Math Foundations of Computer Science.
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.
Digital Logic Circuits – Chapter 1 Section 1-3, 1-2.
Boolean Algebra – I. Outline  Introduction  Digital circuits  Boolean Algebra  Two-Valued Boolean Algebra  Boolean Algebra Postulates  Precedence.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
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)
CSC 107 – Programming For Science. Follow-Up From Last Lecture.
Decision Structures and Boolean Logic
Lecture 7 Topics –Boolean Algebra 1. Logic and Bits Operation Computers represent information by bit A bit has two possible values, namely zero and one.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
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.
Logic Gates Shashidhara H S Dept. of ISE MSRIT. Basic Logic Design and Boolean Algebra GATES = basic digital building blocks which correspond to and perform.
D75P 34R - HNC Computer Architecture Week 6 Boolean Logic © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise stated.
Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
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.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
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.
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.
Booleans Lecture 26. Summary of previous lecture In the previous lecture, we have been learnt,  The if statement  The else statement  Cascaded if 
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
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
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.
Rational Expressions relational operators logical operators order of precedence.
Computer Architecture & Operations I
Computer Architecture & Operations I
CPS120 Introduction to Computer Science
Morgan Kaufmann Publishers
Revision of IO Streams printf scanf.
CSE 220 – C Programming Expressions.
CS Chapter 3 (3A and ) Part 1 of 8
C# Operator Overloading and Type Conversions
Boolean Algebra & De Morgan's Theorems
OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS
CS Chapter 3 (3A and ) Part 1 of 8
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
Propositional Calculus: Boolean Algebra and Simplification
All the Operators 22-Nov-18.
Relational Operators Operator Meaning < Less than > Greater than
Bits and Bytes Boolean algebra Expressing in C
C Operators, Operands, Expressions & Statements
Introduction to Programming – 4 Operators
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Introduction to Computing Lecture 04: Booleans & Selection
C++ Programming Language Lecture 4 C++ Basics – Part II
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Controlling Program Flow
Presentation transcript:

1 Revision of IO Streams printf scanf

2 CSE1301 Computer Programming Lecture 8 Booleans

3 Topics Boolean Type int as Boolean Boolean expressions Boolean Operators Precedence Common mistakes

4 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

5 Boolean Algebra --- History George Boole, –Initially self-guided studies of languages and philosphy –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

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

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

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

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

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

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

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

13 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

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

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

16 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” ( & )

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

18 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” ( | )

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

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

21 A 0 1 NOT A NOT Gate Reminder: Gates

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

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

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

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

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

27 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.

28 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

29 Common Mistakes Using = instead of ==

30 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

31 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...”

32 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

33 Common Mistakes Using = instead of == Multiple comparisons

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

35 #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

36 #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

37 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

38 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

39 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?

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

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

42 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

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

44 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

45 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 ;

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

47 Readings This Lecture: King: Section 5.1 D&D: Sections 2.6, 3.4 – 3.6, 4.10 Kernighan & Ritchie: 2.6, 2.12