CHAPTER
3
DECISIONS
Java for Everyone by Cay Horstmann Slides by Donald W. Smith
Copyright © 2009 by John Wiley & Sons. All rights reserved. TechNeTrain.com
Chapter Goals
To be able to implement decisions
To learn how to compare integers, floating-
point numbers, and strings
To understand the Boolean data type
To develop strategies for validating user
input
In this chapter, you will learn how to
program simple and complex decisions.
You will apply what you learn to the task
of checking user input.
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 2
Contents
The if Statement
Comparing Numbers
and Strings
Multiple Alternatives
Nested Branches
Boolean Variables and Operators
Application: Input Validation
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 3
3.1 The if Statement
A computer program often needs to make
decisions based on input, or circumstances.
For example, buildings often ‘skip’ the 13 th floor,
and elevators should too.
The 14th floor is really the 13th floor
So every floor above 12 is really ‘floor - 1’
• If floor > 12, Actual floor = floor - 1
The two keywords of the if statement are:
if
The if statement allows a program to
else carry out different actions depending on
the nature of the data to be processed.
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 4
Flowchart of the if statement
One of the two branches is executed once
True (if) branch or False (else) branch
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 5
Flowchart with only true branch
The if statement may not need a ‘False’ (else) branch
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 6
Syntax 3.1: The if statement
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 7
ElevatorSimulation.java
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 8
Tips On Using Braces
Line up all pairs of braces vertically
Lined up Not aligned (saves lines)
Always use braces
Although single statement clauses do not require them
Most programmer’s editors have a
tool to align matching braces.
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 9
Tips on indenting blocks
Use Tab to indent a consistent number of spaces
This is referred to as ‘block- structured’
code. Indenting consistently makes
code much easier for humans to follow.
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 10
Common Error 3.1
It is easy to forget and add a semicolon
after an if statement.
The true path is now the space just before the
semicolon
if (floor
if (floor >> 13)
13) ;;
{{
floor--;
floor--;
}}
The ‘body’ will always be executed in this case
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 11
The Conditional Operator
A ‘shortcut’ you may find in existing code
It is not used in this book
Condition True branch False branch
Includes all parts of an if-else clause, but uses:
•? To begin the true branch
actualFloor = floor > 13 ? floor - 1 : floor;
•: To end the true branch and start the false branch
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 12
3.2 Comparing Numbers and Strings
Every if statement has a condition
Usually compares two values with an operator
if (floor
if (floor >>
13) ..
13) ..
if (floor
if (floor >= 13)
>= 13)
..
..
if (floor
if (floor <<
13) ..
13) ..
if (floor
if (floor <= 13)
<= 13)
..
..
if (floor
if (floor == 13)
== 13)
..
..
Beware!
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 13
Syntax 3.2: Comparisons
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 14
Operator Precedence
The comparison operators have lower precedence than arithmetic operators
Calculations are done before the comparison
Normally your calculations are on the ‘right side’ of the comparison or assignment operator
Calculations
actualFloor = floor + 1;
if (floor > height + 1)
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 15
Comparing Strings
Strings are a bit ‘special’ in Java
Do not use the == operator with Strings
The following compares the locations of two strings, and not their contents
Instead use the String’s ‘equals’ method:
if (string1 == string2) ...
if (string1.equals(string2)) ...
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 16
Relational Operator Use (1)
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 17
Relational Operator Use (2)
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 18
Common Error 3.2
Comparison of Floating-Point Numbers
Floating-point numbers have limited precision
Round-off errors can lead to unexpected results
double rr == Math.sqrt(2.0);
double Math.sqrt(2.0);
if (r
if (r ** rr ==
== 2.0)
2.0)
{{
System.out.println("Math.sqrt(2.0) squared
System.out.println("Math.sqrt(2.0) squared is
is 2.0");
2.0");
}}
else
else
{{
System.out.println("Math.sqrt(2.0) squared
System.out.println("Math.sqrt(2.0) squared is
is not
not 2.0
2.0
but "" ++ rr ** r);
but r);
}} Output:
Output:
Math.sqrt(2.0) squared
Math.sqrt(2.0) squared is
is not
not 2.0
2.0 but
but 2.00000000000000044
2.00000000000000044
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 19
The use of EPSILON
Use a very small value to compare the difference if floating-point values are ‘close enough’
The magnitude of their difference should be less than some threshold
Mathematically, we would write that x and y are close enough if:
final double
final double EPSILON
EPSILON == 1E-14;
1E-14;
double rr == Math.sqrt(2.0);
double Math.sqrt(2.0);
if (Math.abs(r
if (Math.abs(r ** rr -- 2.0)
2.0) << EPSILON)
EPSILON)
{{
System.out.println("Math.sqrt(2.0) squared
System.out.println("Math.sqrt(2.0) squared is
is approx.
approx.
2.0");
2.0");
}}
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 20
Common Error 3.3
Using == to compare Strings
== compares the locations of the Strings
Java creates a new String every time a new word inside double-quotes is used
If there is one that matches it exactly, Java re-uses it
String nickname
String nickname == "Rob";
"Rob";
.. .. ..
if (nickname
if (nickname ==
== "Rob") //
"Rob") // Test
Test is
is true
true
String name
String name == "Robert";
"Robert";
String nickname
String nickname == name.substring(0,
name.substring(0, 3);
3);
.. .. ..
if (nickname
if (nickname ==
== "Rob")
"Rob") //
// Test
Test is
is false
false
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 21
Lexicographical Order
To compare Strings in ‘dictionary’ order
When compared using compareTo, string1 comes:
• Before string2 if
• After string2 if
string1.compareTo(string2) << 00
string1.compareTo(string2)
• Equal to string2 if
Notes
• All UPPERCASE letters come before lowercase
string1.compareTo(string2) >> 00
string1.compareTo(string2)
• ‘space’ comes before all other printable characters
• Digits (0-9) come before all letters
• See Appendix A for the Basic Latin Unicode (ASCII) table
string1.compareTo(string2) ==
string1.compareTo(string2) == 00
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 22
Implementing an if Statement
1) Decide on a branching condition
2) Write pseudocode for the true branch
3) Write pseudocode for the false branch
4) Double-check relational operators
Test values below, at, and above the comparison (127, 128, 129)
5) Remove duplication
6) Test both branches
7) Write the code in Java
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 23
Implemented Example
The university bookstore has a Kilobyte Day sale every October
24, giving an 8 percent discount on all computer accessory
purchases if the price is less than $128, and a 16 percent
discount if the price is at least $128.
if (originalPrice
if (originalPrice << 128)
128)
{{
discountRate == 0.92;
discountRate 0.92;
}}
else
else
{{
discountRate == 0.84;
discountRate 0.84;
}}
discountedPrice == discountRate
discountedPrice discountRate ** originalPrice;
originalPrice;
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 24
3.3 Multiple Alternatives
What if you have more than two branches?
Count the branches for the following
earthquake effect example:
8 (or greater)
7 to 7.99
6 to 6.99
4.5 to 5.99
Less than 4.5
When using multiple if statements,
test general conditions after more
specific conditions.
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 25
Multiple Alternatives
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 26
Flowchart of Multiway branching
True
> 8.0? Most Structures Fall
False
True
>= 7.0? Many Buildings Destroyed
False
True Many buildings considerably
>= 6.0?
damaged, some collapse
False
True
Damage to poorly constructed
>= 4.5?
buildings
False
No destruction of buildings
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 27
if, else if multiway branching
if (richter
if (richter >=
>= 8.0)
8.0) // Handle
// Handle the
the ‘special
‘special case’
case’ first
first
{{
System.out.println("Most structures
System.out.println("Most structures fall");
fall");
}}
else if
else if (richter
(richter >=
>= 7.0)
7.0)
{{
System.out.println("Many buildings
System.out.println("Many buildings destroyed");
destroyed");
}}
else if
else if (richter
(richter >=
>= 6.0)
6.0)
{{
System.out.println("Many buildings
System.out.println("Many buildings damaged,
damaged, some
some collapse");
collapse");
}}
else if
else if (richter
(richter >=
>= 4.5)
4.5)
{{
System.out.println("Damage to
System.out.println("Damage to poorly
poorly constructed
constructed buildings");
buildings");
}}
else
else // so
// so that
that the
the ‘general
‘general case’
case’ can
can be
be handled
handled last
last
{{
System.out.println("No destruction
System.out.println("No destruction of
of buildings");
buildings");
}}
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 28
What is wrong with this code?
if (richter
if (richter >=
>= 8.0)
8.0)
{{
System.out.println("Most structures
System.out.println("Most structures fall");
fall");
}}
if (richter
if (richter >=
>= 7.0)
7.0)
{{
System.out.println("Many buildings
System.out.println("Many buildings destroyed");
destroyed");
}}
if (richter
if (richter >=
>= 6.0)
6.0)
{{
System.out.println("Many buildings
System.out.println("Many buildings damaged,
damaged, some
some collapse");
collapse");
}}
if (richter
if (richter >=
>= 4.5)
4.5)
{{
System.out.println("Damage to
System.out.println("Damage to poorly
poorly constructed
constructed buildings");
buildings");
}}
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 29
Another way to multiway branch
The switch statement chooses a case based on an integer
value.
int digit
int digit == .. .. .;
.;
switch (digit)
switch (digit)
break ends {{
case 1:
case 1: digitName
digitName == "one";
"one"; break;
break;
each case case 2:
case 2: digitName
digitName == "two";
"two"; break;
break;
default case 3:
case 3: digitName
digitName == "three";
"three"; break;
break;
case 4:
case 4: digitName
digitName == "four";
"four"; break;
break;
catches all other case 5:
case 5: digitName
digitName == "five";
"five"; break;
break;
values case 6:
case 6: digitName
digitName == "six";
"six"; break;
break;
case 7:
case 7: digitName
digitName == "seven";
"seven"; break;
break;
case 8:
case 8: digitName
digitName == "eight";
"eight"; break;
break;
If the break is missing, the case 9:
9: digitName
digitName == "nine";
"nine"; break;
case break;
case falls through to the default: digitName
digitName == "";
""; break;
default: break;
next case’s statements.
}}
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 30
3.4 Nested Branches
You can nest an if inside either branch of an if statement.
Simple example: Ordering drinks
Ask the customer for their drink order
If customer orders wine
• Ask customer for ID
• If customer’s age is 21 or over
– Serve wine
• Else
– Politely explain the law to the customer
Else
• Serve customers a non-alcoholic drink
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 31
Nested Branches
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 32
Flowchart of a Nested if
Ask for order
Nested if-else inside true
branch of an if statement.
Three paths
True
Wine
Get ID
?
False True
>=
Serve wine
21?
Serve non-
False
Alcoholic
drink Read law
Done
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 33
Tax Example: Nested ifs
Four outcomes (branches)
Single
• <= 32000
• > 32000
Married
• <= 64000
• > 64000
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 34
Flowchart for Tax Example
Four branches
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 35
TaxCalculator.java (1)
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 36
TaxCalculator.java (2)
The ‘True’ branch (Married)
Two branches within this branch
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 37
TaxCalculator.java (3)
The ‘False’ branch (not Married)
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 38
Hand-Tracing
Hand-tracing helps you understand whether a
program works correctly
Create a table of key variables
Use pencil and paper to track their values
Works with pseudocode or code
Track location with a marker such as a
paper clip
Use example input values that:
You know what the correct outcome should be
Will test each branch of your code
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 39
Hand Tracing Tax Example (1)
Setup
Table of variables
Initial values
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 40
Hand Tracing Tax Example (2)
Input variables
From user
Update table
Because marital status is not “s” we skip to the
else on line 41
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 41
Hand Tracing Tax Example (3)
Because income is not <= 64000, we move to the else clause on line
47
Update variables on lines 49 and 50
Use constants
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 42
Hand Tracing Tax Example (4)
Output
Calculate
As expected?
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 43
Choosing Test Cases
Choose input values that:
A boundary case is a
Test boundary cases and 0 values value that is tested in
Test each branch the code.
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 44
Common Error 3.4
The Dangling else Problem
When an if statement is nested inside another if statement, the following can occur:
The indentation level suggests that the else is related to the if country (“USA”)
• Else clauses always associate to the closest if
double shippingCharge
double shippingCharge == 5.00;
5.00; // $5
// $5 inside
inside continental
continental U.S.
U.S.
if (country.equals("USA"))
if (country.equals("USA"))
if (state.equals("HI"))
if (state.equals("HI"))
shippingCharge == 10.00;
shippingCharge 10.00; // Hawaii
// Hawaii is
is more
more expensive
expensive
else //
else // Pitfall!
Pitfall!
shippingCharge == 20.00;
shippingCharge 20.00; // As
// As are
are foreign
foreign shipment
shipment
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 45
3.5 Boolean Variables
Boolean Variables
A Boolean variable is often called a flag because it can be either
up (true) or down (false).
boolean is a Java data type
• boolean failed = true;
• Can be either true or false
Boolean Operators: && and ||
They combine multiple conditions
&& is the and operator
|| is the or operator
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 46
Character Testing Methods
The Character class has a number of handy
methods that return a boolean value:
if (Character.isDigit(ch))
if (Character.isDigit(ch))
{{
...
...
}}
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 47
Combined Conditions: &&
Combining two conditions is often used in range checking
Is a value between two other values?
Both sides of the and must be true for the result to be true
if (temp
if (temp >> 00 &&
&& temp
temp << 100)
100)
{{
System.out.println("Liquid");
System.out.println("Liquid");
}}
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 48
Combined Conditions: ||
If only one of two conditions need to be true
Is a value between two other values?
if
if
(balance
(balance
If either is true >> 100
100 ||
|| credit
credit >> 100)
100)
{{ The result is true
System.out.println(“Accepted");
System.out.println(“Accepted");
}}
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 49
The not Operator: !
If you need to invert a boolean variable or comparison, precede it with !
if (!attending
if (!attending || || grade
grade << 60)
60)
{{
If using !, try to use simpler logic:
System.out.println(“Drop?");
System.out.println(“Drop?");
}}
if (attending
if (attending &&
&& !(grade
!(grade << 60))
60))
{{
System.out.println(“Stay");
System.out.println(“Stay");
}}
if (attending
if (attending &&
&& (grade
(grade >=
>= 60))
60))
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 50
and Flowchart
if (temp
if (temp >> 00 &&
&& temp
temp << 100)
100)
{{
System.out.println("Liquid");
System.out.println("Liquid");
}}
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 51
or Flowchart
if (temp
if (temp <=
<= 00 ||
|| temp
temp >=
>= 100)
100)
{{
System.out.println(“Not Liquid");
System.out.println(“Not Liquid");
}}
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 52
Boolean Operator Examples
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 53
Boolean Operator Examples
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 54
Common Error 3.5
Combining Multiple Relational Operators
if (0
if (0 <=
<= temp
temp <=
<= 100)
100) // Syntax
// Syntax error!
error!
This format is used in math, but not in Java!
It requires two comparisons:
if (0
if (0 <=
<= temp
temp &&
&& temp
temp <=
<= 100)
100)
This is also not allowed in Java:
if (input
if (input ==
== 11 ||
|| 2)
2) // Syntax
// Syntax error!
error!
This also requires two comparisons:
if (input
if (input ==
== 11 ||
|| input
input ==
== 2)
2)
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 55
Common Error 3.6
Confusing && and ||
It is a surprisingly common error to confuse and and or conditions.
A value lies between 0 and 100 if it is at least 0 and at most 100.
It lies outside that range if it is less than 0 or greater than 100.
There is no golden rule; you just have to think carefully.
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 56
Lazy Evaluation: &&
Combined conditions are evaluated from left to right
If the left half of an and condition is false, why look further?
A useful example:
if (temp
if (temp >> 00 &&
&& temp
temp << 100)
100)
{{
System.out.println("Liquid");
System.out.println("Liquid");
}}
Done!
if (quantity
if (quantity >> 00 &&
&& price
price // quantity
quantity << 10)
10)
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 57
Lazy Evaluation: ||
If the left half of the or is true, why look further?
if (temp
if (temp <= <= 00 ||
|| temp
temp >=
>= 100)
100)
{{Java
doesn’t!
Don’t do these second:
System.out.println(“Not Liquid");
System.out.println(“Not
Assignment Liquid");
Output
}}
Done!
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 58
De Morgan’s Law
De Morgan’s law tells you how to negate && and ||
conditions:
!(A && B) is the same as !A || !B
!(A || B) is the same as !A && !B
Example: Shipping is higher to AK and HI
if (!(country.equals("USA")
if (!(country.equals("USA") if !country.equals("USA")
if !country.equals("USA")
&& !state.equals("AK")
&& !state.equals("AK") || state.equals("AK")
|| state.equals("AK")
&& !state.equals("HI")))
&& !state.equals("HI"))) || state.equals("HI")
|| state.equals("HI")
shippingCharge == 20.00;
shippingCharge 20.00; shippingCharge == 20.00;
shippingCharge 20.00;
To simplify conditions with negations of and or or
expressions, it is usually a good idea to apply De Morgan’s
Law to move the negations to the innermost level.
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 59
Input Validation
Accepting user input is dangerous
Consider the Elevator program:
The user may input an invalid character or value
Must be an integer if (in.hasNextInt())
if (in.hasNextInt())
{{
• Scanner can help! int floor
int floor == in.nextInt();
in.nextInt();
• hasNextInt // Process
// Process the
the input
input value
value
} }
– True if integer else
else
– False if not {{
System.out.println("Not integer.");
System.out.println("Not integer.");
}}
Then range check value
We expect a floor number to be between 1 and 20
• NOT 0, 13 or > 20
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 60
ElevatorSimulation2.java
Input value validity checking
Input value range checking
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 61
ElevatorSimulation2.java
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 62
Summary: if Statement
The if statement allows a program to carry out different
actions depending on the nature of the data to be
processed.
Relational operators ( < <= > >= == != ) are used to
compare numbers and Strings.
Do not use the == operator to compare Strings.
Use the equals method instead.
The compareTo method compares Strings in
lexicographic order.
Multiple if statements can be combined to evaluate
complex decisions.
When using multiple if statements, test general
conditions after more specific conditions.
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 63
Summary: Boolean
The Boolean type boolean has two values,
true and false.
Java has two Boolean operators that combine
conditions: && (and) and || (or).
To invert a condition, use the ! (not) operator.
The && and || operators are computed lazily:
As soon as the truth value is determined, no
further conditions are evaluated.
De Morgan’s law tells you how to negate &&
and || conditions.
Java for Everyone by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved. Page 64