Lecture 05
Lecture 05
Statements cont…
Chapter 3:Decision Structures
• 3.1 The if Statement
• 3.2 The if-else Statement
• 3.3 The if-else-if Statement
• 3.4 Nested if Statements
• 3.5 Logical Operators
• 3.6 Comparing String Objects
• 3.7 More About Variable Declaration and Scope
• 3.9 The switch Statement
• 3.10 Creating Objects with the DecimalFormat Class
• 3.12 Common Errors to Avoid
Nested if Statements
• If an if statement appears inside of another if
statement (single or block) it is called a nested
if statement.
• The nested if is only executed if the if
statement it is in results in a true condition.
• Nested if statements can get very complex,
very quickly.
LoanQualifier.java
double salary, yearsOnJob;
String input;
input = JOptionPane.showInputDialog("Enter
your " + "annual salary.");
salary = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter
the number of " +"years at your current job.");
yearsOnJob = Double.parseDouble(input);
LoanQualifier.java
if (salary >= 30000) {
if (yearsOnJob >= 2) {
JOptionPane.showMessageDialog(null, "You qualify " + "for the loan.");
}
else {
JOptionPane.showMessageDialog(null, "You must have " + "been on your
current job for at least " +
"two years to qualify.");
}
}
else {
JOptionPane.showMessageDialog(null, "You must earn at” + “least $30,000 per
year to qualify.");
}
Nested if Statement Flowcharts
No Yes
Is it cold
outside?
Wear shorts.
No Is it Yes
snowing?
No Yes
Is it cold
outside?
Wear shorts.
No Is it Yes
snowing?
Expression 1 !Expression1
true false
false true
Order of Precedence
• The ! operator has a higher order of
precedence than the && and || operators.
• The && and || operators have a lower
precedence than relational operators like <
and >.
• Parenthesis can be used to force the
precedence to be changed.
Order of Precedence
Order of
Operators Description
Precedence
(unary
1 Unary negation, logical NOT
negation) !
2 */% Multiplication, Division, Modulus
3 +- Addition, Subtraction
Less-than, Greater-than, Less-
4 < > <= >= than or equal to, Greater-than or
equal to
5 == != Is equal to, Is not equal to
6 && Logical AND
7 || Logical NOT
= += -= Assignment and combined
8
*= /= %= assignment operators.
Comparing String Objects
• In most cases, you cannot use the relational
operators to compare two String objects.
• Reference variables contain the address of the
object they represent.
• Unless the references point to the same
object, the relational operators will not return
true.
StringCompareTo.java
StringCompare.java
String name1 = "Mark",
name2 = "Mark",
name3 = "Mary";
// Compare "Mark" and "Mark"
if (name1.equals(name2))
{
System.out.println(name1 + " and " + name2 +
" are the same.");
}
else
{
System.out.println(name1 + " and " + name2 +
" are the NOT the same.");
}
StringCompare.java
// Compare "Mark" and "Mary"
if (name1.equals(name3))
{
System.out.println(name1 + " and " + name3 +
" are the same.");
}
else
{
System.out.println(name1 + " and " + name3 +
" are the NOT the same.");
}
StringCompareTo.java
String name1 = "Mary",
name2 = "Mark";
// Compare "Mary" and "Mark"
if (name1.compareTo(name2) < 0)
{
System.out.println(name1 + " is less than " + name2);
}
else if (name1.compareTo(name2) == 0)
{
System.out.println(name1 + " is equal to " + name2);
}
else if (name1.compareTo(name2) > 0)
{
System.out.println(name1 + " is greater than " + name2);
}
Ignoring Case in String
Comparisons
• In the String class the equals and compareTo
methods are case sensitive.
• In order to compare two String objects that
might have different case, use:
– equalsIgnoreCase, or
– compareToIgnoreCase
SecretWord.java
if (input.equalsIgnoreCase("PROSPERO"))
{
System.out.println("Congratulations!” + “You
know the“ + “ secret word!");
}
else
{
System.out.println("Sorry, that is NOT” “the "
+"secret word!");
}