Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do 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
Let’s look at some examples.
eg. 1
var a = 5;var b = 5;
if (a == b)
{
alert("The two quantities are equal");
}
eg. 2
<script type="text/javascript">
//Write a "Good morning" greeting if
//the time is less than 10
var d=new Date();
var time=d.getHours();
if (time<10)
{
document.write("<b>Good morning</b>");
}
</script>
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
Syntax:
if (condition)
{
code to be executed if condition is true
}else
{
code to be executed if condition is not true
}
eg.
var response = confirm("Have you understood the syntax of if-else?");
if (response == true) {
alert("Okay, let us continue");
}else
{
alert(”Go thru the previous session again”);
}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
{
code to be executed if condition1 and
condition2 are not true
}
Note that the else if clause is followed by another condition placed between parenthesis. If this condition is true, the statement inside the else if block are executed.
var d = new Date();
var t_hour = d.getHours();
if (t_hour <= 3)
{
alert("Hello dear visitor,nWorking late aren't we?");
}
else if (t_hour > 3 && t_hour < 12)
{
alert("Good morning dear visitor");
}
else if (t_hour >=12 && t_hour <= 16)
{
alert("Good afternoon dear visitor");
}
else
{
alert("Good Evening dear visitor");
}
We first initialize a variable using the new operator with the Date() contruct. The value of hours is stored in t_hour variable by employing the getHours() method of the date object. The value of t_hour variable is then passed through if-else if-else conditions. Thus, depending on the time of the day (time in hours), a greeting is displayed in an alert box.
You’ll recall that JavaScript clock is a 24 hour clock.
When t_hour is less than equal to 3 (0, 1, 2, or 3): alert box displays “Hello dear visitor,Working late aren’t we?”.
When t_hour is more than 3 AND less than 12: alert box displays “Good morning dear visitor”.
If the value of t_hour is greater than equal to 12 AND less than equal to 16: “Good afternoon dear visitor” is displayed in the alert box.
For all other values of t_hour (17, 18, 19, 20, 21, 22, and 23): alert box displays “Good Evening dear visitor”.
The JavaScript Switch Statement
You should use the switch statement if you want to select one of many blocks of code to be executed.
Syntax
switch(n){
case 1:
execute code block 1
break;
case 2:
execute code block 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.
var d=new Date();
theDay=d.getDay();
switch (theDay)
{
case 5:
document.write("Finally Friday");
break;
case 6:
document.write("Super Saturday");
break;
case 0:
document.write("Sleepy Sunday");
break;
default:
document.write("I'm looking forward to this weekend!");
}
One Trackback/Pingback
[...] Lesson -5. Conditional Statements [...]
Post a Comment