Java Reference
In-Depth Information
Coding an if Statement to Test Multiple Conditions
The previous steps included code that tested for only one condition at a
time. While this is common, an if statement or an if…else statement can test
more than one possible condition as well. The logical AND operator (&&) typi-
cally would be used for situations in which you might want to test more than
one piece of data at the same time. For example, a program that checks for adult
male students might compare the gender with a code 1 for male and compare
the age with a numeric value. In this case, each condition is enclosed in its own
set of parentheses, as shown in the following line of code.
if ((gender == 1) && (age >= 18))
The logical OR operator (| |) typically would be used for testing the same
piece of data in two different ways. For example, a program that tests an age for
possible child or senior discount might compare the age against two different
numeric values, as shown in the following line of code.
if ((age < 13) || (age > 65))
The logical NOT operator (!) typically would be used for testing a boolean
piece of data that evaluates to true or false. For example, a programmer might
assign a boolean true value to the variable, done, in response to a user's input to
quit the program. If done were not set, then processing would continue. The if
statement using the NOT operator might display as shown in the following line
of code.
if (!done)
The logical operators produce boolean results - that is, they evaluate to true
or false. The values or operands used in the expressions also must be boolean.
An important characteristic of the logical AND and OR is that if the left operand
can be sufficient to decide the condition, the right side never is evaluated. For
example, if the left side of the logical AND operator (&&) evaluates to false, the
condition automatically is false and the right side need not be evaluated. The left
operand is sufficient to decide that the condition evaluates to false.
Exception Handling
An exception is a Java event resulting from an unusual or erroneous situation
which disrupts the normal program flow of instructions. An exception also
sometimes is referred to as a run-time exception or run-time error, as discussed
in Chapter 2. Exception handling is the general concept of planning for possi-
ble exceptions by directing the program to deal with them gracefully without
terminating prematurely. For example, in Chapter 3, possible exceptions were
handled by adding the code, throws IOException, to the main() method header.
The code gave the program a way to acknowledge and handle potential input or
output errors and still compile correctly.
Java has different types of exceptions, including the I/O exceptions covered
in Chapter 3, run-time exceptions, and checked exceptions. As you have learned,
a run-time exception occurs within the Java run-time system and includes arith-
metic exceptions, such as when dividing by zero. Run-time exceptions can occur
anywhere in a program and may be quite numerous.
Search WWH ::




Custom Search