April 20, 2009

0 Javascript If...Else Statements

Conditional statements in JavaScript are used to perform different actions based on different conditions.

Conditions Statements

Very often when you write code,you want to perform different actions for different decisions.You can use conditional statements in your code to this.

In Javascript we have the following conditional statements:

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

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

if...else if...statement - use this statement if you want to select one of many blocks at code to be executed

switch statement - use this statement if you want to select one of many blocks of code to be executed

If Statement

You should use the if Statement if you want to execute some code only if a specified condition is true.

syntax

if (conditon)
{
code to be executed if condition is true
}

Note that if written in lowercase letters.Using uppercase letters (IF) will generate a Javascript error

Example

<html>
<body>

<script type="text/javascript">
var d = new Date( )
var time = d.getHours( )
if (time<10)
{
document.write("<b> Good morning </b>")
}
else if (time>=10 && time<16)
{
document.write("<b> Good day </b>")
}
else
{
document.write("<b> Hello World!</b>
}
</script>
<p>
This example demonstrates the if..else if...else statement.
</p>
</body>
</html>





0 comments:

Post a Comment

Blogger Themes

 
Powered by Blogger