0% found this document useful (0 votes)
9 views

Lecture-4 Looping Statements

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture-4 Looping Statements

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

INSTITUTE OF INFORMATION TECHNOLOGY & MANAGEMENT

Accredited ‘A’ Grade by NAAC &Recognised U/s 2(f) of UGC act


Rated Category `A+’ by SFRC & `A’ by JAC Govt. of NCT of Delhi
Approved by AICTE & Affiliated to GGS Indraprastha University, New Delhi

Lecture 4: Control Statement:-


Looping
BCA
Semester: I
Paper code:-BCA 105T

© Institute of Information Technology and Management, D-29, Institutional Area,


Janakpuri, New Delhi-110058
Looping Or Repetition Structure
• While writing a program, there may be a situation
when you need to perform some action over and
over again. In such situation you would need to
write loop statements to reduce the number of
lines.
• Repetition structure: 4 in JavaScript
1. while
2. do…while
3. for
4. for…in
© Institute of Information Technology and
Management, D-29, Institutional Area, Janakpuri,
New Delhi-110058
while Loops
Syntax:
while (expression)
{
Statement(s) to be executed if
expression is true
}
• The purpose of a while loop is to execute a
statement or code block repeatedly as long as
expression is true. Once expression becomes false,
the loop will be exited.
© Institute of Information Technology
and Management, D-29, Institutional
Area, Janakpuri, New Delhi-110058
Example

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

• The do...while loop is similar to the while loop


except that the condition check happens at the end of
the loop. This means that the loop will always be
executed at least once, even if the condition is false.
Syntax:
do
{
Statement(s) to be executed;
} while (expression);
© Institute of Information Technology
and Management, D-29, Institutional
Area, Janakpuri, New Delhi-110058
Example:

<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:

• The continue statement tells the interpreter


to immediately start the next iteration of the
loop and skip remaining code block.
• When a continue statement is encountered,
program flow will move to the loop check
expression immediately and if condition
remain true then it start next iteration
otherwise control comes out of the loop.
© Institute of Information Technology
and Management, D-29, Institutional
Area, Janakpuri, New Delhi-110058
Example:

<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

You might also like