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.

Slides:



Advertisements
Similar presentations
BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
Advertisements

INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Computers and Programming Lecture 5 New York University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Introduction to Computer Programming Decisions If/Else Booleans.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
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.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions require indirect knowledge of Boolean Logic, and.
Computer Science Selection Structures.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures I
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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.
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.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
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.
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 4: Control Structures SELECTION STATEMENTS.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.
1 if / else statements. 2 Conditionals “If you eat your vegetables, then you can have dessert.” “If you do your homework, then you may go outside to play,
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
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.
1 CSE 142 Lecture Notes Conditional Execution with if Statements; Methods that Return Values (briefly) Chapters 3 and 4 Suggested reading: ;
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions require indirect knowledge of Boolean Logic, and.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
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.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
// Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
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.
AP Exam Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions require indirect knowledge of Boolean Logic, and other.
Exposure Java 2012 APCS Edition Chapter 13 Slides
Chapter 4: Control Structures I
Chapter 4: Control Structures I
Introduction to programming in java
SELECTION STATEMENTS (1)
Lecture 4: Conditionals
Chapter 4: Control Structures I
Self study.
Control Structure Chapter 3.
Computer Programming Boolean Logic Trade & Industrial Education
Control Structure.
Conditionals.
Presentation transcript:

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 a statement is always true or false. He founded a branch of mathematics 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. There is a boolean datatype named after George Boole.

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 == 0) (Gender != ‘M’) or (Gender != ‘F’) (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

Boolean Operators Boolean NOT Continued ATTFFATTFF BTFTFBTFTF ~A F T ~B F T F T

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.

// Java1001.java // This program demonstrates a boolean expression being used in an if statement. public class Java1001 { public static void main(String args[]) { System.out.println("\nJAVA1001.JAVA\n"); int x = 10; if ( x == 10 ) System.out.println("true"); else System.out.println("false"); if ( x == 5 ) System.out.println("true"); else System.out.println("false"); System.out.println(); }

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

Output for the next 3 programs…

// Java1003.java // This program shows a boolean expression being used in a while statement. public class Java1003 { public static void main (String args[]) { System.out.println("\nJAVA1003.JAVA\n"); int gcf = 0; int attempt = 0; while (gcf != 12) { attempt++; System.out.print("\nWhat is the GCF of 120 and 108? --> "); gcf = Expo.enterInt(); } System.out.println("\nAnswered correctly after " + attempt + " Attempt(s).\n"); }

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

// Java1005.java // This program executes in the same manner as Java1004.java. // The abbreviated Boolean assignment statement is used in place of the // longer if... else syntax. public class Java1005 { public static void main (String args[]) { System.out.println("\nJAVA1005.JAVA\n"); int gcf; boolean correct = false; int attempt = 0; while (!correct) { attempt++; System.out.print("\nWhat is the GCF of 120 and 108? --> "); gcf = Expo.enterInt(); 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;

Java uses || to indicate a logical OR. Java uses && to indicate a logical AND. Java uses ! to indicate a logical NOT. Java uses != for not equals, but it can also be used to indicate a logical XOR. Java Logical Operators

// Java1006.java // This program demonstrates compound decisions with the logical or ( || ) // operator. public class Java1006 { public static void main (String args[]) { System.out.println("Java1006\n"); int education; // years of education int experience; // years of work experience System.out.print("Enter years of education ===>> "); education = Expo.enterInt(); System.out.print("Enter years of experience ===>> "); experience = Expo.enterInt(); System.out.println(); if (education >= 16 || experience >= 5) System.out.println("You are hired!"); else System.out.println("You are not qualified."); System.out.println(); }

NICE BOSS

// Java1007.java // This program demonstrates compound decisions with the logical and ( && ) // operator. public class Java1007 { public static void main (String args[]) { System.out.println("Java1007\n"); int education; // years of education int experience; // years of work experience System.out.print("Enter years of education ===>> "); education = Expo.enterInt(); System.out.print("Enter years of experience ===>> "); experience = Expo.enterInt(); System.out.println(); if (education >= 16 && experience >= 5) System.out.println("You are hired!"); else System.out.println("You are not qualified."); System.out.println(); }

PICKY BOSS

// Java1008.java // This program demonstrates compound decisions with not equals ( != ) operator. // != can be used to implement XOR (eXclusive OR). public class Java1008 { public static void main (String args[]) { System.out.println("Java1008\n"); int education; // years of education int experience; // years of work experience System.out.print("Enter years of education ===>> "); education = Expo.enterInt(); System.out.print("Enter years of experience ===>> "); experience = Expo.enterInt(); System.out.println(); if (education >= 16 != experience >= 5) System.out.println("You are hired!"); else System.out.println("You are not qualified."); System.out.println(); }

CHEAP BOSS

// Java1009.java // This program demonstrates the logical not ( ! ) operator. public class Java1009 { public static void main (String args[]) { System.out.println("Java1009\n"); int education; // years of education int experience; // years of work experience System.out.print("Enter years of education ===>> "); education = Expo.enterInt(); System.out.print("Enter years of experience ===>> "); experience = Expo.enterInt(); System.out.println(); if ( ! (education >= 16 || experience >= 5) ) System.out.println("You are hired!"); else System.out.println("You are not qualified."); System.out.println(); }

CRAZY BOSS

// Java1010.java // This program determines if an age is between 16 and 89. public class Java1010 { public static void main (String args[]) { System.out.println("Java1010\n"); int age; System.out.print("How old are you? ===>> "); age = Expo.enterInt(); System.out.println(); if (age >= 16 && age <= 89) System.out.println("You are the proper age to drive."); else System.out.println("You are not the proper age to drive."); System.out.println(); }

// Java1011.java // This program shows ranges, nested two-way selection, // and multi-way selection all at the same time. // This is how you have to do multi-way selection with real numbers. public class Java1011 { public static void main (String args[]) { System.out.println("Java1011\n"); double gpa; System.out.print("What is your gpa ===>> "); gpa = Expo.enterDouble(); System.out.println(); if (gpa >= 3.9) System.out.println("Summa Cum Laude"); else if (gpa >= 3.75) System.out.println("Magna Cum Laude"); else if (gpa >= 3.5) System.out.println("Cum Laude"); else if (gpa >= 2.0) System.out.println("Graduate without honors"); else System.out.println("Will not graduate"); System.out.println(); }

In the previous program many if…else statements were used to simulate multi-way selection. These statements are necessary because the switch statement has 2 limitations: a. switch does not work with ranges b. switch only works with ordinal data types An ordinal data type is one where every value has a definite next value and a definite previous value. int and char are ordinal data types. double and String are NOT. Why didn’t you use switch ?