Download presentation
Presentation is loading. Please wait.
1
FIT1002 2006 1 Objectives By the end of this lecture, students should: understand selection statements understand nested selection understand tradeoff between complex boolean expressions and nested selection be able to write simple code with conditional executions Reading: Savitch, Sec. 3.1 - 3.2
2
FIT1002 2006 2 The if statement Determines whether a statement or block is executed. Implements the selection instruction within an algorithm. Decides what to do by evaluating a Boolean expression. If the expression is true, the block is executed. if ( expression ) block
3
FIT1002 2006 3 Reminder: Blocks A block is either a single statements (ending with a semicolon ;) or A compound statement or block is a series of statements surrounded by braces. { x = x +1; System.out.println(x); } An empty statement is a single semicolon. E.g.
4
FIT1002 2006 4 Example: method oddtest (version 1) Test whether the instance variable “x” is odd and print a message if it is. public class Test { private int x=0; public void set_x(int val) { x=val; } public void oddtest() { if (x % 2 == 1) System.out.println("X is odd"); }
5
FIT1002 2006 5 public class Test { private int x=0; public void set_x(int val) { x=val; } public void oddtest() { if (x % 2 == 1) ; System.out.println("X is odd"); } Example: method oddtest (version 1) Test whether the instance variable “x” is odd and print a message if it is. Do not put a semicolon after rhe condition!
6
FIT1002 2006 6 Example: method oddtest (version 1) Test whether the instance variable “x” is odd and print a message if it is. Do not put “then” here either! public class Test { private int x=0; public void set_x(int val) { x=val; } public void oddtest() { if (x % 2 == 1) System.out.println("X is odd"); }
7
FIT1002 2006 7 Notes on if Which of the following code fragments are equivalent? A B C if (x % 2 == 1) { System.out.println(x); } System.out.println(” is odd"); if (x % 2 == 1) System.out.println(x); System.out.println(” is odd"); if (x % 2 == 1) { System.out.println(x); System.out.println(” is odd"); }
8
FIT1002 2006 8 Notes on if Which of the following code fragments are equivalent? A B C A Statement A Block if (x % 2 == 1) { System.out.println(x); } System.out.println(” is odd"); if (x % 2 == 1) System.out.println(x); System.out.println(” is odd"); if (x % 2 == 1) { System.out.println(x); System.out.println(” is odd"); }
9
FIT1002 2006 9 The else statement Can only occur after an if statement Is only executed when the if block does not execute if ( expression ) block1 else block2
10
FIT1002 2006 10 public class Test { private int x=0; public void set_x(int val) { x=val; } public void oddeventest() { if (x % 2 == 1) System.out.println("X is odd"); else System.out.println("X is even"); } Example: oddeventest (version 2) Test whether the instance variable “x” is odd or even and print an appropriate message in both cases.
11
FIT1002 2006 11 public class Test { private int x=0; public void set_x(int val) { x=val; } public boolean isodd() { if (x % 2 == 1) return true; else return false; } Example: isodd (version 1) Test whether the instance variable “x” is odd or even and return a boolean value indicating whether it is odd. if x is odd then return true else return false
12
FIT1002 2006 12 public class Test { private int x=0; public void set_x(int val) { x=val; } public boolean isodd() { return (x % 2 == 1) ; } Example: isodd (version 2) Since we are only selecting between two boolean values this can be written much more elegantly without selection. return the truth value of “x is odd” directly
13
FIT1002 2006 13 Boolean Expressions … are used as the selection condition. … Complex boolean expressions are formed with the boolean operators (also “logical” operators ) Constants: true, false Comparison Operators: Equality ( == ) Inequality ( != ) Comparison (, = ) Logical Operators: And ( && ) Or ( || ) Not ( ! )
14
FIT1002 2006 14 Equality True only if both arguments have identical values. 1 == 2 x == 0 (x+4) == 42 p1.x == p2.x Examples: not to be confused with assignment ( = )
15
FIT1002 2006 15 Inequality True only if arguments have different values. 1 != 2 x != 0 p.x != 42 p1.x != p2.x Examples:
16
FIT1002 2006 16 Comparison True only if the specified relationship holds 1 < 2 x > 1 p.x <= 42 p2.x+3 >= p2.x Examples:
17
FIT1002 2006 17 Not True only if the single Boolean argument is false. ! true ! (x<0) ! happy Examples:
18
FIT1002 2006 18 And True only if both Boolean arguments are true. (x > 0 && X < 100) healthy && wealthy && wise Examples:
19
FIT1002 2006 19 Or True if either Boolean argument is true (or both are true). ! (x 100) good || bad || ugly Examples:
20
FIT1002 2006 20 Equality True only if both arguments have identical values. 1 == 2 1 == 0 42 == 42 truth == beauty Examples:
21
FIT1002 2006 21 Precedence Highest to lowest: Brackets Not ( ! ) Comparison (, = ) Equality ( == ), Inequality ( != ) And ( && ) Or ( || ) Note: The assignment operator (=) is lower in order of precedence than Boolean / Logical operators. analyze: result = age >= 18 && (haveMoney || haveCard) && thirst > 0.3 && ! afterHours;
22
FIT1002 2006 22 public boolean inRange(int x) { if (0 < x < 99 ) { System.out.println(“X is in Range”); } public boolean inRange(int x) { if (x > 0 && x < 99 ) { System.out.println(“X is in Range”); } is incorrect. Instead use Common Mistake: Multiple Comparison
23
FIT1002 2006 23 Cascaded if statement Multiple alternative blocks each with a Boolean expression. First expression which evaluates to true causes execution of the associated block. At most only one block will be executed.
24
FIT1002 2006 24 Example: months Determine the number of days in a given month: 30 days hath September, April, June and November. All the rest hath 31, Excepting February alone, Which hath 28 days clear, And 29 in each leap year. public class Months { int September = 9; int April = 4; int June = 6; int November = 11; int February = 2; …
25
FIT1002 2006 25 Example: months public int month_length(int month) { if (month==September || month==April || month==June || month==November ) { return 30; } else if (month==February) { return 28; } // only for non-leap year else // at this point the following values for // month have been ruled out: // September, April, June, November, February { return 31; } } Note that the braces around the return statements are not really required (but they never hurt!)
26
FIT1002 2006 26 Nested If Nested If statements correspond to a logical conjunction of the conditions: The innermost statement will only be executed if all enclosing conditions are true. int x; … … if (x>0) if (x<100) System.out.println(“X is in the range 1…99”); … if (x>0) { if (x<1000) System.out.println(“x is a small positive number”); if (x>=1000) System.out.println(“x is a large positive number”); }
27
FIT1002 2006 27 Nested If versus Complex Conditions You can often avoid nested If statements by using compound boolean expressions. The following two have identical function: if (a <= b) if (x >= a) if (x <= b) System.out.println(“x is between”+a+” and “+b); else System.out.println(“x is outside the range “+a+” to “+b); if (b <= a) if (x >= b) if (x <= a) System.out.println(“x is between a and b”); else System.out.println(“x is outside the range “+a+” to “+b); if ((a = a && x <= b) || (b = b && x <= a)) System.out.println(“x is between”+a+” and “+b); else System.out.println(“x is outside the range “+a+” to “+b); Does the first code segment really work? Can you simplify further?
28
FIT1002 2006 28 Dangling Else Problem An “else” statement always belongs to the closest unmatched “if”. If you want it to be associated with another if you need to indicated block boundaries of unmatched “if”s with braces. if (a <= b) { if (x >= a) if (x <= b) System.out.println(“x is between”+a+” and “+b); else System.out.println(“x is outside the range “+a+” to “+b); } else if (x >= b) if (x <= a) System.out.println(“x is between a and b”); else System.out.println(“x is outside the range “+a+” to “+b); Braces indicate end of inner if
29
FIT1002 2006 29 Conditional Operator We can write an expression such that its value depends on a TRUTH value Condition ? Expr2 : Expr3 A ternary operator For example: int max, a, b; … max = (a>b) ? a : b ;
30
FIT1002 2006 30 Switch Statements The switch statement selects one of several execution branches. int level; … switch (level) { case 1: System.out.println(“Beginner”); break; case 2: System.out.println(“Intermediate”); break; case 3: System.out.println(“Advanced”); break; case 4: System.out.println(“Wizard”); break; default: system.out.println(“Unsupported Level”); }
31
FIT1002 2006 31 Switch Statements Switch searches through the cases until a value is found that matches the value of the test expression. This case will be executed. The break statement terminates the switch case and execution continues after the whole switch statement. int level; … switch (level) { case 1: System.out.println(“Beginner”); break; case 2: System.out.println(“Intermediate”); break; case 3: System.out.println(“Advanced”); break; case 4: System.out.println(“Wizard”); break; default: system.out.println(“Unsupported Level”); }
32
FIT1002 2006 32 Switch Statements int level; … switch (level) { case 1: System.out.println(“Beginner”); break; case 2: System.out.println(“Intermediate”); break; case 3: System.out.println(“Advanced”); break; case 4: System.out.println(“Wizard”); break; default: system.out.println(“Unsupported Level”); } The switch expression can be of type int, char, short or byte.
33
FIT1002 2006 33 Switch Statements int level; … switch (level) { case 1: System.out.println(“Beginner”); break; case 2: System.out.println(“Intermediate”); break; case 3: System.out.println(“Advanced”); break; case 4: System.out.println(“Wizard”); break; default: system.out.println(“Unsupported Level”); } The case labels must match the type of the switch expression.
34
FIT1002 2006 34 Switch Statements int level; … switch (level) { case 1: System.out.println(“Beginner”); break; case 2: System.out.println(“Intermediate”); break; case 3: System.out.println(“Advanced”); break; case 4: System.out.println(“Wizard”); break; default: system.out.println(“Unsupported Level”); } If you omit the break, the execution continues with the next case!
35
FIT1002 2006 35 Switch Statements int level; … switch (level) { case 1: System.out.println(“Beginner”); break; case 2: System.out.println(“Intermediate”); break; case 3: System.out.println(“Advanced”); break; case 4: System.out.println(“Wizard”); break; default: system.out.println(“Unsupported Level”); } A default case is not mandatory, but it is always safer to have one
36
FIT1002 2006 36 Switch Statements int level; … switch (level) { case 1: System.out.println(“Beginner”); break; case 2: System.out.println(“Intermediate”); break; case 3: System.out.println(“Advanced”); break; case 4: System.out.println(“Wizard”); break; default: system.out.println(“Unsupported Level”); } without a default case nothing will be executed if no case label matches.
37
FIT1002 2006 37 Equality of Objects What happens if you use “==“ to compare objects? You are not testing for the same field values, but for object identity! public class Test { int x=0; } … Test a = new Test(); Test b = new Test(); Test c = a; System.out.println(a==c); System.out.println(a==b); … prints “true” (the very same object)
38
FIT1002 2006 38 Equality of Objects What happens if you use “==“ to compare objects? You are not testing for the same field values, but for object identity! public class Test { int x=0; } … Test a = new Test(); Test b = new Test(); Test c = a; System.out.println(a==c); System.out.println(a==b); … prints “false” (different objects with same field values)
39
FIT1002 2006 39 Equality of Objects To test any other notion of equality that makes sense in your application (e.g. same field values) you need to write a test method yourself. By convention this should be a a method “equals” with a single parameter which is an object of the same class as “this” (see Java API) You are free to implement any notion of equality as long as it is transitive, refelxive and symmetric. public class Test { int x=0; public boolean equals(Test other) { return this.x=other.x; } … Test a = new Test(); Test b = new Test(); Test c = a; System.out.println(a.equals(b)); System.out.println(c.equals(a)); … both now print “true” (tested for same field values) You are effectively overriding the pre-exisiting default equals method. This is inherited from the class object and uses object identity (==)
40
FIT1002 2006 40 Equality of Strings The class String comes with a number of useful equality operators. The basic “equals” compares two String for identical contents. You can also use “equalsIgnoreCase”. (check the class String in the Java API definition at http://java.sun.com/reference/api/index.html to find other String methods) String s1=“Bernd Meyer”; String s2=“Bernd MEYER”; System.out.println(s1.equalsIgnoreCase(s2); …
41
FIT1002 2006 41 Exclusive OR True if “at most one” of two alternatives is true False if neither is true, also False if both are true! Define! A xor B : Do you need brackets ?
42
FIT1002 2006 42 Checking & Simplifying Boolean Expressions Use Truth Tables Transform Expressions using Boolean Algebra 000 101 011 110 ABA xor B 00 10 01 11 AB A && !B || B && !A
43
FIT1002 2006 43 Boolean Algebra --- History George Boole, 1815-1864 Initially self-guided studies of languages and philosphy With 16 assistant teacher at private school With 20 opened a school and taught himself mathematics Published in “Cambridge Mathematical Journal” with 24 The Mathematical Analysis of Logic was published in 1847 Casts logical reasoning in the form of algebra + is OR, * is AND 0 is FALSE, 1 is TRUE An Investigation of the Laws of Thought (1854) extends the algebra
44
FIT1002 2006 44 Axioms of Boolean Algebra In the following, “a b” means “is equivalent”. This means that you can re-write the form a (the left-hand side) into the form b (the right hand side) without changing the meaning. (and also the other way around) Commutative (A && B) (B && A) (A || B) (B || A) Associative A && (B && C) (A && B) && C A || (B || C) (A || B) || C
45
FIT1002 2006 45 Axioms of Boolean Algebra (cont) Distributive A && (B || C) (A && B) || (A && C) A || (B && C) (A || B) && (A || C) … plus some “technicalities”
46
FIT1002 2006 46 De Morgan’s Law From the axioms of Boolean algebra it follows that: ! ( A || B ) !A && !B !(A && B) !A || !B You can use all rules above to simplify / verify expressions. Exercise: simplify ! ( A || !B ) && A
47
FIT1002 2006 47 Short-circuiting A complex Boolean expression is only evaluated as far as necessary (from left to right) The test for y>20 will never be executed. This is important if one of the tests can cause a so-called side effect. int x = 99; int y = 30; x 20 Examples:
48
FIT1002 2006 48 Side Effects (Advanced, optional): A side effect is caused when an operation (eg a method call) causes a change to the state of the program other than just returning a value (for example, changing a variable, printing something…) public class Test { private final static int January = 1; … private int discount=0; private int month=January;
49
FIT1002 2006 49 Shortcircuits and Side Effects private boolean storeWideSale() { if (month==January) { discount=50; return true; } else return false; } private boolean onSale(int productNumber) { if (productNumber == 999) { discount=10; return true; } else return false; } public void sell(int productNumber) { if (onSale(productNumber) || storeWideSale() ) System.out.println(”You receive "+discount+" % discount"); } (Advanced, optional): With this code Item 999 will even in January only get 10% discount
50
FIT1002 2006 50 Shortcircuits and Side Effects public void sell(int productNumber) { boolean discounted = false; discounted = onSale(productNumber)|| discounted; discounted = storeWideSale() || discounted; if (discounted) System.out.println(”You receive "+discount+" % discount"); } (Advanced, optional): Better style: avoid side effects where possible always evaluate every statement that has side effects explicitly You can also avoid short-circuiting by using complete evaluation operators – or: “|” instead of “||” – and: “&” instead of “&&”
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.