Java Reference
In-Depth Information
This would work, but it's a little verbose and can be quite confusing. JavaScript offers a better alterna-
tive — using multiple conditions inside the condition part of the if statement. The multiple conditions
are strung together with the logical operators you just looked at. So the preceding code could be rewritten
like this:
if (degCent > 0 && degCent < 100)
{
document.write(“degCent is between 0 and 100”);
}
The if statement's condition fi rst evaluates whether degCent is greater than zero. If that is true, the
code goes on to evaluate whether degCent is less than 100. Only if both of these conditions are true
will the document.write() code line execute.
Try It Out Multiple Conditions
This example demonstrates multi-condition if statements using the AND, OR, and NOT operators. Type
the following code, and save it as ch3_examp2.htm:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<body>
<script type=”text/javascript”>
var myAge = Number(prompt(“Enter your age”,30));
if (myAge >= 0 && myAge <= 10)
{
document.write(“myAge is between 0 and 10<br />“);
}
if ( !(myAge >= 0 && myAge <= 10) )
{
document.write(“myAge is NOT between 0 and 10<br />“);
}
if ( myAge >= 80 || myAge <= 10 )
{
document.write(“myAge is 80 or above OR 10 or below<br />“);
}
if ( (myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89) )
{
document.write(“myAge is between 30 and 39 or myAge is between 80 and 89”);
}
</script>
</body>
</html>
Search WWH ::




Custom Search