Download presentation
Presentation is loading. Please wait.
Published byNickolas Goodman Modified over 9 years ago
1
Decisions
2
Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional operator (tests a boolean expression)
3
if Statement if(boolean expr.) statement [block] else if(boolean expr.) statement [block] else statement [block] if (a < 0) { // do something } else if (a == 0) { // do something else } else { // do something // in any case }
4
if Statement (cont.) One option: to do or not to do if (a < 0) a = 0; Two alternatives: one or the other if (a < 0) b = 0; else b = 1; Several alternatives: do one or do nothing if (a < 0) b = 0; else if (a < 1) b = 1; else if (a < 10) b = 2; // what if a = 12? Several alternatives with default (continues above structure) else b = 3;
5
Relational Operators Evaluate to a boolean (true/false) value <less than <=less than/equal >greater than >=greater than/equal ==equal !=not equal if (a < 10) if (a <= 10) if (a > b) if (a >= b) if (a == b * 13) if (a+b != c+d)
6
Boolean Operators combine boolean values b1 && b2 true if both b1 and b2 are true b1 || b2 true if either b1 or b2 is true b1 ^ b2 true if only b1 or b2 is true !b1 True if b1 is not true if (a > b && c != 7) if (a 10) if (a == 5 ^ b == 5) if (!person.isFemale())
7
Operator Precedence Arithmetic operators ++ -- * / % + - Relational operators == != >= Boolean operators ! ^ && || Example: double crLimit, crUsed; boolean sale, hasAcct; sale = cash > price || hasAcct && crLimit – crUsed > price; sale = (cash > price) || (hasAcct && ((crLimit – crUsed) > price));
8
Traps else pairs with nearest unmatched if if (a > 10) if (b > 12) statement1; else statement2; statement3; What will happen if a = 5? (statement2 will be executed only if a>10 and b <=12) if (a > 10) { if (b > 12) { statement1; } else { statement2; } statement3;
9
More Traps a = 0 is an assignment statement a == 0 is a relational expression if (a = 0) will generate a compiler error
10
More Traps floats and doubles cannot be compared exactly double x = 6.4, y = 8.6; if (x + y == 15.) will probably fail if (Math.abs (x + y – 15.) <.0001) will achieve the desired effect
11
Comparing Objects the problem String s1 = “Monday”; String s2 = KeyboardReader.readString(); if (s2 == s1) compares references, and will fail even if “Monday” is typed at the keyboard the solution if (s2.equals (s1)) compares the contents of two strings (or other object types) if (s2.equalsIgnoreCase (s1)) compares the contents of two strings without considering case
12
Comparing Objects (cont.) all objects define or inherit an equals() method (however, the equals() method inherited from Object merely compares the references with ==)
13
switch Statement switch (integer expression) { case value1: // do something break; case value2: // do something break; case value3: // do something break; default: // do something } int paycode = …; boolean exempt; switch (paycode) { case 2: exempt = false; break; case 4: case 6: case 7: exempt = true; break; default: System.out.println (“Bad paycode”); }
14
Data Types in Switches byte char short int not long case keyword must be followed by a constant: –a literal, e.g, case 5: –a final variable (see next slide)
15
Use of Final Variables in Switch Statements int gender = …; switch (gender) { case 1: … break; case 2: … break; } final int MALE = 1; final int FEMALE = 2; int gender = …; switch (gender) { case MALE: … break; case FEMALE: … break }
16
Traps Forgetting the break statement switch (paycode) { case 2: exempt = false; case 4: case 6: case 7: exempt = true; default: System.out.println (“Bad paycode”); }
17
Conditional Operator (boolean expr.) ? value if true : value if false The boolean expression is evaluated; –if true, the result of the operation is the expression following the ? – if false, the result of the operation is the expression following the : Example int nHats = …; String word=(nHats != 1)? ” hats” :” hat”; System.out.println (“I have “ + nHats + word);
18
Conditional Operator (cont.) This is the same as: int nHats = …; String word; if (nHats != 1) word = ” hats”; else word = ” hat”; System.out.println (“I have “ + nHats + word);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.