Programming often requires performing the same task multiple times. Writing the same code repeatedly is inefficient and difficult to maintain. This is where loops come into the picture.
Loops allow us to execute a block of code repeatedly until a specified condition becomes false.
Why Do We Need Loops?
Suppose we want to print 1 five times.
Without loops, we would need to write:
let count = 0;
if (count < 5) {
console.log(1);
count++;
}
// Repeat the same block 4 more times
Repeating the same block several times increases the number of lines and makes the program difficult to manage.
To avoid this repetition, JavaScript provides loops.
The while Loop
A while loop is a shorter and more efficient way to execute the same block of code multiple times.
Syntax
while(condition) {
// code to execute
}
The loop continues executing as long as the condition evaluates to true.
Example 1: Print 1 Five Times
let count = 0;
while (count < 5) {
console.log(1);
count++;
}
Output
1
1
1
1
1
How It Works
-
countstarts at 0. - The condition
count < 5is checked. - Since the condition is true,
1is printed. -
count++increases the value by 1. - The process repeats until
countbecomes 5. - At that point, the condition becomes false and the loop stops.
Infinite Loop
Consider the following code:
let count = 0;
while (count < 5) {
console.log(1);
}
Since count is never incremented, the condition always remains true.
As a result, the loop keeps running forever, creating an infinite loop.
Therefore, always ensure that the loop variable is updated properly.
Example 2: Print Numbers from 1 to 5
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
Output
1
2
3
4
5
Explanation
- Initial value = 1
- Ending value = 5
- Interval = +1
The variable increases by one after each iteration.
Example 3: Print Numbers from 5 to 1
let i = 5;
while (i >= 1) {
console.log(i);
i--;
}
Output
5
4
3
2
1
Explanation
- Initial value = 5
- Ending value = 1
- Interval = -1
The variable decreases after each iteration.
Example 4: Find the Sum of the First Five Numbers
let i = 1;
let sum = 0;
while (i <= 5) {
sum += i;
i++;
}
console.log(sum);
Output
15
Step-by-Step
| Iteration | Value of i | Sum |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
| 5 | 5 | 15 |
Example 5: Print Odd Numbers
Odd numbers can be generated by increasing the value by 2 in each iteration.
let i = 1;
while (i <= 9) {
console.log(i);
i += 2;
}
Output
1
3
5
7
9
Explanation
- Initial value = 1
- Ending value = 9
- Interval = 2
Components of a Loop
Every loop generally contains three important parts:
1. Initial Value
let i = 1;
Determines where the loop starts.
2. Condition
i <= 5
Determines when the loop should continue executing.
3. Increment or Decrement
i++;
or
i--;
Changes the loop variable after each iteration.
Short Summary
- Loops help avoid writing repetitive code.
- A
whileloop executes repeatedly as long as its condition is true. -
Every loop requires:
- Initial value
- Condition
- Increment or decrement
Forgetting to update the loop variable results in an infinite loop.
Loops can be used to print numbers, calculate sums, and generate patterns efficiently.
References
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while?utm_source=chatgpt.com
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration?utm_source=chatgpt.com
https://www.w3schools.com/js/js_loop_while.asp?utm_source=chatgpt.com
https://javascript.info/while-for?utm_source=chatgpt.com
Top comments (0)