Java Reference
In-Depth Information
condition
evaluated
true
false
statement
FIGURE 5.6
The logic of an
if
statement
is part of the
if
statement; it implies that the
if
statement governs whether the
assignment statement will be executed. Although this indentation is extremely
important for the human reader, it is ignored by the compiler.
The example in Listing 5.1 reads the age of the user and then makes a decision
as to whether to print a particular sentence based on the age that is entered.The
Age
program echoes the age value that is entered in all cases. If the age is less than the
value of the constant
MINOR
, the statement about youth is printed. If the age is equal
to or greater than the value of
MINOR
, the
println
statement is skipped. In either case,
the final sentence about age being a state of mind is printed.
LISTING 5.1
//********************************************************************
// Age.java Author: Lewis/Loftus
//
// Demonstrates the use of an if statement.
//********************************************************************
import
java.util.Scanner;
public class
Age
{
//-----------------------------------------------------------------
// Reads the user's age and prints comments accordingly.
//-----------------------------------------------------------------
public static void
main (String[] args)
{
final int
MINOR = 21;
Scanner scan =
new
Scanner (System.in);
Search WWH ::
Custom Search