HTML and CSS Reference
In-Depth Information
EXAMPLE 6.2 ( CONTINUED )
3
alert("You pay $" + price + 0);
</script>
</big>
</body>
</html>
EXPLANATION
1
The user is prompted for input. The value he or she enters in the prompt box is
assigned to the variable age .
2
If the value of age is greater than 55, the value to the right of the ? is assigned to
the variable price ; if not, the value after the : is assigned to the variable price .
3
The alert dialog box displays the value of the variable price .
6.2.2 if/else if
“If you've got $1, we can go to the Dollar Store; else if you've got $10, we could get a
couple of movies; else if you've got $20 we could buy a CD . . . else forget it!” JavaScript
provides yet another form of branching, the if/else if construct. This construct provides
a multiway decision structure.
FORMAT
if (condition) {
statement(s);
}
else if (condition) {
statement(s);
}
else if (condition) {
statement(s);
}
else{
statement(s);
}
If the first conditional expression following the if keyword is true, the statement or
block of statements following the expression is executed and control starts after the final
else block. Otherwise, if the conditional expression following the if keyword is false,
control branches to the first else if and the expression following it is evaluated. If that
expression is true, the statement or block of statements following it are executed, and if
false, the next else if is tested. All else if s are tested and if none of their expressions are
true, control goes to the else statement. Although the else is not required, it normally
serves as a default action if all previous conditions were false.
 
 
Search WWH ::




Custom Search