Thursday, December 2, 2010

The If Statement




if statement
use this statement to execute some code only if a specified condition is true


if...else statement
use this statement to execute some code if the condition is true and another code if the condition is false


if...else if....else statement
use this statement to select one of many blocks of code to be executed

switch statement
use this statement to select one of many blocks of code to be executed

Using the If Statement

Try this in your page


<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10)>Good morning");
}
else if (time>10 && time<16)>Good day");
}
else
{
document.write("Hello World!");
}
</script>


Prompt Box

<html>
<head>
<script type="text/javascript">
function show_prompt()
{
var name=window.prompt("Please enter your name","Harry Potter");
if (name!=null && name!="")
{
document.write("Hello " + name + "! How are you today?");
} }
</script>
</head>
<body>
<input type="button" onclick="show_prompt()" value="Show prompt box" />
</body>
</html>

Check the prompt box on top of the page on the right.


No comments: