Java Reference
In-Depth Information
You can selectively execute part of a program through the use of Java's conditional state-
ment: the
if
. The Java
if
statement works much like the IF statement in any other language.
Its simplest form is shown here:
if
(condition) statement;
Here,
condition
is a Boolean expression. If
condition
is true, then the statement is executed.
If
condition
is false, then the statement is bypassed. Here is an example:
In this case, since 10 is less than 11, the conditional expression is true, and
println( )
will
execute. However, consider the following:
In this case, 10 is not less than 9. Thus, the call to
println( )
will not take place.
Java defines a full complement of relational operators that may be used in a conditional
expression. They are shown here:
Operator
Meaning
<
Less than
<=
Less than or equal
>
Greater than
>=
Greater than or equal
= =
Equal to
!=
Not equal
Notice that the test for equality is the double equal sign.
Here is a program that illustrates the
if
statement: