Boolean Expressions September 1, 2019 ICS102: The course.

Slides:



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

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Chapter 3 Flow of Control Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
Comp 248 Introduction to Programming Chapter 3 – Flow of Control Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control.
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 3 Boolean Expressions Section 3.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control.
Flow of Control Joe McCarthy CSS 161: Fundamentals of Computing1.
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 Type Called bool  Bool has only two possible values: True and False.
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
CompSci 230 S Programming Techniques
Chapter 3 Flow of Control
Chapter 3 Flow of Control
Java Programming Fifth Edition
Boolean expressions and if-else statements
Java Primer 1: Types, Classes and Operators
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 3 Edited by JJ Shepherd
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 3 Branching Statements
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
The Selection Structure
Primitive and Reference Data Values
Logical Operators & Truth Tables.
Data Types, Identifiers, and Expressions
Chapter 8 JavaScript: Control Statements, Part 2
CMSC 202 Java Primer 2.
Expressions and Assignment
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3 Flow of Control
The Data Element.
Chapter 3: Selection Structures: Making Decisions
The Data Element.
Chap 7. Advanced Control Statements in Java
Operator King Saud University
CSS161: Fundamentals of Computing
Controlling Program Flow
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Boolean Expressions September 1, 2019 ICS102: The course

Outline Introduction Java Comparison Operators Evaluating Boolean Expressions Pitfall: Using == with Strings Lexicographic and Alphabetical Order Building Boolean Expressions Truth Tables Short-Circuit and Complete Evaluation Precedence and Associativity Rules Evaluating Expressions Rules for Evaluating Expressions September 1, 2019 ICS102: The course

- Introduction A Boolean expression is an expression that is either true or false The simplest Boolean expressions compare the value of two expressions time < limit yourScore == myScore Note that Java uses two equal signs (==) to perform equality testing: A single equal sign (=) is used only for assignment [Deleted from the Slide] A Boolean expression does not need to be enclosed in parentheses, unless it is used in an if-else statement September 1, 2019 ICS102: The course

- Java Comparison Operators Examples testScore < 80 testScore * 2 >= 350 30 < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius <= 359.99 September 1, 2019 ICS102: The course

- Evaluating Boolean Expressions Even though Boolean expressions are used to control branch and loop statements, Boolean expressions can exist independently as well A Boolean variable can be given the value of a Boolean expression by using an assignment statement A Boolean expression can be evaluated in the same way that an arithmetic expression is evaluated The only difference is that arithmetic expressions produce a number as a result, while Boolean expressions produce either true or false as their result boolean madeIt = (time < limit) && (limit < max); September 1, 2019 ICS102: The course

- Pitfall: Using == with Strings The equality comparison operator (==) can correctly test two values of a primitive type However, when applied to two objects such as objects of the String class, == tests to see if they are stored in the same memory location, not whether or not they have the same value In order to test two strings to see if they have equal values, use the method equals, or equalsIgnoreCase string1.equals(string2) string1.equalsIgnoreCase(string2) September 1, 2019 ICS102: The course

- Lexicographic and Alphabetical Order Lexicographic ordering is the same as ASCII ordering, and includes letters, numbers, and other characters All uppercase letters are in alphabetic order, and all lowercase letters are in alphabetic order, but all uppercase letters come before lowercase letters If s1 and s2 are two variables of type String that have been given String values, then s1.compareTo(s2) returns: A negative number if s1 is before s2 in lexicographic ordering zero if the two strings are equal. A positive number if s2 comes before s1 When performing an alphabetic comparison of strings (rather than a lexicographic comparison) that consist of a mix of lowercase and uppercase letters, use the compareToIgnoreCase method instead September 1, 2019 ICS102: The course

- Building Boolean Expressions When two Boolean expressions are combined using the "and" (&&) operator, the entire expression is true provided both expressions are true Otherwise the expression is false When two Boolean expressions are combined using the "or" (||) operator, the entire expression is true as long as one of the expressions is true The expression is false only if both expressions are false Any Boolean expression can be negated using the ! Operator Place the expression in parentheses and place the ! operator in front of it Unlike mathematical notation, strings of inequalities must be joined by && Use (min < result) && (result < max) rather than min < result < max September 1, 2019 ICS102: The course

- Truth Tables September 1, 2019 ICS102: The course

- Short-Circuit and Complete Evaluation … Consider x > y || x > z The expression is evaluated left to right. If x > y is true, then there’s no need to evaluate x > z because the whole expression will be true whether x > z is true or not. This also happens when we use && operator and the first expression is false. To stop the evaluation once the result of the whole expression is known is called short-circuit evaluation or lazy evaluation September 1, 2019 ICS102: The course

… - Short-Circuit and Complete Evaluation What would happen if the short-circuit evaluation is not done for the following expression? kids != 0 && toys/kids >= 2 There are times when using short-circuit evaluation can prevent a runtime error Sometimes it is preferable to always evaluate both expressions, i.e., request complete evaluation In this case, use the & and | operators instead of && and || September 1, 2019 ICS102: The course

- Precedence and Associativity Rules … Boolean and arithmetic expressions need not be fully parenthesized If some or all of the parentheses are omitted, Java will follow precedence and associativity rules (summarized in the following table) to determine the order of operations If one operator occurs higher in the table than another, it has higher precedence, and is grouped with its operands before the operator of lower precedence If two operators have the same precedence, then associativity rules determine which is grouped first September 1, 2019 ICS102: The course

… - Precedence and Associativity Rules September 1, 2019 ICS102: The course

- Evaluating Expressions In general, parentheses in an expression help to document the programmer's intent Instead of relying on precedence and associativity rules, it is best to include most parentheses, except where the intended meaning is obvious Binding: The association of operands with their operators A fully parenthesized expression accomplishes binding for all the operators in an expression Side Effects: When, in addition to returning a value, an expression changes something, such as the value of a variable The assignment, increment, and decrement operators all produce side effects September 1, 2019 ICS102: The course

- Rules for Evaluating Expressions Perform binding Determine the equivalent fully parenthesized expression using the precedence and associativity rules Proceeding left to right, evaluate whatever subexpressions can be immediately evaluated These subexpressions will be operands or method arguments, e.g., numeric constants or variables Evaluate each outer operation and method invocation as soon as all of its operands (i.e., arguments) have been evaluated September 1, 2019 ICS102: The course

THE END September 1, 2019 ICS102: The course