While loop: Printing more numbers in Java


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:

int i = 4;
while(i <= 14) {
	System.out.println(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:

int i = 12;
while(i >= 4) {
	System.out.println(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.


Introduction

In this lesson, we will explore the use of the while loop in Java to print sequences of numbers. The while loop is a fundamental control flow statement that allows code to be executed repeatedly based on a given Boolean condition. Understanding how to use loops effectively is crucial for solving many programming problems, such as iterating over data structures, performing repetitive tasks, and more.

Understanding the Basics

The while loop in Java repeatedly executes a block of statements as long as a specified condition is true. The 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.

Main Concepts

Let's break down the key concepts and techniques involved in using the while loop:

In the examples provided, we use these concepts to print sequences of even and odd numbers.

Examples and Use Cases

Let's revisit the examples provided and explain them step-by-step:

Example 1: Printing Even Numbers from 4 to 14

int i = 4;
while(i <= 14) {
    System.out.println(i);
    i += 2;
}

In this example:

Example 2: Printing Even Numbers from 12 to 4 in Reverse

int i = 12;
while(i >= 4) {
    System.out.println(i);
    i -= 2;
}

In this example:

Assignment: Printing Odd Numbers from 23 to 7 in Decreasing Order

Now, let's solve the assignment:

int i = 23;
while(i >= 7) {
    System.out.println(i);
    i -= 2;
}

In this assignment:

Common Pitfalls and Best Practices

When using while loops, be mindful of the following:

Best practices include writing clear and concise loop conditions and ensuring your loop has a well-defined exit condition.

Advanced Techniques

Advanced techniques with while loops include nested loops, using loops for complex data processing, and combining loops with other control flow statements like if and switch for more sophisticated logic.

Code Implementation

Here is the complete code for the assignment:

int i = 23;
while(i >= 7) {
    // Print the current value of i
    System.out.println(i);
    // Decrement i by 2 to get the next odd number in decreasing order
    i -= 2;
}

Debugging and Testing

To debug and test your while loops:

Thinking and Problem-Solving Tips

When approaching problems involving loops:

Conclusion

Mastering the while loop is essential for any programmer. It allows you to perform repetitive tasks efficiently and is a building block for more complex algorithms. Practice writing and debugging loops to become proficient in their use.

Additional Resources

For further reading and practice, consider the following resources: