Class Xii Notes
Class Xii Notes
While vs Do … While
Parameter While Do-While
Loop body is executed, and
Loop body is executed after the given
Definition then the given condition is
condition is evaluated.
checked.
Variable Variables are initialized before the Variables may initialize
Initialization execution of the loop. before or within the loop.
Loop Type Entry Control Loop Exit Control Loop.
// Output: // 1 // 2 // 3 // 4 // 5 // 6 // 7 // 8
// 9
do….while • The do...while loop is closely
related to while loop. In a
do...while loop, condition is
checked at the end of each
Syntax iteration of the loop, rather than
do at the beginning before the loop
runs.
{ • This means that code in a
// statement do...while loop is guaranteed to
run at least once, even if the
} condition expression already
while (condition); evaluates to true.
let i = 1;
do
{
console.log(i);
i++;
} while (i < 10);
// Output: // 1 // 2 // 3 // 4 // 5 // 6 //
7 // 8 // 9
For loop
❖initialization - This expression runs before the execution of the first
loop, and is usually used to create a counter.
❖condition - This expression is checked each time before the loop
runs. If it evaluates to true, the statement or code in the loop is
executed. If it evaluates to false, the loop stops. And if this
expression is omitted, it automatically evaluates to true.
❖finalExpression - This expression is executed after each iteration
of the loop. This is usually used to increment a counter, but can be
used to decrement a counter instead.
Syntax:
• for (initialization; condition; finalExpression)
• {
• statement
• }
for (let i = 0; i < 9; i++)
{
console.log(i);
}
// Output: // 0 // 1 // 2 // 3 // 4 //
5 // 6 // 7 // 8
ARRAY OBJECT
• Reverse()
• Short
Ways to create an Array []
• const cars = ["Tomato", "Potato", "Chili"];
• const cars = [
"Tomato",
"Potato",
"Chili"
];
• const cars = [];
cars[0]= "Tomato";
cars[1]= "Potato";
cars[2]= "Chili";
• const cars = new Array("Saab", "Volvo", "BMW");
//Program to create an array.
<html>
<body>
<script>
var i;
var fruits=new Array();
fruits[0]="apple";
fruits[1]="banana";
fruits[2]="orange";
for(i=0;i<fruits.length;i++)
{
document.write(fruits[i]+"<br>");
}
</script>
</body>
</html>
CREATING AN ARRAY
• Using an array literal is the easiest way to
create a JavaScript Array.
• Syntax:
const array_name = [item1, item2, ...];
POP()