control flow for selection statements
play

Control Flow for Selection Statements } Suppose we run this code, - PowerPoint PPT Presentation

Control Flow for Selection Statements } Suppose we run this code, String s = button.getInput(); s = s.toLowerCase(); with our guess from button being TAILS , but the int flip = (int)( Math.random() * 2 + 1 ); random number comes up if (


  1. Control Flow for Selection Statements } Suppose we run this code, String s = button.getInput(); s = s.toLowerCase(); with our guess from button being “TAILS” , but the int flip = (int)( Math.random() * 2 + 1 ); random number comes up if ( flip == 1 ) { 0.1333789 … win.add( heads, 0 ); if ( s.equals( "heads" ) ) { Integer value 1 generated. 1. l2.setText( " You win!" ); } else if ( s.equals( "tails" ) ) { First if-else evaluated. 2. l2.setText( "You lose!" ); } Only code in the main 3. else { if -clause is executed. l2.setText( "Be serious..." ); } Class #10: } Interior if-else is 4. else { Selection, Logic, and Control, I evaluated. win.add( tails, 0 ); if ( s.equals( "tails" ) ) { Only code in first inner l2.setText( " You win!" ); 5. } else -clause will execute. else if ( s.equals( "heads" ) ) { Software Design I (CS 120): D. Mathias l2.setText( "You lose!" ); } else { l2.setText( "Be serious..."); } } 2 Software Design I (CS 120) Differences between if , else , and else-if Differences between if , else , and else-if } A set of instructions inside an } When we add an else -block, } When we add an else if to an } Again, adding an else -block, if -clause block may or may then exactly one set of if -clause block, we now have means exactly one set of multiple conditions , each of instructions must execute. not execute. instructions must execute. which may or may not execute. int i = (int)(Math.random() * 6) + 1; int i = (int)(Math.random() * 6) + 1; int i = (int)(Math.random() * 6) + 1; int i = (int)(Math.random() * 6) + 1; if ( (i % 2) == 0 ) if ( (i % 2) == 0 ) if ( i <= 2 ) if ( i <= 2 ) { { { { System.out.println( “Even” ); System.out.println( “Even” ); System.out.println( “Low” ); System.out.println( “Low” ); } } } } else else if ( i <= 4 ) else if ( i <= 4 ) { If i is even, then this prints “ Even ”. { { System.out.println( “Odd” ); System.out.println( “Medium” ); System.out.println( “Medium” ); } If i is odd, then nothing happens. } } else { If i is even, then this prints “ Even ”. If i is greater than 4 , nothing happens. System.out.println( “High” ); If i is odd (every other possible case), } it prints “ Odd ”. Now this code prints something for any legal value of i ( 1 to 6 ). 3 4 Software Design I (CS 120) Software Design I (CS 120) 1

  2. The boolean Primitive Type Using boolean Expressions } Java boolean variables/expressions are used for true or false values } When we use relational } Constant expressions: true , false operators (==, !=, etc.) } Binary infix operators: && (and), || (or) int i = 3; properly, we produce an } Unary prefix operator: ! (not) int j = 5; expression that is either if ( i <= j ) { Operator Precedence } As usual, to combine true or false System.out.println( i ); (highest to lowest) these with relational and } ++ -- mathematical operators, } Such an expression can be ! - (unary negation) we must remember used anywhere boolean int i = 3; * / % precedence order values could be used: int j = 5; + - (subtraction) } For a set of operations boolean b = ( i <= j ); < <= > >= } condition for an if -clause this complex, use of if ( b ) { == != } instantiation of some parentheses to mark System.out.println( i ); && boolean variable precedence is often highly } || recommended! } Anywhere else… = 5 6 Software Design I (CS 120) Software Design I (CS 120) Evaluating boolean Expressions Evaluating boolean Expressions } The simple numeric comparison } Sometimes we need to operators and equality relations negate a boolean expression are easy to evaluate P Q P && Q P Q !(P && Q) } T o determine whether a more that contains multiple T T T T T F complex logical formula is true or variables T F F T F T false, we use truth tables for logical operators F T F F T T F F F F F T P ! P P ! P P Q P || Q P Q !(P || Q) T F T F T T T T T F F T F T T F T T F F F T T F T F F F F F F T 7 8 Software Design I (CS 120) Software Design I (CS 120) 2

  3. Evaluating boolean Expressions Evaluating boolean Expressions } Sometimes we need to } Sometimes we need to negate a boolean expression negate a boolean expression that contains multiple that contains multiple variables variables P Q P && Q !(P && Q) !P || !Q P Q P || Q !(P || Q) !P && !Q T T T F F T T T F F T F F T T T F T F F F T F T T F T T F F F F F T T F F F T T 9 10 Software Design I (CS 120) Software Design I (CS 120) Evaluating boolean Expressions Review: Relations between Primitives } We can combine primitive types when we compare them } Sometimes we need to negate a boolean } We can use any relational operator, to produce a boolean value expression that contains multiple variables } Just like arithmetic, Java does an automatic widening of all types as needed so they are able to be compared meaningfully P Q R P || Q || R !(P || Q || R) !P && !Q && !R T T T T F F int i is widened to a double . T T F T F F Expression then evaluates to be true T F T T F F T F F T F F F T T T F F int i = 3; F T F T F F double j = 3.0; F F T T F F if ( i == j ) { F F F F T T System.out.println( “Equal” ); } 11 12 Software Design I (CS 120) Software Design I (CS 120) 3

  4. Relations between Reference Types Equality between Reference Types } Do not assume comparison operators work for non-primitive types For any reference (Class) type, to check whether the content of two objects is the } same, we must use an equals() method, not the basic equality operator } For any reference type (Class), basic comparison is not reliable Scanner scan = new Scanner( System.in ); Scanner scan = new Scanner( System.in ); String input = scan.next(); String input = scan.next(); String check = “Exit”; String check = “Exit”; if ( input.equals( check ) ) { if ( input == check ) { System.out.println( “Ciao!” ); System.out.println( “Goodbye” ); } } The equals() method for the String class actually checks whether or not they are identical, character-by-character . The == operator checks that two values are exactly the same . If a class has such a method, then we can compare it reliably. When writing our For reference types, this means that they refer to the same address in memory. own classes, if we want to be able to do this, then we must write our own method to do so. Here, no matter what the user types, this will be false , since the two String objects are created in two different ways in two different locations. Here, as long as the user types in the same word exactly, this will be true . 13 14 Software Design I (CS 120) Software Design I (CS 120) Comparison between Reference Types } Every primitive type can be compared using basic operators like < , <= , etc., This is not true for reference (Class) types } If such a type is supposed to be comparable, then it must have a method, compareTo() , that: } Takes another object of the same type as input 1. Returns an integer that is: 2. Zero (0) if the objects are equal a) Negative (less than 0) if the object calling the method comes before the input b) Positive (greater than 0) if the object calling the method comes after the input c) String s1 = “abacus”; String s2 = “barnacle”; Here, all of the if ( s1.compareTo( s2 ) < 0 ) { comparisons will be System.out.println( s1 + “ before “ + s2 ); true , since the two } String objects are else if ( s1.compareTo( s1 ) == 0 ) { compared using their System.out.println( s1 + “ equals “ + s1 ); proper method. } else if ( s2.compareTo( s1 ) > 0 ) { System.out.println( s2 + “ after “ + s1 ); } 15 Software Design I (CS 120) 4

Recommend


More recommend