For Loops: Printing Numbers II in JavaScript


Let's see an example where we change the iteration statement.

We can print all even numbers from 2 through 10:

for (let i = 2; i <= 10; i += 2) {
	console.log(i);
}

The output of this code is:

2
4
6
8
10

Let's break down this code:

  • The initialization statement is let i = 2, so we start iterating from number 2.

  • The condition statement is i <= 10 so we end at number 10. We could've written i < 11 and it would still be correct.

  • The iteration statement is i += 2, so we increase our number by 2 every time.


Assignment

Let's print all odd numbers from 7 through 23 using a for loop.


Hint
Look at the examples above if you get stuck.


Introduction

In this lesson, we will explore how to use for loops in JavaScript to print a sequence of numbers. Specifically, we will focus on printing odd numbers within a given range. Understanding how to manipulate the iteration statement in a for loop is crucial for controlling the flow of your loops and is a fundamental skill in programming.

For loops are commonly used in scenarios where you need to repeat a block of code a certain number of times, such as iterating over arrays, generating sequences, or performing repetitive calculations.

Understanding the Basics

A for loop in JavaScript consists of three main parts:

Here is a simple example of a for loop that prints numbers from 1 to 5:

for (let i = 1; i <= 5; i++) {
  console.log(i);
}

Main Concepts

To print all odd numbers from 7 through 23, we need to adjust the initialization, condition, and iteration statements accordingly:

Examples and Use Cases

Let's implement the for loop to print all odd numbers from 7 through 23:

for (let i = 7; i <= 23; i += 2) {
  console.log(i);
}

The output of this code will be:

7
9
11
13
15
17
19
21
23

This loop is useful in scenarios where you need to process or display only odd numbers within a specific range, such as filtering data or generating specific sequences.

Common Pitfalls and Best Practices

Here are some common mistakes to avoid when using for loops:

Best practices include:

Advanced Techniques

For more advanced use cases, you can combine for loops with other control structures, such as if statements, to create more complex logic. For example, you can use a for loop to iterate over an array and use an if statement to filter out specific elements:

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let i = 0; i < numbers.length; i++) {
  if (numbers[i] % 2 !== 0) {
    console.log(numbers[i]);
  }
}

Code Implementation

Here is the complete code to print all odd numbers from 7 through 23:

for (let i = 7; i <= 23; i += 2) {
  // Print the current value of i
  console.log(i);
}

This code initializes the loop variable i to 7, checks if i is less than or equal to 23, and increments i by 2 in each iteration, printing the value of i each time.

Debugging and Testing

When debugging for loops, consider the following tips:

To test your loop, you can write test cases that verify the output for different ranges and conditions. For example:

function printOddNumbers(start, end) {
  for (let i = start; i <= end; i += 2) {
    console.log(i);
  }
}

// Test case
printOddNumbers(7, 23); // Should print odd numbers from 7 to 23

Thinking and Problem-Solving Tips

When approaching problems involving loops, consider the following strategies:

Practice is key to mastering loops. Try solving different problems that require loops to build your confidence and understanding.

Conclusion

In this lesson, we covered how to use for loops in JavaScript to print a sequence of numbers, specifically focusing on odd numbers. We discussed the basics of for loops, provided examples, and highlighted common pitfalls and best practices. By mastering these concepts, you will be better equipped to handle repetitive tasks and control the flow of your programs effectively.

Keep practicing and exploring different applications of for loops to deepen your understanding and improve your coding skills.

Additional Resources

For further reading and practice, consider the following resources: