© 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”

Slides:



Advertisements
Similar presentations
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
Advertisements

© 2006 Pearson Education Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
1 Chapter 3: Program Statements Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Comparing Data Comparing More than Numbers. Comparing Data When comparing data using boolean expressions, it's important to understand the nuances of.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Java Program Statements Selim Aksoy Bilkent University Department of Computer Engineering
Logical Operators and Conditional statements
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 3: Program Statements
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Chapter 3: Program Statements
Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2012 Pearson Education, Inc. Lab 8: IF statement …. Conditionals and Loops.
CSC 1051 M.A. Papalaskari, Villanova University Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
Control statements Mostafa Abdallah
Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science.
© 2006 Pearson Education Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
CprE 185: Intro to Problem Solving (using C)
Boolean Expressions and If
Boolean Expressions & the ‘if’ Statement
Logical Operators & Truth Tables.
Chapter 3: Program Statements
The ‘while’ Statement September 27, 2006
Outline Software Development Activities
CprE 185: Intro to Problem Solving (using C)
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
Boolean Expressions & the ‘if’ Statement
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
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:

© 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” program. C3 D3

© 2006 Pearson Education 2 Logical Operators  Boolean expressions can use the following logical operators: ! Logical NOT && Logical AND || Logical OR  They all take boolean operands and produce boolean results

© 2006 Pearson Education 3 Logical NOT  The logical NOT operation is also called logical negation or logical complement  If some boolean condition a is true, then !a is false; if a is false, then !a is true  Logical expressions can be shown using truth tables a!a truefalse true

© 2006 Pearson Education 4 Logical AND and Logical OR  The logical AND expression a && b is true if both a and b are true, and false otherwise  The logical OR expression a || b is true if a or b or both are true, and false otherwise

© 2006 Pearson Education 5 Truth Tables  A truth table shows the possible true/false combinations of the terms  Since && and || each have two operands, there are four possible combinations of conditions a and b aba && ba || b true false true falsetruefalsetrue false

© 2006 Pearson Education 6 Logical Operators  Conditions can use logical operators to form complex expressions if (total < MAX+5 && !found) System.out.println ("Processing…");  Logical operators have precedence relationships among themselves and with other operators all logical operators have lower precedence than the relational or arithmetic operators logical NOT has higher precedence than logical AND and logical OR

© 2006 Pearson Education 7 Short Circuited Operators  The processing of logical AND and logical OR is “short-circuited”  If the left operand is sufficient to determine the result, the right operand is not evaluated if (count != 0 && total/count > MAX) System.out.println ("Testing…");  If count != 0 was false, then the whole thing would be false so it was not necessary to check total/count > MAX  For | | if the left side is true, then done checking.

© 2006 Pearson Education 8 Truth Tables  Specific expressions can be evaluated using truth tables total < MAXfound!foundtotal < MAX && !found false truefalse truefalse truefalsetrue false

© 2006 Pearson Education 9 Comparing Characters  We can use the relational operators on character data  The following condition is true because the character + comes before the character J in the Unicode character set: if ('+' < 'J') System.out.println ("+ is less than J");  The uppercase alphabet (A-Z) followed by the lowercase alphabet (a-z) appear in alphabetical order in the Unicode character set (See Appendix B on p.624)

© 2006 Pearson Education 10 Comparing Strings  Remember that a character string in Java is an object  We cannot use the relational operators to compare strings  The equals method can be called with strings to determine if two strings contain exactly the same characters in the same order  The String class also contains a method called compareTo to determine if one string comes before another (based on the Unicode character set)

© 2006 Pearson Education 11 Lexicographic Ordering  Because comparing characters and strings is based on a character set, it is called a lexicographic ordering  The string "Great" comes before the string "fantastic" because all of the uppercase letters come before all of the lowercase letters in Unicode  Also, short strings come before longer strings with the same prefix (lexicographically)  Therefore "book" comes before "bookcase"

© 2006 Pearson Education 12 Comparing Float Values  We also have to be careful when comparing two floating point values ( float or double ) for equality  You should rarely use the equality operator ( == ) when comparing two floats  In many situations, you might consider two floating point numbers to be "close enough" even if they aren't exactly equal  Therefore, to determine the equality of two floats, you may want to use the following technique: if (Math.abs(f1 - f2) < ) System.out.println ("Essentially equal.");

© 2006 Pearson Education 13 Questions  What is the difference between a unary operator and a binary operator? Give an example of each.  Unary operates on one operand (ex: NOT) binary operates on two operands (ex: AND and OR).  If !d is false, what is true? dd

© 2006 Pearson Education 14  Which of the following are true: it is raining && today is Tuesday it is raining | | today is Tuesday  If a && b is true, is a | | b also true?  Yes, always.  If a | | b is true, is a && b also true?  Only sometimes

© 2006 Pearson Education 15  If a && b is false, is a | | b also false?  Only sometimes.  If a | | b is false, is a && b also false?  Yes, always.  If time: do p.181 #3.2 and 3.3