[Go to site: main page, start]

0% found this document useful (0 votes)
4 views4 pages

JavaScript Loops

JavaScript loops allow code to run multiple times based on a condition, enhancing efficiency and organization. There are three main types of loops: for, while, and do-while, each with specific syntax and use cases for iterating over arrays, objects, or executing code repeatedly. The document provides examples and explanations for each loop type, including their initialization, condition checking, and termination processes.

Uploaded by

monikamr.it
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

JavaScript Loops

JavaScript loops allow code to run multiple times based on a condition, enhancing efficiency and organization. There are three main types of loops: for, while, and do-while, each with specific syntax and use cases for iterating over arrays, objects, or executing code repeatedly. The document provides examples and explanations for each loop type, including their initialization, condition checking, and termination processes.

Uploaded by

monikamr.it
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JavaScript Loops



Loops in JavaScript allow a block of code to run multiple times as long as a given
condition is satisfied. They help reduce repetition and make programs more
efficient and organized.
 Loops continue running until the condition becomes false.
 They are useful for iterating over arrays, strings, and ranges of values.
In JavaScript, there are three types of Loops :

1. for Loop
The for loop repeats a block of code a specific number of times. It contains
initialization, condition, and increment/decrement in one line.
Syntax
for (initialization; condition; increment/decrement) {
// Code to execute}
Example: The below JavaScript program for loop runs from i = 1 to i = 3,
incrementing i by 1 each time, and prints "Count:" followed by the current value of
i.

for (let i = 1; i <= 3; i++) {


[Link]("Count:", i);
}
The image below demonstrates the flow chart of a for loop:

 Initialization condition: Here, we initialize the variable in use. It marks the


start of a for loop. An already declared variable can be used or a variable can
be declared, local to loop only.
 Testing Condition: It is used for testing the exit condition for a loop. It must
return a boolean value. It is also an Entry Control Loop as the condition is
checked prior to the execution of the loop statements.
 Statement execution: Once the condition is evaluated to true, the statements
in the loop body are executed.
 Increment/ Decrement: It is used for updating the variable for next iteration.
 Loop termination:When the condition becomes false, the loop terminates
marking the end of its life cycle.
1.1 for in loop
The for-in loop iterates over the enumerable keys of an object.
Syntax:
for (let key in object) {
// code to be executed
}
Example: The below JavaScript program for-in loop iterates over the keys of the
person object and prints each key with its corresponding value.

const person = { name: "Alice", age: 22, city: "Delhi" };

for (let key in person) {


[Link](key, ":", person[key]);
}
1.2 For each loop
The forEach loop executes a provided function once for each element in an array.
Syntax:
[Link](function(element, index, array) {
// code to execute
});
Example: The below JavaScript program forEach loop executes a function for
each element in the numbers array, printing each number.

const numbers = [10, 20, 30];

[Link](function(num) {
[Link](num);
});
2. while Loop
The while loop executes as long as the condition is true. It can be thought of as a
repeating if statement.
Syntax
while (condition) {
// Code to execute
}
Example: The below JavaScript program while loop prints "Number:" followed by
i repeatedly while i is less than 3, incrementing i by 1 each time.

let i = 0;
while (i < 3) {
[Link]("Number:", i);
i++;
}
The image below demonstrates the flow chart of a while loop:

 While loop starts with the checking of Boolean condition. If it evaluated to true,
then the loop body statements are executed otherwise first statement following
the loop is executed. For this reason it is also called Entry control loop
 Once the condition is evaluated to true, the statements in the loop body are
executed. Normally the statements contain an update value for the variable
being processed for the next iteration.
 When the condition becomes false, the loop terminates which marks the end
of its life cycle.
3. do-while Loop
The do-while loop is similar to while loop except it executes the code block at
least once before checking the condition.
Syntax
do {
// Code to execute
} while (condition);
Example: The below JavaScript program do-while loop prints "Iteration:" followed
by i, increments i by 1, and repeats the process while i is less than 3, ensuring
the block runs at least once.

let i = 0;
do {
[Link]("Iteration:", i);
i++;
} while (i < 3);
The image below demonstrates the flow chart of a do-while loop:

 do while loop starts with the execution of the statement. There is no checking
of any condition for the first time.
 After the execution of the statements, and update of the variable value, the
condition is checked for true or false value. If it is evaluated to true, next
iteration of loop starts.
 When the condition becomes false, the loop terminates which marks the end
of its life cycle.
 It is important to note that the do-while loop will execute its statements a tleast
once before any condition is checked, and therefore is an example of exit
control loop.

You might also like