Java Reference
In-Depth Information
NumberCheck.java
How It Works
You saw the random() method that is defined in the standard Math class in the previous chapter. It returns
a random value of type double between 0.0 and 1.0, but the result is always less than 1.0, so the largest
number you can get is 0.9999. . . (with the number of recurring digits being limited to the maximum
number that the type double allows, of course). Consequently, when you multiply the value returned
by 100.0 and convert this value to type int with the explicit cast, you discard any fractional part of the
number and produce a random integer between 0 and 99. Adding 1 to this results in a random integer
between 1 and 100, which you store in the variable number . You then generate the program output in the
if statement. If the value of number is even, the first println() call is executed; otherwise, the second
println() call in the else clause is executed.
Note the use of indentation here. It is evident that main() is within the class definition because indenta-
tion relative to the first line of the class definition provides you with a cue to that. The code for main()
is clearly distinguished because it is indented relative to the first line of the method. You can also see
immediately which statement is executed when the if expression is true and which applies when it is
false .
Nested if Statements
The statement that is executed when an if expression is true can be another if , as can the statement in an
else clause. This enables you to express such convoluted logic as “if my bank balance is healthy, then I will
buy the car if I have my check book with me, else I will buy the car if I can get a loan from the bank.” An if
statement that is nested inside another can also itself contain a nested if . You can continue nesting if s one
inside the other like this for as long as you still know what you are doing — or even beyond if you enjoy
confusion.
To illustrate the nested if statement, I can modify the if from the previous example:
if(number%2 == 0) { // Test if it is even
if(number < 50) { // Output a message if number is < 50
System.out.println("You have got an even number < 50, " + number);
}
} else {
System.out.println("You have got an odd number, " + number); // It is odd
}
Now the message for an even value is displayed only if the value of number is also less than 50. There
are three possible outcomes from this code fragment: If number is even and less than 50, you see a message
to that effect; if number is even and is not less than 50, there is no output; and finally; if number is odd, a
message is displayed.
The braces around the nested if are necessary here because of the else clause. The braces constrain the
nested if in the sense that if it had an else clause, it would have to appear between the braces enclosing
the nested if . If the braces were not there, the program would still compile and run but the logic would be
different. Let's see how.
Search WWH ::




Custom Search