More Boolean Expressions and If Statements Flags Flags
More Boolean Expressions and If Statements
Flags
Flags boolean on. Green = false; Monitor some condition Boolean variable if (distance. To. Hole < 30) on. Green = true; Can be set And later tested if (on. Green) System. out. println("Get out your putter!");
Comparing Characters
Comparing Characters < > <= >= == != char ch = ‘C’; Characters can be compared using relational operators 000001000011 Unicode is stored as a 16 bit number
Comparing Characters < > <= >= == != Characters can be compared using relational operators char ch = ‘C’; 000001000011 Unicode is stored as a 16 bit number ‘ 0’ … ‘ 9’ ‘? ’ ‘A’ … ‘Z’ ‘a’ … ‘z’ Characters are ordinal -- they have an order char c = ′A′; if(c < ′Z′) System. out. println("A is less than Z");
Comparing Strings
String name 1, name 2; name 2 name 1 “Sally” if (name 1 == name 2) System. out. println(“The names are equal”); Can’t compare strings with ==
String name 1, name 2; name 2 name 1 “Sally” if (name 1. equals(name 2)) System. out. println(“The names are equal”); Must use equals method
String name 1, name 2; name 2 name 1 “Sally” “SALLY” if (name 1. equals. Ignore. Case(name 2)) System. out. println(“The names are equal”);
Logical Operators
Logical Operators Operator Meaning && AND || OR ! NOT Effect Both expressions must be true for the overall expression to be true. One or both expressions must be true for the overall expression to be true. The ! operator reverses the truth of a boolean expression.
AND && Expression 1 Expression 2 true false true Expression 1 && Expression 2
OR || Expression 1 Expression 2 true false true Expression 1 || Expression 2
NOT Expression 1 true false ! !Expression 1
Precedence
Order of Precedence 1 Operators Description (unary negation) ! Unary negation, logical NOT 2 * / % 3 + - 4 < > <= >= 5 == != 6 && Logical AND 7 || Logical OR Multiplication, Division, Modulus Addition, Subtraction Less-than, Greater-than, Less-than or equal to, Greater-than or equal to Is equal to, Is not equal to
Practice
Logical Operators Evaluate the following expressions given these values for Boolean variables x, y, and z: boolean x = true, y = false, z = true; Expression Result x && y || x && z _____ (x || !y) && (!x || z) _____ x || y && z _____ !(x || y) && z _____ PI – Boolean Logic 1 -3
Boolean Expressions Evaluate the following expressions given these variables: int count = 0, limit = 10, x = 5, y = 7; char midterm. Grade = ‘A’; Expression Result count == 0 && limit < 20 _____ limit > 20 || count < 5 _____ !(midterm. Grade != ‘C’) _____ (count < 10) || (x < y) _____
Boolean Expressions (cont. ) Evaluate the following expressions given these variables: int count = 0, limit = 10, x = 5, y = 7; char midterm. Grade = ‘A’; Expression Result !(((count < 10) || (x < y)) && (count >= 0)) _____ ((limit/count) > 7) || (limit < 20) _____ ((midterm. Grade == ‘A’) || ((limit/count) < 20) _____ (limit < 0) && ((limit/count) > 7) _____ PI – Boolean Exp & Equiv. if
Write an if statement to calculate tax (total. Sales times TAXRATE) if the user input for. Profit is ‘Y’ or ‘y’.
Write an if statement to calculate tax (total. Sales times TAXRATE) if the user input response is “yes” (be sure to ignore case).
Write an if statement to print “Play golf!” if the temperature is between 65 and 80.
Reference: Tables and flowcharts adapted from Starting Out with Java by T. Gaddis Chapter 3 Slides.
- Slides: 25