Javascript If...Else Statements
Conditional Statements
If Statement
If..Else Statement
If...Else if ....else statement
Conditional Statements
to perform different actions for different decisions.
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 to select one of many bloks of code to be executed.
a {} block must contain the statements to be executed. if curly braces are not present, only the subsequent statement is executed, which is very common programming error
if(condition)
{
statement 1;
statement 2;
statement 3;
}//all three statements are executed
if(condition)
statement 1;
statement 2;
statement 3;
//only statement 1 is executed
if statement
use the if statement to execute some code only if a specified condition is true.
if(condition)
{
code to be executed if condition is true
}
if is written in lowercase letters. using Uppercase letters (IF) will generate a javascript error!
<html>
<body>
<script type="text/javascript">
var d=new Date();
var time=d.getHours();
if(time<10)
{
document.write("<b>Good morning</b>");
}
</script>
<p>This example demonstrates the if statement.</p>
<p>If the time on your browser is less than 10, you will get a "Good morning" greeting.</p>
</body>
</html>
If...else statement
use the if ...else statement to execute some code if a condition is true and another code is false
if(condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
<html>
<body>
<script type="text/javascript">
var d=new Date();
var time=d.getHours();
if(time<10)
{
document.write("<b>Good Morning<</b>");
}
else
{
document.write("<b>Good day</b>");
}
</script>
<P>This example demonstrates the If...Else statement.</p>
<p>
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
Otherwise you will get a "Good day" greeting.
</p>
</body>
</html>
<html>
<body>
<script type="text/javascript">
var r=Math.random();
if(r>0.5)
{
document.write("<a href='hppt://www.w3schools.com'>Learn web development!</a>");
}
else
{
document.write("<a href='http://www.refsnesdata.com'>Refsnes Data!</a>");
}
</script>
</body>
</html>
if..else if...else statement
Use the if....else if...else statement to select one of several blocks of code to be executed
if(condition)
{
code to be executed if condition1 is true
}
else if(condition2)
{
code to be executed if condition2 is true
}
else
{
code to be executed if condition1 and condition2 are not true
}
<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 demonsrates the if ..else if .... else statement.
</p>
</body>
</html>
Conditional Statements
If Statement
If..Else Statement
If...Else if ....else statement
Conditional Statements
to perform different actions for different decisions.
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 to select one of many bloks of code to be executed.
a {} block must contain the statements to be executed. if curly braces are not present, only the subsequent statement is executed, which is very common programming error
if(condition)
{
statement 1;
statement 2;
statement 3;
}//all three statements are executed
if(condition)
statement 1;
statement 2;
statement 3;
//only statement 1 is executed
if statement
use the if statement to execute some code only if a specified condition is true.
if(condition)
{
code to be executed if condition is true
}
if is written in lowercase letters. using Uppercase letters (IF) will generate a javascript error!
<html>
<body>
<script type="text/javascript">
var d=new Date();
var time=d.getHours();
if(time<10)
{
document.write("<b>Good morning</b>");
}
</script>
<p>This example demonstrates the if statement.</p>
<p>If the time on your browser is less than 10, you will get a "Good morning" greeting.</p>
</body>
</html>
If...else statement
use the if ...else statement to execute some code if a condition is true and another code is false
if(condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
<html>
<body>
<script type="text/javascript">
var d=new Date();
var time=d.getHours();
if(time<10)
{
document.write("<b>Good Morning<</b>");
}
else
{
document.write("<b>Good day</b>");
}
</script>
<P>This example demonstrates the If...Else statement.</p>
<p>
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
Otherwise you will get a "Good day" greeting.
</p>
</body>
</html>
<html>
<body>
<script type="text/javascript">
var r=Math.random();
if(r>0.5)
{
document.write("<a href='hppt://www.w3schools.com'>Learn web development!</a>");
}
else
{
document.write("<a href='http://www.refsnesdata.com'>Refsnes Data!</a>");
}
</script>
</body>
</html>
if..else if...else statement
Use the if....else if...else statement to select one of several blocks of code to be executed
if(condition)
{
code to be executed if condition1 is true
}
else if(condition2)
{
code to be executed if condition2 is true
}
else
{
code to be executed if condition1 and condition2 are not true
}
<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 demonsrates the if ..else if .... else statement.
</p>
</body>
</html>
No comments:
Post a Comment