Practice makes perfect! Let's check two more examples of printing numbers using the while loop.
Example 1:
Let's print all the even numbers from 4
through 14
:
let i = 4;
while(i <= 14) {
console.log(i);
i += 2;
}
The output of this program is:
4
6
8
10
12
14
Example 2:
We can also loop in reverse. Let's print all even numbers from 12
to 4
in decreasing order:
let i = 12;
while(i >= 4) {
console.log(i);
i -= 2;
}
The output of this program is:
12
10
8
6
4
Assignment
Let's print all the odd numbers from 23
to 7
in decreasing order!
Hint
Look at the examples above if you get stuck.
In this lesson, we will explore the use of the while
loop in JavaScript to print sequences of numbers. The while
loop is a fundamental control structure that allows code to be executed repeatedly based on a given Boolean condition. Understanding how to use loops effectively is crucial for tasks such as iterating over data structures, automating repetitive tasks, and implementing complex algorithms.
The while
loop continues to execute a block of code as long as the specified condition evaluates to true
. The basic syntax of a while
loop is:
while (condition) {
// code to be executed
}
It's important to ensure that the condition will eventually become false
; otherwise, the loop will run indefinitely, causing an infinite loop.
Let's break down the key concepts and techniques involved in using the while
loop:
true
.false
.In the examples provided, we initialize a variable i
, set a condition for the loop, and update i
within the loop to print sequences of numbers.
Let's look at the examples provided in the lesson:
let i = 4;
while (i <= 14) {
console.log(i);
i += 2;
}
This loop starts with i
initialized to 4 and prints even numbers up to 14 by incrementing i
by 2 in each iteration.
let i = 12;
while (i >= 4) {
console.log(i);
i -= 2;
}
This loop starts with i
initialized to 12 and prints even numbers down to 4 by decrementing i
by 2 in each iteration.
Now, let's solve the assignment:
let i = 23;
while (i >= 7) {
console.log(i);
i -= 2;
}
This loop starts with i
initialized to 23 and prints odd numbers down to 7 by decrementing i
by 2 in each iteration.
Here are some common mistakes to avoid and best practices to follow:
false
.Once you're comfortable with basic loops, you can explore more advanced techniques such as nested loops, using loops with arrays, and combining loops with conditional statements to solve more complex problems.
Here is the complete code for the assignment:
let i = 23;
while (i >= 7) {
console.log(i);
i -= 2;
}
This code demonstrates how to use a while
loop to print odd numbers from 23 to 7 in decreasing order.
When debugging loops, consider the following tips:
console.log
to print the loop variable and check its value at each iteration.When approaching problems involving loops, consider these strategies:
In this lesson, we explored the use of the while
loop in JavaScript to print sequences of numbers. We covered the basics, examined examples, and discussed best practices and common pitfalls. Mastering loops is essential for efficient programming and problem-solving. Keep practicing and experimenting with different loop scenarios to enhance your skills.
For further reading and practice, consider the following resources: