While Loops
While Loops
Note that the variable declaration and initialization happen before the start
of the while loop and any changes that occur to the variable happen within
the body of the curly braces {}. On the other hand, everything happens in
one step within parentheses () when using a for loop.
Here is another example of a while loop that prints Hello based on the
value of the variable count.
while (4 > -1 * 4)
while (3 > -1 * 3)
while (2 > -1 * 2)
while (1 > -1 * 1)
Once the condition gets to while (0 > -1 * 0), it no longer holds true and
the while loop ends. The result is 5 Hellos being printed to the screen.
Infinite Loops
Infinite loops are loops that do not have a test condition that causes them to
stop. The following is a common mistake that results in an infinite loop:
Since the variable count never gets decremented. It remains at 5, and 5 will
forever be greater than 0, so the loop will never stop.
warning
Copy the code above and TRY IT to see what happens. Java will
eventually stop the loop due to an output limit, but it may take some
time before this happens.
int player_lives = 3;
Instead of a for loop, recreate the images below using a while loop.
Challenge 1
.guides/img/TurtleChallenge1
Hint
The pattern is still the same:
Challenge 2
.guides/img/TurtleChallenge2
Hint
Since a circle has 360 degrees, you will need a loop that repeats 360 times.
Be careful about how far the turtle moves forward and turns. The circle
can get very big, very quickly.
Challenge 3
.guides/img/TurtleChallenge3
Hint
The pattern here is to move forward and make a right turn.
The trick lies within the fact that the distance the turtle moves has to get
larger as the loop advances. Think of some operators that you can use to
make the loop iterator variable get bigger during each iteration.
Sample Solutions
tina.penColor("blue");
tina.shape("arrow");
tina.speed(200);
int i = 0;
while (i < 4) {
tina.forward(75);
tina.right(90);
tina.forward(25);
tina.right(90);
tina.forward(25);
tina.right(90);
tina.forward(25);
i++;
}
tina.penColor("red");
tina.shape("square");
tina.speed(10);
int i = 0;
while (i < 360) {
tina.forward(1);
tina.right(1);
i++;
}
tina.penColor("green");
tina.shape("triangle");
tina.speed(100);
int i = 10;
while (i <= 200) {
tina.forward(i);
tina.right(90);
i+=10;
}
info
NOTE
In most cases, for loops and while loops can be used interchangeably.
It is up to you to decide which one is better suited for your task.
Break Statement
Take a look at the following program (also shown in the text editor on the
left).
import java.util.Random;
while(true){
System.out.println("This is an infinite loop");
int randNum = random.nextInt(100) + 1; // random integer
between 1 and 100
Then click on the TRY IT button below a few times to run the code and see
the resulting outputs. You can also click on the ++Code Visualizer++ link
below to see how the code runs behind-the-scenes.
Even though while(true) will always evaluate as a true statement, the loop
never becomes infinite because of the break; statement.
challenge
The while loops introduced on the previous pages look different from the
while loop covered on this page; however, they both have the same
components and behave similarly.