Java Reference
In-Depth Information
The if Statement
The if statement in Nashorn works the same way it works in Java. Its general syntax is:
if(condition)
statement;
You can also have an else part for an if statement:
if(condition)
statement-1;
else
statement-2;
If the condition evaluates to true , statement-1 is executed; otherwise, statement-2
is executed. Notice that the condition can be any type of expression, not necessarily a
Boolean expression. The condition expression is evaluated and converted to a Boolean
type value. The following snippet of code shows how to use an if and an if-else
statements:
var x = 100, y = 200;
if (x <= y)
printf("%d <= %d", x, y);
// The print() function returns undefined that evaluates to a Boolean false.
if (print(x)) {
print("Inside if");
}
else {
print("Inside else")
}
100 <= 200
100
Inside else
Notice that the expression print(x) is used as the condition for the second if
statement. The print() function prints the value of the variable x and returns undefined .
The value undefined is converted to the Boolean false (please refer to the section To
Boolean Conversion ) that will execute the statement associated with the else part.
 
Search WWH ::




Custom Search