Java Reference
In-Depth Information
Step 1.
y = 2 * 5 * 5 + 3 * 5 + 7;
(Leftmost multiplication)
2 * 5 is 10
(Leftmost multiplication)
Step 2.
y = 10 * 5 + 3 * 5 + 7;
10 * 5 is 50
(Multiplication before addition)
Step 3.
y = 50 + 3 * 5 + 7;
3 * 5 is 15
Step 4.
y = 50 + 15 + 7;
(Leftmost addition)
50 + 15 is 65
Step 5.
y = 65 + 7;
(Last addition)
65 + 7 is 72
Step 6.
y = 72
(Last operation—place
72
in
y
)
Fig. 2.13
|
Order in which a second-degree polynomial is evaluated
.
A
condition
is an expression that can be
true
or
false
. This section introduces Java's
if
selection statement
, which allows a program to make a
decision
based on a condition's
value. For example, the condition “grade is greater than or equal to 60” determines wheth-
er a student passed a test. If the condition in an
if
statement is
true
,
the body of the
if
statement executes. If the condition is
false
, the body does not execute. We'll see an exam-
ple shortly.
Conditions in
if
statements can be formed by using the
equality operators
(
==
and
!=
) and
relational operators
(
>
,
<
,
>=
and
<=
) summarized in Fig. 2.14. Both equality oper-
ators have the same level of precedence, which is
lower
than that of the relational operators.
The equality operators associate from
left to right
. The relational operators all have the
same level of precedence and also associate from
left to right
.
Algebraic
operator
Java equality or
relational operator
Sample Java
condition
Meaning of Java condition
Equality operators
=
==
x == y
x
is equal to
y
¹
x
is not equal to
y
!=
x != y
Fig. 2.14
|
Equality and relational operators. (Part 1 of 2.)