Conditional Statements
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....else statement - use this statement if you want to select one of many blocks of
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 (condition)
{
code to be executed if condition is true
}
Note that if is written in lowercase letters. Using uppercase letters (IF) will generate a
JavaScript error!
Example 1
<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...else Statement
If you want to execute some code if a condition is true and another code if the condition is
not true, use
the if....else statement.
Syntax
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
Example
<script type="text/javascript">
//If the time is less than 10,
//you will get a "Good morning" greeting.
//Otherwise you will get a "Good day" greeting.
var d = new Date();
If...else if...else Statement
You should use the if....else if...else statement if you want to select one of many sets of lines
to execute.
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
}
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
}
Example
<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("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!");
}
</script>
JavaScript Controlling(Looping) Statements
Loops in JavaScript are used to execute the same block of code a specified number of times
or while
a specified condition is true.
JavaScript Loops
Very often when you write code, you want the same block of code to run over and over again
in a row.
Instead of adding several almost equal lines in a script we can use loops to perform a task like
this.
In JavaScript there are two different kind of loops:
for - loops through a block of code a specified number of times
while - loops through a block of code while a specified condition is true
The for Loop
The for loop is used when you know in advance how many times the script should run.
Syntax
for (var=startvalue;var<=endvalue;var=var+increment)
{
code to be executed
}
JavaScript While Loop
Loops in JavaScript are used to execute the same block of code a specified number of times
or while
a specified condition is true.
The while loop
The while loop is used when you want the loop to execute and continue executing while the
specified
condition is true.
while (var<=endvalue)
{
code to be executed
}
The do...while Loop
The do...while loop is a variant of the while loop. This loop will always execute a block of
code ONCE,
and then it will repeat the loop as long as the specified condition is true. This loop will
always be
executed at least once, even if the condition is false, because the code is executed before the
condition is
tested.
do
{
code to be executed
}
while (var<=endvalue);
JavaScript Break and Continue
There are two special statements that can be used inside loops: break and continue.
JavaScript break and continue Statements
There are two special statements that can be used inside loops: break and continue.
Break
The break command will break the loop and continue executing the code that follows after
the loop (if
any).
Example
<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=10;i++)
{
if (i==3)
{
break;
}
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
Continue
The continue command will break the current loop and continue with the next value.
Example
<html>
<body>
<script type="text/javascript">
var i=0
for (i=0;i<=10;i++)
{
if (i==3)
{
continue;
}
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>