What is Loop?
- A loop is used to execute a block of code repeatedly as long as a specified condition is true. The loop stops when the condition becomes false.
- A loop is used to perform the same task multiple times without writing the same code repeatedly.
Without Loop:
console.log("Welcome");
console.log("Welcome");
console.log("Welcome");
console.log("Welcome");
console.log("Welcome");
With Loop:
let i=1;
while(i<=5)
{
console.log("Welcome");
i++;
}
Welcome
Welcome
Welcome
Welcome
Welcome
Why do we use loop?
- Reduce the lines of code.
- Without loops, we need to write the same code multiple times manually. Using loops, we can execute a block of code repeatedly and automatically until the specified condition becomes false.
- Easier to maintain.
- Used to iterate arrays, objects...
Types of Loop:
There are two types of Loops:
Entry Controlled Loops:
- Any loop where we check the test condition before entering the loop is an entry controlled loop.
- If the condition is false initially, the loop body will not execute even once.
Exit Controlled Loops:
- Any loop where we check the test condition after the statements are executed once is an exit controlled loop.
- The loop body executes at least once, even if the condition is initially false.
While loop
do...while loop
for loop
for .. in loop
for .. of loop
While loop:
- While loop is an entry-controlled loop.
- It executes the block of code when the condition is true, when the condition becomes false, the loop stops.
Syntax:
while(condition)
{
//code
}
Example:
// While loop
let n=1;
while(n<=5)
{
console.log(n);
n++;
}
Output:
1
2
3
4
5
Here:
- Check 1 <= 5 → true → print 1
- Check 2 <= 5 → true → print 2
- Check 3 <= 5 → true → print 3
- Check 4 <= 5 → true → print 4
- Check 5 <= 5 → true → print 5
- Check 6 <= 5 → false → stop the loop
do.. While Loop:
- Do... While is a exit-controlled loop.
- The loop body executes at least once, even if the condition is initially false.
- The condition is checked after executing the loop body.
Syntax:
do
{
//code
}while(condition)
Example:
//do while
let i = 10;
do {
console.log(i);
i++;
} while (i <= 5);
Output:
10
Here, i <= 5 is initially false, but 10 is printed because the loop body executes before the condition is checked.
For Loop:
- For loop is an entry-controlled loop.
- For loop is used when the number of iteration is known.
- In for loop, initialization, condition, and increment/decrement are written in the same line, separated by semicolons (;).
Syntax:
for (initialization; condition; increment/decrement) {
// code to execute
}
Three Parts of for Loop:
for (let i = 1; i <= 5; i++)
↑ ↑ ↑
| | |
Initialization Condition Update
Initialization → Executes only once at the beginning.
Condition → Checked before every iteration.
Increment/Decrement → Updates the value after each iteration.
Example:
//for loop
for (let i = 1; i <= 5; i++) {
console.log(i);
}
Output:
1
2
3
4
5
for .. in Loop:
- The for...in loop is used to iterate over the keys (property names) of an object.
- In each iteration, the variable contains the key/index, not the value.
Syntax:
for (let key in object) {
// code
}
Example:
// for in
let Data={
name:"Raksha",
location:"Thiruvarur",
isStudent:true,
age:21
}
for(let key in Data)
{
console.log(key);
}
Output:
name
location
isStudent
age
Example:
let animals=["Lion","Tiger","Cat"]
for(let index in animals)
{
console.log(index);
}
Output:
0
1
2
for .. of Loop:
- The for...of loop is used to iterate over the values of an iterable objects.
- It is commonly used with arrays, strings, Sets, and Maps.
- It directly gives you the value, not the key/index.
Syntax:
for (let value of iterable) {
// code
}
Example:
let fruits = ["Apple", "Banana", "Mango"];
for (let fruit of fruits) {
console.log(fruit);
}
Output:
Apple
Banana
Mango


Top comments (0)