CSCI 144 - Introduction to Computer Science
Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 2018
1
CSCI 144 - Introduction to Computer Science Instructor: John - - PowerPoint PPT Presentation
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
Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 2018
1
if (distanceToHole < 30)
boolean onGreen = false; if (onGreen) System.out.println("Get out your putter!");
Boolean variable Monitor some condition Can be set And later tested
< > <= >= == != Characters can be compared
using relational operators
0000000001000011 char ch = ‘C’;
Unicode is stored as a 16 bit number
char c = ′A′; if(c < ′Z′) System.out.println("A is less than Z"); < > <= >= == != Characters can be compared
using relational operators
0000000001000011 char ch = ‘C’;
Unicode is stored as a 16 bit number ‘0’ … ‘9’ ‘?’ ‘A’ … ‘Z’ ‘a’ … ‘z’ Characters are ordinal -- they have an order
Character Unicode 'A' 65 'B' 66 'C' 67 'D' 68 ... ... 'Z' 90 Character Unicode 'a' 97 'b' 98 'c' 99 'd' 100 ... ... 'z' 122
“Sally”
name1 String name1, name2;
“Sally”
name2 if (name1 == name2) System.out.println(“The names are equal”);
Can’t compare strings with ==
“Sally”
name1 String name1, name2;
“Sally”
name2 if (name1.equals(name2)) System.out.println(“The names are equal”);
Must use equals method
“Sally”
name1 String name1, name2;
“SALLY”
name2 if (name1.equalsIgnoreCase(name2)) System.out.println(“The names are equal”);
Operator Meaning Effect
&& AND Both expressions must be true for the overall expression to be true. || OR One or both expressions must be true for the overall expression to be true. ! NOT The ! operator reverses the truth of a boolean expression.
Expression 1 Expression 2 Expression1 && Expression2 true false false true false false true true
Expression 1 Expression 2 Expression1 || Expression2 true false false true false false true true
Expression 1 !Expression1 true false
Order of Precedence Operators Description
1 (unary negation) ! Unary negation, logical NOT 2 * / % Multiplication, Division, Modulus 3 + - Addition, Subtraction 4 < > <= >= Less-than, Greater-than, Less-than or equal to, Greater-than or equal to 5 == != Is equal to, Is not equal to 6 && Logical AND 7 || Logical OR
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 __________
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) __________
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; }
Reference: Tables and flowcharts adapted from Starting Out with Java by T. Gaddis Chapter 3 Slides.