Presentation is loading. Please wait.

Presentation is loading. Please wait.

If Statement if (amount <= balance) balance = balance - amount;

Similar presentations


Presentation on theme: "If Statement if (amount <= balance) balance = balance - amount;"— Presentation transcript:

1 if Statement if (amount <= balance) balance = balance - amount;

2 if/else Statement if (amount <= balance) balance = balance - amount; else balance = balanceOVERDRAFT_PENALTY;

3 Syntax if (boolean expression) //use for 1 way decision statement; if (boolean expression) //use for 2 way decision statement1; else statement2; Boolean expression is an expression which evaluates to true or false Only 1 statement allowed where statement indicated.

4 Relational Operators evaluates to true if …. a < b a is less than b a > b a is greater than b a <= b a is less than or equal to b a >= b a is greater than or equal to b a != b a is not equal to b a == b a is equal to b

5 Block Statement if (amount <= balance) { double newBalance = balance - amount; balance = newBalance; } Note: block allows more than one ‘statement’ to be combined, to form a new ‘statement’

6 Equality Testing vs. Assignment  The = = operator tests for equality: if (x = = 0).. // if x equals zero  The = operator assigns a value to a variable: x = 0; // assign 0 to x  Don't confuse them.

7 Comparing Floating-Point Numbers  Consider this code: double r = Math.sqrt(2); double d = r * r -2; if (d == 0) System.out.println( "sqrt(2)squared minus 2 is 0"); else System.out.println( "sqrt(2)squared minus 2 is not 0 but " + d);

8  It prints: sqrt(2)squared minus 2 is not 0 but 4.440892098500626E-16  Don't use == to compare floating-point numbers

9 Comparing Floating-Point Numbers  Two numbers are close to another if |x - y| <= ε  ε is a small number such as 10 -14  Example: if ( Math.abs(x-y) <.0000000000001) statement ;

10 String Comparison Don't use = = for strings! if (input = = "Y") // WRONG!!!  Use equals method: if (input.equals("Y"))  = = tests identity, equals tests equal contents  Case insensitive test ("Y" or "y") if (input.equalsIgnoreCase("Y"))

11 Lexicographic Comparison  if s and t are strings, if (s < t) also does not make sense  s.compareTo(t) < 0 means: s comes before t in the dictionary  "car"comes before "cargo" "cargo" comes before "cathode"  All uppercase letters come before lowercase: "Hello" comes before "car"

12 Lexicographic Comparison

13 Object Comparison  = = tests for identical object  equals for identical object content  Rectangle cerealBox = new Rectangle(5, 10, 20, 30); Rectangle oatmealBox = new Rectangle(5, 10, 20, 30); Rectangle r = cerealBox;  cerealBox != oatmealBox, but cerealBox.equals(oatmealBox)  cerealBox == r Caveat: equals must be defined for the class (chap 11)

14 Object Comparison

15 The null Reference  null reference refers to no object at all  Can be used in tests: if (account == null)...  Use ==, not equals, to test for null  showInputDialog returns null if the user hit the cancel button: String input = JOptionPane.showInputDialog("..."); if (input != null) {... }  null is not the same as the empty string ""

16 Multiple Alternatives if (condition1) statement1; else if (condition2) statement2; else if (condition3) statement3; else statement4; The first matching condition is executed. Order matters.

17 Write a method which accepts a double value (a grade average), and returns the letter grade which would be awarded. Assume 90 and above -- an A 80 and under 90 -- a B 70 and under 80 -- a C 65 and under 70 -- a D under 65 -- an F

18 Nested Branches Branch inside another branch if (condition1) { if (condition1a) statement1a; else statement1b; } else statement2;

19 Write a method accepts a grade point average parameter, and returns the String “Congratulations” if the GPA is 3.5 and above, but returns “Sorry” if the GPA is below 2.0. The method will return “Provide References” is the GPA does not fall into either of the above two categories.

20 The boolean Type  George Boole (1815-1864): pioneer in the study of logic  value of expression amount < 1000 is true or false.  boolean type: set of 2 values, true and false

21 Predicate Method  return type boolean  Example public boolean isOverdrawn(){ return balance < 0; }  Use in conditions if (myaccount.isOverdrawn()) statement;

22 Boolean Operators  ! not  && and (short circuited)  || or (short circuited)

23 Truth Tables

24 Write a method called ‘even’ which accepts one int parameter and returns true if that value is even. Otherwise false is returned. Write a method which accepts three int parameters and returns the largest of the three. Write an application which asks the user for 3 int values and outputs the parity (even or odd) of the sum of the three, and the largest.

25 private boolean FirstLetterIsAVowel() { char first = word.charAt(0); return (first==‘a’ || first==‘e’ || first==‘i’|| first == ‘o’ || first == ‘u’ || first == ‘y’ || first == ‘A’ || first == ‘E’ || first == ‘I’ || first == ‘O’ || first == ‘U’ || first == ‘Y’); } Problem: For the Pig Latin algorithm on the right, write a method that tests if first letter is vowel. if (the first letter of word is a vowel) return word as it is else modify word and return it private boolean FirstLetterIsAVowel() { char first = word.charAt(0); if (first == ‘a’ || first == ‘e’ || first == ‘i’ || first == ‘o’ || first == ‘u’ || first == ‘y’ || first == ‘A’ || first == ‘E’ || first == ‘I’ || first == ‘O’ || first == ‘U’ || first == ‘Y’) return true; else return false; } First variant of the test Second variant of the test private boolean FirstLetterIsAVowel(){ string vowels = “aeiouyAEIOUY”; char first = word.charAt(0); return (vowels.indexOf(first) > -1); } Third variant of the test public void translateWord() { if (FirstLetterIsAVowel() ) return (word); else return(word.substring(1) + word.charAt(0) + “ay”); } Method translateWord()

26 Boolean Variables boolean married; married = input.equals("M"); //set to boolean value if (married == true) statement; else statement;

27 Switch statement switch ( test expression ) { case expression1: statement1; case expression2: statement2; ….. default: default statement; } ** matches and executes down ** expressions must be of primitive non-decimal type

28 switch ( test expression ) { case expression1: statement1; break; //to avoid fall thru case expression2: statement2; break; default: default statement; }

29 Write a method called “twelveDays” which accepts one in parameter, (a value which will be between 1 and 12 inclusive), and returns a string containing the item given on that day in the song “The Twelve Days of Christmas”. Write an application which prints out the lyrics to this song, using the method twelveDays.


Download ppt "If Statement if (amount <= balance) balance = balance - amount;"

Similar presentations


Ads by Google