1 CSCI 144 - Introduction to Computer Science Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 2018
More Boolean Expressions and If Statements
Flags
Flags boolean onGreen = false; Monitor some condition Boolean variable if (distanceToHole < 30) onGreen = true; Can be set And later tested if (onGreen) System.out.println("Get out your putter!");
Comparing Characters
Comparing Characters < > <= >= == != Characters can be compared using relational operators char ch = ‘C’; 0000000001000011 Unicode is stored as a 16 bit number
Comparing Characters < > <= >= == != Characters can be compared using relational operators char ch = ‘C’; 0000000001000011 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");
Character Unicode Character Unicode 'A' 'a' 65 97 'B' 'b' 66 98 'C' 'c' 67 99 'D' 'd' 68 100 ... ... ... ... 'Z' 'z' 90 122
Comparing Strings
String name1, name2; name1 name2 “Sally” “Sally” if (name1 == name2) System.out.println(“The names are equal”); Can’t compare strings with ==
String name1, name2; name1 name2 “Sally” “Sally” if (name1.equals(name2)) System.out.println (“The names are equal”); Must use equals method
String name1, name2; name1 name2 “Sally” “SALLY” if (name1.equalsIgnoreCase(name2)) System.out.println (“The names are equal”);
Logical Operators
Logical Operators Operator Meaning Effect Both expressions must be true for the overall && AND expression to be true. One or both expressions must be true for the overall || OR expression to be true. The ! operator reverses the truth of a boolean ! NOT expression.
AND && Expression 1 Expression 2 Expression1 && Expression2 true false false true false false true true
OR || Expression 1 Expression 2 Expression1 || Expression2 true false false true false false true true
NOT ! Expression 1 !Expression1 true false
Precedence
Order of Operators Description Precedence (unary negation) ! Unary negation, logical NOT 1 * / % 2 Multiplication, Division, Modulus + - 3 Addition, Subtraction Less-than, Greater-than, Less-than or < > <= >= 4 equal to, Greater-than or equal to == != 5 Is equal to, Is not equal to && 6 Logical AND || 7 Logical OR
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 __________
Boolean Expressions Evaluate the following expressions given these variables: int count = 0, limit = 10, x = 5, y = 7; char midtermGrade = ‘A’; Expression Result count == 0 && limit < 20 __________ limit > 20 || count < 5 __________ !(midtermGrade != ‘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 midtermGrade = ‘A’; Expression Result !(((count < 10) || (x < y)) && (count >= 0)) __________ ((limit/count) > 7) || (limit < 20) __________ ((midtermGrade == ‘A’) || ((limit/count) < 20) __________ (limit < 0) && ((limit/count) > 7) __________
Write an if statement to calculate tax (totalSales times TAXRATE) if the user input forProfit is ‘Y’ or ‘y’. if (forProfit == ‘Y’ || forProfit == ‘y’ ) { tax = totalsales * TAXRATE; }
Write an if statement to calculate tax (totalSales times TAXRATE) if the user input response is “yes” (be sure to ignore case). if (response.equalsIgnoreCase( “yes” )) { tax = totalsales * TAXRATE; }
Write an JAVA program, scan user’s input as temperature and mood, write if statement to print “Play golf!” if the temperature is between 65 and 80 , and mood is “happy”. For all other cases, print ‘Programming at home’. (hint: consider ‘flags’)
Reference: Tables and flowcharts adapted from Starting Out with Java by T. Gaddis Chapter 3 Slides.
Recommend
More recommend