HTML and CSS Reference
In-Depth Information
EXAMPLE 6.1
<html>
<head>
<title>Conditional Flow Control</title>
</head>
<body>
<h3>
1
<script type="text/javascript">
<!-- Hiding JavaScript from old browsers
2
var age=prompt("How old are you? ","");
3
if( age >= 55 ){
4
document.write("You pay the senior fare! ");
5
}
6
else{
7
document.write("You pay the regular adult fare. ");
}
//-->
8
</script>
</h3>
</body>
</html>
EXPLANATION
1
JavaScript program starts here.
2
The prompt dialog box will display the message “ How old are you? ”. Whatever the
user types into the box will be stored in the variable age (see Figure 6.1).
3, 4
If the value of the variable age is greater than or equal to 55 , line 4 is executed (see
Figure 6.2).
5
This closing curly brace closes the block of statements following the if expression.
When there is only one statement in the block, the curly braces are not required.
6, 7
The else statement, line number 7, is executed if the expression in line 3 is false.
8
This tag marks the end of the JavaScript program.
Figure 6.1 The user is prompted for input.
Search WWH ::




Custom Search