Java Reference
In-Depth Information
where the targets of the
if
and
else
are single statements. The
else
clause is optional. The
targets of both the
if
and
else
can be blocks of statements. The general form of the
if
, using
blocks of statements, is
If the conditional expression is true, the target of the
if
will be executed; otherwise, if it
exists, the target of the
else
will be executed. At no time will both of them be executed. The
conditional expression controlling the
if
must produce a
boolean
result.
To demonstrate the
if
(and several other control statements), we will create and develop
a simple computerized guessing game that would be suitable for young children. In the first
version of the game, the program asks the player for a letter between A and Z. If the player
presses the correct letter on the keyboard, the program responds by printing the message
**
Right **
. The program is shown here: