Java Reference
In-Depth Information
10.2. ifelse
The most basic form of conditional control flow is the if statement, which
chooses whether to execute statements that follow it. Its syntax is:
if ( expression )
statement1
else
statement2
First, the expressionwhich must be of type boolean or Boolean is evaluated.
If its value is TRue , then statement1 is executed; otherwise, if there is an
else clause, statement2 is executed. The else clause is optional.
You can build a series of tests by joining another if to the else clause of
a previous if . Here is a method that maps a stringexpected to be one of
a particular set of wordsinto an action to be performed with a value:
public void setProperty(String keyword, double value)
throws UnknownProperty
{
if (keyword.equals("charm"))
charm(value);
else if (keyword.equals("strange"))
strange(value);
else
throw new UnknownProperty(keyword);
}
What if there is more than one preceding if without an else ? For ex-
ample:
public double sumPositive(double[] values) {
double sum = 0.0;
 
Search WWH ::




Custom Search