Conditional Statements
In This Tutorial You want to perform different actions for different situvation.In JavaScript we have the following conditional statements:
- if statement .
- if-else statement .
- if...else if....else statement.
- switch statement.
- if statement - If statement if you want to execute some code only if a specified condition is true
Syntax
if (condition){
Write if condition which is true
}
Example
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
//If Age is grater than 18 then eligible for voting
var age= 20;
if(age>18) {
document.write("Age Is Valid For Vote")
}
</script>
</head>
</html>
<html>
<head>
<script type="text/javascript">
//If Age is grater than 18 then eligible for voting
var age= 20;
if(age>18) {
document.write("Age Is Valid For Vote")
}
</script>
</head>
</html>
- if...else statement - This statement if you want to execute some code if the condition is true and another code if the condition is false
Syntax
if (condition){
Write True condition.
}
else
{
Write False condition.
}
Example
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
//If Age is grater than 18 then eligable for voting
var age= 15;
if(age>18) {
document.write("Age Is Valid For Vote")
}
else
document.write("Age Is Not Valid For Vote")
</script>
</head>
</html>
<html>
<head>
<script type="text/javascript">
//If Age is grater than 18 then eligable for voting
var age= 15;
if(age>18) {
document.write("Age Is Valid For Vote")
}
else
document.write("Age Is Not Valid For Vote")
</script>
</head>
</html>
- if...else if....else statement - Use this statement if you want to select one of many blocks of code to be executed
Syntax
if (condition1){
code to be executed if condition1 is true
}
else if (condition2)
{
code to be executed if condition2 is true
}
else if (condition3)
{
code to be executed if condition3 is true
}
else
{
code to be executed if condition1 and condition2 and condition3 are not true
}
Example
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10) {
document.write("Good morning");
}
else if (time>10 && time)<16 br=""> {
document.write("Good day");
}
else if (time>16 && time)<19 br=""> {
document.write("Good Evening");
}
else if (time>19 && time)<24 br=""> {
document.write("Good Night");
}
else
{
document.write("Hello World!");
}
</script>
</head>
</html>
24>19>16>
<html>
<head>
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10) {
document.write("Good morning");
}
else if (time>10 && time)<16 br=""> {
document.write("Good day");
}
else if (time>16 && time)<19 br=""> {
document.write("Good Evening");
}
else if (time>19 && time)<24 br=""> {
document.write("Good Night");
}
else
{
document.write("Hello World!");
}
</script>
</head>
</html>
24>19>16>
- switch statement - Switch statement if you want to select one of many blocks of code to be executed
Syntax
switch(n){
case 1:
execute code case 1
break;
case 2:
execute code case 2
break;
default:
code to be executed if n is
different from case 1 and 2
}
This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically.
Example
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.
var d=new Date();
theDay=d.getDay();
switch (theDay)
{
case 5:
document.write("Day is Friday");
break;
case 6:
document.write("Day is Saturday");
break;
case 0:
document.write("Day is Sunday");
break;
default:
document.write("I'm looking forward to this weekend!");
}
</script>
</head>
</html>
<html>
<head>
<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.
var d=new Date();
theDay=d.getDay();
switch (theDay)
{
case 5:
document.write("Day is Friday");
break;
case 6:
document.write("Day is Saturday");
break;
case 0:
document.write("Day is Sunday");
break;
default:
document.write("I'm looking forward to this weekend!");
}
</script>
</head>
</html>
0 comments :
Post a Comment