Download - If Statement if (amount
![Page 1: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/1.jpg)
if Statement
if (amount <= balance) balance = balance - amount;
![Page 2: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/2.jpg)
if/else Statementif (amount <= balance)
balance = balance - amount;else
balance = balanceOVERDRAFT_PENALTY;
![Page 3: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/3.jpg)
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.
![Page 4: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/4.jpg)
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
![Page 5: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/5.jpg)
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’
![Page 6: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/6.jpg)
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.
![Page 7: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/7.jpg)
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);
![Page 8: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/8.jpg)
It prints:sqrt(2)squared minus 2 is not 0 but 4.440892098500626E-16
Don't use == to compare floating-point numbers
![Page 9: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/9.jpg)
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 ;
![Page 10: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/10.jpg)
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"))
![Page 11: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/11.jpg)
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"
![Page 12: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/12.jpg)
Lexicographic Comparison
![Page 13: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/13.jpg)
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)
![Page 14: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/14.jpg)
Object Comparison
![Page 15: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/15.jpg)
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 ""
![Page 16: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/16.jpg)
Multiple Alternatives
• if (condition1) statement1;else if (condition2) statement2;else if (condition3) statement3;else statement4;
• The first matching condition is executed.
• Order matters.
![Page 17: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/17.jpg)
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
![Page 18: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/18.jpg)
Nested Branches• Branch inside another branch if (condition1)
{ if (condition1a) statement1a; else statement1b;}else statement2;
![Page 19: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/19.jpg)
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.
![Page 20: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/20.jpg)
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
![Page 21: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/21.jpg)
Predicate Method
return type boolean
Example public boolean isOverdrawn(){
return balance < 0;
}
Use in conditionsif (myaccount.isOverdrawn())
statement;
![Page 22: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/22.jpg)
Boolean Operators
! not
&& and (short circuited)
|| or (short circuited)
A ! A
true false
false true
![Page 23: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/23.jpg)
Truth Tables
A B A && B
true true true
true false falsefalse Any false
A B A || B
true Any truefalse true truefalse false false
![Page 24: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/24.jpg)
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.
![Page 25: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/25.jpg)
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 iselse 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()
![Page 26: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/26.jpg)
Boolean Variables
boolean married;
married = input.equals("M"); //set to boolean value
if (married == true)
statement;
else
statement;
![Page 27: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/27.jpg)
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
![Page 28: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/28.jpg)
switch ( test expression ) {
case expression1:
statement1;
break; //to avoid fall thru
case expression2:
statement2;
break;
default:
default statement;
}
![Page 29: If Statement if (amount](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d555503460f94a325bc/html5/thumbnails/29.jpg)
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.