George Boole A century ago there was a mathematician, George Boole, who took statements and wrote them in a precise format, such that a statement is.

Slides:



Advertisements
Similar presentations
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Thurs., March 4, 1999 n Homework 2 due n Handout Homework 3 (Due Thurs., March 18) n Turing Test n Multivariate Logic and Event Spaces n Week 8: Subjective.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Propositional Calculus Math Foundations of Computer Science.
Digital Logic Circuits – Chapter 1 Section 1-3, 1-2.
AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions require indirect knowledge of Boolean Logic, and.
Boolean expressions Conditional statements and expressions.
CSC 107 – Programming For Science. Follow-Up From Last Lecture.
Computer Science Selection Structures.
Chapter 3: Data Types and Operators JavaScript - Introductory.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
George Boole More than 150 years ago, there was a mathematician named George Boole, who took statements and wrote them in a precise format, such that.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
Chapter 4 Introduction to Control Statements Section 1 - Additional Java Operators Section 2 - If Statements Section 3 - If-Else Statements Section 4.
George Boole More than 150 years ago, there was a mathematician named George Boole, who took statements and wrote them in a precise format, such that.
1 Java Basics: Data Types, Variables, and Loops “ If debugging is the process of removing software bugs, then programming must be the process of putting.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Lecture 4 Boolean Algebra. Logical Statements °A proposition that may or may not be true: Today is Monday Today is Sunday It is raining °Compound Statements.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
Boolean values Gateway to decision making. Background Our problem-solving solutions so far have the straight-line property –They execute the same statements.
Laws of Boolean Algebra Commutative Law Associative Law Distributive Law Identity Law De Morgan's Theorem.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Fundamental Programming Fundamental Programming More Expressions and Data Types.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Logic Simplification-Using Boolean Laws Logic Design Laboratory EE 2121 Lectures By Manesh T EE2121-In Charge
AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions require indirect knowledge of Boolean Logic, and.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
1 Binary Signals Logic gate circuits are designed to input and output only two types of signals: “high” (1) and “low” (0), as represented by a variable.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
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.
George Boole More than 150 years ago, there was a mathematician named George Boole, who took statements and wrote them in a precise format, such that.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
Dr. ClincyLecture Slide 1 CS6020- Chapter 3 (3A and ) Dr. Clincy Professor of CS First Exam - Tuesday, September 6th Coverage: All subjects up to.
AP Exam Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions require indirect knowledge of Boolean Logic, and other.
Computer Programming Boolean Logic.
CPS120 Introduction to Computer Science
Exposure Java 2012 APCS Edition Chapter 13 Slides
Morgan Kaufmann Publishers
Computer Science 210 Computer Organization
Selection (also known as Branching) Jumail Bin Taliba by
Section 7.1 Logical Operators
More important details More fun Part 3
Selection—Making Decisions
Boolean Algebra.
Building Java Programs
Computer Science 210 Computer Organization
Building Java Programs
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Building Java Programs
Building Java Programs
Building Java Programs
Computer Programming Boolean Logic Trade & Industrial Education
Building Java Programs
Control Structure.
Digital Systems Section 3 Boolean Algebra. Digital Systems Section 3 Boolean Algebra.
Building Java Programs
Presentation transcript:

George Boole A century ago there was a mathematician, George Boole, who took statements and wrote them in a precise format, such that a statement is always true or false. He founded a branch of math called Boolean Algebra. Statements that are either true or false are called Boolean statements. The conditions you used with selection and repetition back in Chapter 5 were all Boolean statements. The boolean datatype is named after George Boole.

AP Exam Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions require indirect knowledge of Boolean Logic, and other questions are directly focused on testing a student’s understanding of Boolean concepts. Test results have shown that many students score quite poorly on this part of the APCS Examination. Statistical Analysis of these test results have also shown that the students who perform poorly on Boolean Logic questions, perform poorly on the AP Exam as a whole; and the students who perform well on the Boolean Logic questions, perform well on the AP Exam as a whole.

What is a Boolean Statement? The sentence, statement, condition, whatever, must be true or false. No questions, no ambiguities, no arguments. The basis of processing data is the binary system of on and off, or 1 and 0, which certainly sounds a bunch like true or false. Each of the following five statements is a Boolean statement: A mile is longer than a kilometer. July and August both have the same number of days. A pound of feathers is lighter than a pound of lead. The Moon is larger than the Sun. New York City has more people than Baltimore.

