Lecture-4 Looping Statements
Lecture-4 Looping Statements
Example:
<body>
<script type="text/javascript">
var count = 0;
document.write("Starting Loop" + "<br />");
while (count < 10)
{
document.write("Current Count : " + count +
"<br />"); count++;
}
document.write("Loop stopped!");
</script> © Institute of Information Technology
</body> and Management, D-29, Institutional
Area, Janakpuri, New Delhi-110058
The do...while Loop
<body>
<script type="text/javascript">
var count = 0;
document.write("Starting Loop" + "<br />");
do
{
document.write("Current Count : " + count + "<br />");
count++;
}while (count < 0);
document.write("Loop stopped!");
</script>
© Institute of Information Technology
</body> and Management, D-29, Institutional
Area, Janakpuri, New Delhi-110058
The for Loop
• The for loop is the most compact form of looping and includes the
following 3 important parts:
1. The loop initialization where we initialize our counter to a starting
value. The initialization statement is executed before the loop begins.
2. The test statement which will test if the given condition is true or not.
If condition is true then code given inside the loop will be executed
otherwise loop will come out.
3. The iteration statement where you can increase or decrease your
counter.
Syntax:
for (initialization; test condition; iteration statement)
{
Statement(s) to be executed if test condition is true
} of Information Technology and Management, D-29, Institutional Area, Janakpuri, New
© Institute
Delhi-110058
Example
<script type="text/javascript">
var count;
document.write("Starting Loop" + "<br />");
for(count = 0; count < 10; count++)
{
document.write("Current Count : " + count );
document.write("<br />");
}
document.write("Loop stopped!");© Institute of Information Technology
and Management, D-29, Institutional
</script> Area, Janakpuri, New Delhi-110058
for…in Loop
• For – In statement : The for…in statement loops through the
elements of an array or through the properties of an object.
The 'for - in' loop is used to cycle through all exposed
properties of an object (or array). Every time you create
properties or methods on an object, these will be added to the
list of properties that will be exposed.
Syntax –
for ( variable in object)
{
// code to be executed
}
© Institute of Information Technology and
Management, D-29, Institutional Area, Janakpuri, New
Delhi-110058
Example
<body>
<script type =“text/javascript”>
var x;
var mycars = Array(3);
mycars[0] = "BMW";
mycars[1] = "Audi";
mycars[2] = "Benz";
document.write("Hello");
for (x in mycars )
{
document.write(mycars[x] + "<br\>");
}
</script>
© Institute of Information Technology
</body> and Management, D-29, Institutional
Area, Janakpuri, New Delhi-110058
Dynamic Array
<script type ="text/javascript" language="javascript">
var x;
var mycars = [];
mycars[0] = "BMW";
mycars[1] = "Audi";
mycars[2] = "Benz";
for (x in mycars )
{
document.write(mycars[x] + "<br\>");
}
}
</script> © Institute of Information Technology
and Management, D-29, Institutional
Area, Janakpuri, New Delhi-110058
Branching Control Statement
• JavaScript provides you full control to handle your loops
and switch statement.
• There may be a situation when you need to come out of a
loop without reaching at its bottom.
• There may also be a situation when you want to skip a part
of your code block and want to start next iteration of the
look.
• To handle all such situations, JavaScript provides break
and continue statements.
• These statements are used to immediately come out of any
loop or to start the next iteration of any loop respectively.
© Institute of Information Technology
and Management, D-29, Institutional
Area, Janakpuri, New Delhi-110058
The break Statement:
• The break statement, which was briefly
introduced with the switch statement, is used to
exit a loop early, breaking out of the enclosing
curly braces.
Example:
• This example illustrates the use of a break
statement with a while loop. Notice how the loop
breaks out early once x reaches 5 and reaches to
document.write(..) statement just below to closing
curly brace.
© Institute of Information Technology and Management, D-
29, Institutional Area, Janakpuri, New Delhi-110058
<script type="text/javascript">
var x = 1; OUTPUT
document.write("Entering the loop<br /> ");
Entering the
while (x < 20)
{ loop
if (x == 5) 2
{ 3
break; // breaks out of loop completely 4
}
5
x = x + 1;
document.write( x + "<br />"); Exiting the
} loop!
document.write("Exiting the loop!<br /> "); © Institute of Information Technology
</script> and Management, D-29, Institutional
Area, Janakpuri, New Delhi-110058
The continue Statement:
<script type="text/javascript">
var x = 1; OUTPUT:-
document.write("Entering the loop<br /> "); Entering the loop
while (x < 10) 2
3
{
4
x = x + 1;
6
if (x == 5)
7
{
8
continue; // skip rest of the loop body 9
} 10
document.write( x + "<br />"); Exiting the loop!
}
document.write("Exiting the loop!<br /> "); © Institute of Information Technology
and Management, D-29, Institutional
</script> Area, Janakpuri, New Delhi-110058