Documente Academic
Documente Profesional
Documente Cultură
if/else Statement
if (amount <= balance)
balance = balance - amount;
else
balance = balanceOVERDRAFT_PENALTY;
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
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);
It prints:
sqrt(2)squared minus 2 is not 0 but
4.440892098500626E-16
Don't use == to compare floating-point
numbers
Comparing Floating-Point
Numbers
Two numbers are close to another if
|x - y| <=
is a small number such as 10-14
Not good enough if x, y very large or very small.
Then use
|x - y| / max(|x|, |y|) <=
But if one of the numbers is zero, don't divide by
max(|x |, |y|)
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"))
Lexicographic Comparison
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"
Lexicographic Comparison
Object Comparison
Object Comparison
if (account == null) . . .
Multiple Alternatives
if (condition1)
statement1;
else if (condition2)
statement2;
else if (condition3)
statement3;
else
statement4;
The first matching condition is executed.
Order matters.
Multiple Alternatives
Order matters:
if (condition1)
Statement1;
else if (condition2)
Statement2;
else if (condition3)
Statement3;
else // option executes of all conditions fail
Statement4;
Nested Branches
Branch inside another branch
if (condition1)
{
if (condition1a)
statement1a;
else
statement1b;
}
else
statement2;
Predicate Method
return type boolean
Example:
public boolean isOverdrawn()
{ return balance < 0; }
Use in conditions
if (harrysChecking.isOverdrawn()) ...
Useful predicate methods in Character class:
isDigit
isLetter
isUpperCase
isLowerCase
if (Character.isUpperCase(ch)) ...
Boolean Operators
! not
&& and (short circuited)
|| or (short circuited)
A
!A
true
false
false
true
Truth Tables
A
A && B
true
true
true
true
false
false
Any
false
false
A
true
false
false
B
Any
true
false
A || B
true
true
false
Boolean Variables
private boolean married;
Set to truth value:
married = input.equals("M");
Use in conditions:
if (married) ... else ...
if (!married) ...
Also called flag
Don't test Boolean variables against truth values-sign of cluelessness:
if (married == true) // DON'T
if (married == false) // DON'T
if (married != false) // NO!!!
Year Balance
0
$10,000
1
$10,500
2
$11,025
3 $11,576.25
When will the balance be at least $20,000?
while Statement
while (condition)
statement;
//repeats the statement while the condition is true
while (balance < targetBalance){
year++;
double interest = balance * rate / 100;
balance = balance + interest;
}
do Statement
Executes loop body at least once:
do
statement
while (condition);
Example: Validate input
double value;
do{
String input = JOptionPane.showInputDialog(
"Please enter a positive number");
value = Integer.parseInt(input);
} while (input <= 0);
for Statement
for (initialization; condition; update)
statement
Example:
for (int i = 1; i <= n; i++){
double interest = balance * rate / 100;
balance = balance + interest;
}
Equivalent to:
initialization;
while (condition){
statement;
update;
}
sum = 0;
i = 1;
while ( i <= 10) ;
{
sum = sum + i;
i = i + 1;
}
System.out.println(sum);
Nested Loops
Create triangle pattern
[]
[][]
[][][]
[][][][]
Loop through rows
for (int i = 1; i <= n; i++)
{
// make triangle row
}
Another option
General pattern:
String Tokenization
StringTokenizer breaks up string into tokens
By default, white space separates tokens
"4.3 7 -2" breaks into three tokens:
"4.3", "7", "-2"
StringTokenizer tokenizer
= new StringTokenizer(a String);
while (tokenizer.hasMoreTokens())
{
String token = tokenizer.nextToken();
process token
}