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.
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.
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.
Let's break down the key concepts and techniques involved in using the while
loop:
true
.false
.In the examples provided, we use these concepts to print sequences of even and odd numbers.
Let's revisit the examples provided and explain them step-by-step:
int i = 4;
while(i <= 14) {
System.out.println(i);
i += 2;
}
In this example:
i
to 4.i <= 14
ensures the loop runs as long as i
is less than or equal to 14.i
and then increment it by 2.int i = 12;
while(i >= 4) {
System.out.println(i);
i -= 2;
}
In this example:
i
to 12.i >= 4
ensures the loop runs as long as i
is greater than or equal to 4.i
and then decrement it by 2.Now, let's solve the assignment:
int i = 23;
while(i >= 7) {
System.out.println(i);
i -= 2;
}
In this assignment:
i
to 23.i >= 7
ensures the loop runs as long as i
is greater than or equal to 7.i
and then decrement it by 2.When using while
loops, be mindful of the following:
false
.Best practices include writing clear and concise loop conditions and ensuring your loop has a well-defined exit condition.
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.
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;
}
To debug and test your while
loops:
When approaching problems involving loops:
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.
For further reading and practice, consider the following resources: