Unit I PPT CSS 3
Unit I PPT CSS 3
1
• Create interactive web pages
CO1 using program flow control
structure
2
TEACHING AND EXAMINATION SCHEME
3
CONTENTS
1. For loop
2. While loop
3. Do.. while loop
4. For… in loop
4
Department of Information Technology,Government
Polytechnic Awasari(kh)
FOR LOOP
• It is a looping statement which executes block of code N times.
Syntax:
//block of statements
}
• In for loop, program controller first goes to initialize section, after that it will test the
condition.
• if condition is true then it will executes body of for loop and after that goes to
increment/decrement section.
• Again it will check the condition and if condition is true then it will again executes
the body of for loop. This process will continue till condition becomes false.
5
Department of Information Technology,Government
Polytechnic Awasari(kh)
FOR LOOP- EXAMPLE
<html>
<body>
<script type = "text/javascript">
var i;
document.write("Output of For Loop=");
</script>
</body>
</html>
6
Department of Information Technology,Government
Polytechnic Awasari(kh)
WHILE LOOP
• ‘while’ is a predefined keyword of JavaScript.
• It is called as entry controlled loop.
• It is a looping statement which executes block of code N times.
Syntax:
while(condition)
{
//block of statements
7
Department of Information Technology,Government
Polytechnic Awasari(kh)
WHILE LOOP- EXAMPLE
<html>
<body>
<script type = "text/javascript">
var i=1;
i=i+1;
}
</script>
</body>
</html>
8
Department of Information Technology,Government
Polytechnic Awasari(kh)
DO-WHILE LOOP
• ‘do’ & ‘while’ both are predefined keyword of JavaScript.
• It is called as exit controlled loop.
• It is a looping statement which executes block of code N times.
Syntax:
do
{
//block of statements
}while(condition);
• In do- while loop, program controller first executes the body of loop and then test
the condition.
• If condition is true then it will again executes body of loop.
This process will continue till condition becomes false.
• In do-while loop, program controller executes body of loop at least once even if
condition becomes false very first time.
9
Department of Information Technology,Government
Polytechnic Awasari(kh)
DO-WHILE LOOP- EXAMPLE
<html>
<body>
<script type = "text/javascript">
var i=1;
document. Write("Output of For Loop=");
do
{
document. Write("Current value of i : " + i );
i=i+1;
}while(i<=10);
</script>
</body>
</html>
10
Department of Information Technology,Government
Polytechnic Awasari(kh)
FOR-IN LOOP
• for’ is a predefined keyword of JavaScript.
• The for...in loop is used to loop through an object's properties.
• In each iteration, one property from object is assigned to variable name
and this loop continues till all the properties of the object are exhausted.
Syntax:
for (variable name in object)
{
//block of code
}
Example:
<html>
<body>
<script type = "text/javascript">
var aProperty;
document.write("Navigator Object Properties<br> ");
11
Polytechnic Awasari(kh)
QUIZ TIME
Q1. What are the three important Q2. What happens in the following
javaScript code?
manipulations done in a for loop
on a loop variable? var count = 0;
while (count < 10)
a) Updation, incrementation, initialization {
b) Initialization, testing, Updation console.log(count);
c) incrementation, initialization, testing count++;
d) Testing, Updation, incrementation }
a) The values of count are logged or stored
in a particular location or storage
► Ans. b. Initialization, b) The value of count from 0 to 9 is
testing, Updation displayed in the console
c) An error is displayed
d) An exception is thrown
► Ans. b. The value of count
from 0 to 9 is displayed in
the console
12
Department of Information Technology,Government
Polytechnic Awasari(kh)
QUIZ TIME
Q3.What will happen if the body of a Q4. One of the special feature of an
for/in loop deletes a property that has interpreter in reference with the for
not yet been enumerated? loop is that ?
a. The property will be stored in a cache a. Before each iteration, the interpreter
b. The loop will not run evaluates the variable expression and assigns
c. That property will not be enumerated the name of the property
d. All of the mentioned b. The iterations can be infinite when an
interpreter is used
► Ans. c. That property will c. The body of the loop is executed only once
not be enumerated d. All of the mentioned
► Ans. a.
Before each iteration, the
interpreter evaluates the
variable expression and
assigns
13
Department of Information Technology,Government
Polytechnic Awasari(kh)
THANK YOU
Thank You
14
Polytechnic Awasari(kh)