Imagine a teacher wants to greet 5 students: Without a loop, we need to write the same code multiple times.
Instead of writing the same type of code again and again, JavaScript provides loops. 🔄 What is a Loop?
A loop is used to execute a block of code repeatedly.
It helps us avoid writing the same code again and again.
A loop continues running based on a condition or a collection of values.
In simple words: A loop means repeating a task multiple times using code.
For example: This is the basic idea of a loop. 🤔 Why Do We Use Loops?
Loops are useful when the same task needs to be performed multiple times.
For example, without a loop: Using a loop: Output: If the task needs to be performed 100 or 1000 times, using a loop is much easier than writing the same code repeatedly. 📍 Where Are Loops Used?
Loops can be used in many situations, such as: Displaying a list of products Processing a list of students Reading values from an array Printing numbers Calculating marks Processing multiple records Repeating a task until a condition becomes false For example: ⏰ When Should We Use a Loop?
A loop can be used when: The same task needs to be performed multiple times.
For example: or: Different situations require different types of loops. 🔢 Types of Loops in JavaScript JavaScript provides different types of loops: loop loop loop loop loop Let's look at each loop one by one.
- 🔁 Loop The loop is used to execute a block of code repeatedly.
It is commonly used when the number of repetitions is known or can be controlled.
A loop has three main parts: initialization, condition, and update. 🌍 Real-World Example Imagine a teacher says: "Write your name 5 times." The task can be represented as: A loop can be used for this type of task.
Syntax Three Parts of a Loop
1.
Initialization Defines where the loop starts.
2.
Condition Checks whether the loop should continue.
If the condition is , the code executes.
If the condition is , the loop stops.
3.
Update Changes the value after each iteration.
Example Output: How Does It Work?
When to Use Loop?
Use a loop when the number of repetitions is known or can be controlled. - 🔄 Loop The loop executes a block of code as long as a condition is .
The condition is checked before executing the code.
If the condition is at the beginning, the code inside the loop will not execute. 🌍 Real-World Example Imagine charging a mobile phone.
The idea can be: The charging continues while the condition is true.
Syntax Example Output: The execution looks like this: ⚠️ Infinite Loop An infinite loop is a loop that never stops because its condition always remains .
For example: Here, is never changed.
So the condition always remains: The loop keeps running continuously. ❌ How to Avoid an Infinite Loop?
Make sure the value used in the condition is updated so that the condition can eventually become .
Here: changes the value: When becomes : So the loop stops.
Output: To avoid an infinite loop, make sure the loop condition can eventually become .
When to Use Loop?
Use a loop when the repetition mainly depends on a condition. - 🔁 Loop The loop is similar to the loop.
The main difference is that the code executes before the condition is checked.
Because of this, the code inside the loop executes at least once. 🌍 Real-World Example Imagine an ATM asking for a PIN.
The user needs to enter the PIN at least once.
The process can be represented as: Syntax Example Output: How Does It Work? vs For example: Nothing is printed because the condition is checked first: But: prints: because the code executes once before checking the condition.
When to Use ?
Use when the code needs to execute at least once. - 🔄 Loop The loop is used to iterate over the values of an iterable.
It is commonly used with arrays and strings.
It takes one value at a time.
In simple words: takes values one by one. 🌍 Real-World Example Imagine a bag containing: Taking the fruits one by one is similar to how works.
Syntax Example Output: Here, gets each v