The boolean type and boolean operators

Slides:



Advertisements
Similar presentations
Logic & program control part 2: Simple selection structures.
Advertisements

3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
Chapter 6 Horstmann Programs that make decisions: the IF command.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
IT253: Computer Organization Lecture 7: Logic and Gates: Digital Design Tonga Institute of Higher Education.
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
Primitive Variables.
Chapter 5 Selection Statements Mr. Dave Clausen La Cañada High School.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Operators.
Decision Statements, Short- Circuit Evaluation, Errors.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
CS 139 – Programming Fundamentals Lecture 08A. Relational/comparison Operators Relational Operator Meaning > is greater than < is less than >= is greater.
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
IF Statements flowcharts and pseudocode Please open the speaker notes - they contain additional information!
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
APS105 Deciding 1. Comparing 2 Relational Expressions Relational expression: –Compare two values (or values of variables) Examples: x > 5 income < expenses.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Relational Operator and Operations
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Computer Science 210 Computer Organization
Conditional or Decision Logic
Lecture 3 Java Operators.
Boolean expressions and if-else statements
Making Choices with if Statements
OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS
Sequence, Selection, Iteration The IF Statement
Section 7.1 Logical Operators
More important details More fun Part 3
Data Types, Identifiers, and Expressions
ARTIFICIAL INTELLIGENCE
Intro to Logic Logical statements evaluate to one of two things:
Conditionals & Boolean Expressions
Propositional Calculus: Boolean Algebra and Simplification
OPERATORS (2) CSC 111.
Logical Operators & Truth Tables.
Data Types, Identifiers, and Expressions
Computers & Programming Languages
Relational Operators Operator Meaning < Less than > Greater than
Decision Structure - 2 ISYS 350.
Computer Science 210 Computer Organization
Decision Structure - 2 ISYS 350.
TRUTH TABLES.
Decision Structure - 2 ISYS 350.
MATLAB Logical Expressions
Conditional Logic Presentation Name Course Name
The System.exit() Method
Control Structure Chapter 3.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
SSEA Computer Science: Track A
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
CS2011 Introduction to Programming I Selections (I)
CprE 185: Intro to Problem Solving (using C)
Control Structure.
Boolean Expressions September 1, 2019 ICS102: The course.
Chapter 5 Selection Statements
boolean Expressions Relational, Equality, and Logical Operators
Presentation transcript:

The boolean type and boolean operators Recall that Java provides a data type boolean which can take on only one of two values: true or false. boolean b = true; // stores the truth value true in b b = false; // overwrites b with the value false There are other ways to create boolean values and assign them into boolean variables besides a simplistic direct assignment of a boolean literal into a variable. Boolean operators produce true/false values. For example let’s assume this declaration: int i = 10; We can assign a truth value into variable b using boolean operators like this: b = i < 20; The expression i < 20 is true since I contains the number 10. The value true is then assigned into the variable b. Let’s look at a summary of all the boolean operators and their behavior.

Boolean operators || && && is logical and true false || is logical or true true false false false false || true true true false true false ! false true and -- ALL conditions must be true or -- ANY condition can be true && is logical and || is logical or ! In logical negation (pronounced not) true and true true and false false and true false and false true or true true or false false or true false or false

boolean variables int age = 21; foo = true; boolean variables can have the value true or false. That’s it. boolean minor, foo; int age = 21; foo = true; minor = ( age < 18 ); // (age<18) produces either true or false What value is now in the variable minor?

Relational operators Relational operators produce boolean values == equality != inequality < less than <= less than or equal > greater than >= greater than or equal Relational operators have higher priority then boolean operators x < y && a > b evaluated as if  (x<y) && (a>b) Not a bad idea to parenthesis just for emphasis/clarity

Short circuiting practice Short-circuiting happens when the result can be determined before the entire expression has been examined What do each of the following boolean expressions evaluate to ? Which of the following expressions short circuit? boolean a = true, b = false; int c = 6, d = 5; a && (!b) a && b b && a b && (!a) a || d < c d != 10 || b b || c == 6 d > 10 || b

More short-circuiting practice boolean a = true, b = false; int c = 6, d = 5; b && (b || c < d) (c < d) || b a && ((! b) || (c < d)) a || b !a !(a || b) !((c > d) && a) (!b && a) || (a && d < c)

not has a higher precedence than and/or DeMorgan’s Law DeMorgans Law - any expression can be equivalently expressed by multiplying a NOT through the boolean expression and changing || to && or changing && to || The negation of a conjunction is the disjunction of the negations !(p && q)  !p || !q The negation of a disjunction is the conjunction of the negations !(p || q)  !p && !q

and/or examples and examples if (age > 6 && age < 19 ) { System.out.println(“You should be in school!”); } if ( age < 18 && milesOverLimit > 20) System.out.println(“Underage flagrant speeders get double fine!”); fine *= 2; or examples if ( letter == ‘A’|| letter == ‘B’ || letter == ‘C’ ) RIGHT if (letter == ‘A’|| ‘B’ || ‘C’ ) WRONG

More forms of the if statement Simple conditional : use if if ( age < 21 ) { System.out.println(“too young to drink :=( ”); } Two way branch: use if else if ( age < 18 ) else System.out.println(“Draft or bottle?”);

three way branch - use an if else/if else if ( age < 18 ) { System three way branch - use an if else/if else if ( age < 18 ) { System.out.println(“too young to drink :=( ”); } else if ( age < 70 ) System.out.println(“Draft or bottle?”); else System.out.println(“How about some Geritol instead?”);

Good usage of the if test You may have your if structured like this: if ( <boolean expression here>) { // nothing in the if part } else do something In that case negate the test and put the action under the if instead of the else if ( !<boolean expression here> ) Now you don’t need the else