English SentenceBoolean StatementT/F A mile is longer than a kilometer. Mile > Kilometer True July and August have the same days. JulDays == AugDays True A pound of feathers is lighter than a pound of lead. PoundF < PoundL False The Moon is larger than the Sun. MoonSize > SunSize False New York City has more people than Baltimore. NYPop > BaltPop True Boolean Statement Examples

Sentences are not always so short and straight forward. Frequently there are multiple conditions in one statement. Special rules need to be followed to determine if the entire statement is true or false. Consider the following sentences with compound conditions: She is a computer science teacher and she is a math teacher. The number is odd or the number is even. Enter again if gender is not male or gender is not female. Employment requires a CPA and five years experience. The same sentences converted into Boolean statements are: (She==CSTeacher) and (She==MathTeacher) (Number%2==1) or (Number%2!=1) (Gender!=Male) or (Gender!=Female) (CPA == "Y") and (YrExp >= 5)

Logical OR Example Consider two young ladies who have a rather simplistic, and quite politically incorrect, view of judging potential dates. The first is Kathy who will date a guy if he is Good Looking OR drives a Nice Car. This chart shows her 4 possible cases: Good Looking?Nice Car?Date Material?

Boolean Operators Boolean OR ATTFFATTFF BTFTFBTFTF A or B T F

Logical AND Example Suzy is more picky than Kathy. Suzy will only date a guy if he BOTH Good Looking AND drives a Nice Car. This chart shows her 4 possible cases: Good Looking?Nice Car?Date Material?

Boolean Operators Boolean AND ATTFFATTFF BTFTFBTFTF A and B T F

Boolean Operators Boolean XOR ATTFFATTFF BTFTFBTFTF A xor B F T F

Boolean Operators Boolean NOT ATFATF ~A F T

Truth Table #1 A and (A or B) ATTFFATTFF BTFTFBTFTF A or B T F A and (A or B) T F

Truth Table #2 (A and B) or C ATTTTFFFFATTTTFFFF BTTFFTTFFBTTFFTTFF CTFTFTFTFCTFTFTFTF A and B T F (A and B) or C T F T F T F

Truth Table #3 (A or B) and C ATTTTFFFFATTTTFFFF BTTFFTTFFBTTFFTTFF CTFTFTFTFCTFTFTFTF A or B T F (A or B) and C T F T F T F

Truth Table Fact The truth tables of equivalent Boolean expressions are identical.

Truth Table #4 Is ~(A or B) = ~A or ~B ? ATTFFATTFF BTFTFBTFTF A or B T F ~(A or B) F T ~A F T ~B F T F T ~A or ~B F T ^ NO ^

Truth Table #5 Is ~(A or B) = ~A and ~B ? ATTFFATTFF BTFTFBTFTF A or B T F ~(A or B) F T ~A F T ~B F T F T ~A and ~B F T ^ YES ^

Truth Tables #6 (T/F) and #7 (1/0) Is ~(A and B) = ~A or ~B ? ATTFFATTFF BTFTFBTFTF A and B T F ~(A and B) F T ~A F T ~B F T F T ~A or ~B F T A1100A1100 B1010B1010 A and B 1 0 ~(A and B) 0 1 ~A 0 1 ~B ~A or ~B 0 1 YES

Boolean Algebra Notation A or B  A + B A and B  A●B A*B AB not A  ~A true  1 false  0

Basic Laws of Boolean Algebra - 1 A + A = AIdempotent Law for or A * A = AIdempotent Law for and A + B = B + ACommutative Law for or A * B = B * ACommutative Law for and A + (B + C) = (A + B) + CAssociative Law for or A * (B * C) = (A * B) * CAssociative Law for and A * (B+C) = (A*B) + (A*C)Distributive Law for and over or A + (B*C) = (A+B) * (A+C)Distributive Law for or over and

Basic Laws of Boolean Algebra - 2 A + 1 = 1Law of Union A * 0 = 0Law of Intersection A * (A + B) = ALaw of Absorption A + (A * B) = ALaw of Absorption A * 1 = A1 is the identity element for and A + 0 = A0 is the identity element for or ~(~A) = ADouble negative law

Basic Laws of Boolean Algebra - 3 A + ~A = 1Law of Complement for or A * ~A = 0Law of Complement for and ~(A + B) = ~A * ~BDeMorgan's Law ~(A * B) = ~A + ~BDeMorgan's Law

The Law of Absorption presents the following two expression equivalencies: A * (A + B) = A A + (A * B) = A AATTFFAATTFF BBTFTFBBTFTF A and B A*B T F A or B A+B T F A and (A or B) A * (A+B) T F A or (A and B) A + (A*B) T F ^ All 3 are the same! ^ ^

Venn Diagram #1 The Boolean Algebra logical and ( * ) can be demonstrated with Venn Diagrams, using intersection. A intersect B also A and B also A * B also AB

Venn Diagram #2 The Boolean Algebra logical or ( + ) can be demonstrated with Venn Diagrams, using union. A union B also A or B also A + B

Venn Diagram #3 The Boolean Algebra logical not ( ~ ) not A also ~A This is the opposite of A.

Venn Diagram #4 The Boolean Algebra logical not ( ~ ) not B also ~B This is the opposite of B.

Venn Diagram #5 not (A and B) ~(A * B) This is the opposite of (A and B).

Venn Diagram #6 not (A or B) ~(A + B) This is the opposite of (A or B).

Venn Diagram #7 not A and not B ~A * ~B This is identical to not(A or B).

Venn Diagram #8 not A or not B ~A + ~B This is identical to not(A and B).

DeMorgan’s Law not(A and B) = not A or not B not(A or B) = not A and not B

Tricky Venn Diagrams A and not (B) A or not(B) A * ~B A + ~B Step 1 – Shade A AB

Tricky Venn Diagrams A and not (B) A or not(B) A * ~B A + ~B Step 2 – Shade ~B a different way

Tricky Venn Diagram A and not (B) A * ~B Answer: The part that is shaded twice.

Tricky Venn Diagram A or not (B) A + ~B Answer: Everything that is shaded.

The first group of problems are meant to be simple and straight forward. These problems are designed to help you understand Boolean logic better. These problems are not APCS Examination type problems. Some of the problems that follow later are similar in nature to the Boolean Algebra multiple choice questions that were presented on previously released APCS examinations. The AP Computer Science Examination includes a number of questions that are language independent. Boolean Algebra questions are one group of language independent questions. AP Exam Alert

Sample Problem 1. The Boolean expression A >= B is equivalent to which of the following expressions? (A) not(A < B) (B) not(B >= A) (C) not(A <= B) (D) A != B (E) B >= A Answer: A

Sample Problem 2. The Boolean expression (A or B) and B is true (A) whenever A is true. (B) whenever B is true. (C) whenever either A is true or B is true. (D) only whenever both A is true and B is true. (E) for all values of A and B. Answer: B

Sample Problem 3. The Boolean expression (A or B) and (A or B) is true (A) only when A is true. (B) only when B is true. (C) whenever either A is true or B is true. (D) only whenever both A is true and B is true. (E) for all values of A and B. Answer: C

Sample Problem 4. The Boolean expression not(A and B and C) is equivalent to which of the following expressions? (A) A != B != C (B) A and B and C (C) A or B or C (D) not A or not B or not C (E) not A and not B and not C Answer: D

Sample Problem 5. The Boolean expression (A and B) and not(A and B) evaluates to (A) false in all cases. (B) true in all cases. (C) true whenever only A is true or only B is true. (D) true whenever both A is true and B is true. (E) false only when both A is false and B is false. Answer: A

Sample Problem 6. The Boolean expression not((A D)) is equivalent to which of the following expressions? (A) (A D) (B) (A >= B) and (C <= D) (C) (A >= B) or (C <= D) (D) (A > B) and (C < D) (E) (A > B) or (C < D) Answer: C

Sample Problem 7. The Boolean expression (A > B) or (A <= B) can be simplified to which of the following expressions? (A) A or B (B) A and B (C) A and not B (D) false (E) true Answer: E

Sample Problem 8. The Boolean expression (A and B) and (not A or not B) evaluates to (A) true in all cases. (B) false in all cases. (C) true only whenever both A is true and B is true. (D) false only whenever both A is false and B is false. (E) true only whenever A is true or B is true. Answer: B

Sample Problem 9. The Boolean expression (A and B) or (not A or not B) evaluates to (A) true in all cases. (B) false in all cases. (C) true only whenever both A is true and B is true. (D) false only whenever both A is false and B is false. (E) true only whenever A is true or B is true. Answer: A

Sample Problem 10. The Boolean expression (A and B) or (not A and not B) evaluates to (A) true in all cases. (B) false in all cases. (C) true only whenever both A is true and B is true. (D) false only whenever both A is false and B is false. (E) true only whenever both A and B are true or both A and B are false. Answer: E ATTFFATTFF BTFTFBTFTF A and B T F ~A and ~B F T ~A F T ~B F T F T (A and B) or (~A and ~B) T F T

// Java1001.java // This program demonstrates that conditional statements have // true or false Boolean values and can display such values. public class Java1001 { public static void main(String args[]) { System.out.println("\nJAVA1001.JAVA\n"); int x = 10; System.out.println(x == 10); System.out.println(x == 5); System.out.println(); }

// Java1002.java // This program introduces the data type, which only has two // values, true or false. Boolean variables adds readability to programs. import java.util.Scanner; public class Java1002 { public static void main (String args[]) { System.out.println("\nJAVA1002.JAVA\n"); Scanner input = new Scanner(System.in); int gcf; boolean correct = false; int attempt = 0; while (!correct) { attempt++; System.out.print("\nWhat is the GCF of 120 and 108? --> "); gcf = input.nextInt(); if (gcf == 12) correct = true; else correct = false; } System.out.println("\nAnswered correctly after " + attempt + " Attempt(s).\n"); }

// Java1003.java // This program executes in the same manner as Java1002.java. // The abbreviated Boolean assignment statement is used in place of the // longer if... else syntax. import java.util.Scanner; public class Java1003 { public static void main (String args[]) { System.out.println("\nJAVA1003.JAVA\n"); Scanner input = new Scanner(System.in); int gcf; boolean correct = false; int attempt = 0; while (!correct) { attempt++; System.out.print("\nWhat is the GCF of 120 and 108? --> "); gcf = input.nextInt(); correct = (gcf == 12); } System.out.println("\nAnswered correctly after " + attempt + " Attempt(s).\n"); }

Think of it this way… VariableExpressionAssignment int x; x = ; double y; y = ; String name;“John” + “Smith”name = “John”+”Smith”; boolean pass;grade >= 70pass = grade >= 70;

Boolean Logic Exercises This section provides you with ten Boolean logic exercises. The main difference between these exercises and the previous exercises is that these use actual Java syntax.

Logical Operators In Java Java uses || to indicate a logical or. Java uses && to indicate a logical and. Java uses ! to indicate a logical not.

Sample Problem 1. The Boolean expression (A && B) || (A && C) is equivalent to which of the following expressions? (A) A && (B || C) (B) A || (B && C) (C) (A || B) && (A || C) (D) A && B && C (E) A || B || C Answer: A

Sample Problem 2. The Boolean expression (A || B) && B is true (A) only when A is true. (B) only when B is true. (C) whenever either A is true or B is true. (D) only whenever both A is true and B is true. (E) for all values of A and B. Answer: B

Sample Problem 3. The Boolean expression (A && B) && A is true (A) only when A is true. (B) only when B is true. (C) whenever either A is true or B is true. (D) only whenever both A is true and B is true. (E) for all values of A and B. Answer: D

Sample Problem 4. The Boolean expression (A || B) && (A || B) is true (A) only when A is true. (B) only when B is true. (C) whenever either A is true or B is true. (D) only whenever both A is true and B is true. (E) for all values of A and B. Answer: C

Sample Problem 5. The Boolean expression !(A && B) is equivalent to which of the following expressions? (A) A != B (B) !A && B (C) A || B (D) !A || !B (E) !A && !B Answer: D

Sample Problem 6. The Boolean expression (A && B) && !(A && B) evaluates to (A) false in all cases. (B) true in all cases. (C) true whenever only A is true or only B is true. (D) true whenever both A is true and B is true. (E) false only when both A is false and B is false. Answer: A

Sample Problem 7. The Boolean expression (A || B) && !(A || B) evaluates to (A) false in all cases. (B) true in all cases. (C) true whenever only A is true or only B is true. (D) true whenever both A is true and B is true. (E) false only when both A is false and B is false. Answer: A

Sample Problem 8. The Boolean expression (A > B) && (A <= B) can be simplified to which of the following expressions? (A) A || B (B) A && B (C) A && ! B (D) false (E) true Answer: D

Sample Problem 9. The Boolean expression (A && B) || (!A || !B) evaluates to (A) true in all cases. (B) false in all cases. (C) true only whenever both A is true and B is true. (D) false only whenever both A is false and B is false. (E) true only whenever A is true or B is true. Answer: A

Sample Problem 10. Consider the following program segment. Assume that X is a Boolean variable. X = !(X == true); (A) X will always be true. (B) X will always be false. (C) The value of X does not change. (D) The value of X always changes. (E) The result cannot be determined with the given information Answer: D XTFXTF X == true T F !(X == true) F T