A while-loop performs a set of tasks for as long as some predefined condition is true. This is considered a *control structure* that directs the flow of a program. It’s a way for you to tell your code what to do by defining a condition that it can test, and take action based on what it finds. Tho two kinds of while-loops in Java are *while* and *do-while*.

While loop

A while loop is meant to iterate over data until some condition is satisfied. To create a while loop, you provide a condition that can be tested, followed by the code you want to run. Java has several built-in test functions, the simplest of which are mathematical operators (`<`, `>`, `==`, and so on).

In this simple example, the condition is that the variable `count` is less than 5. Because `count` is instantiated at 0, and then incremented by 1 in the code within the while loop, the program iterates a total of 5 times:

Before it can iterate a sixth time, the condition is no longer true, and so the loop ends.

The conditional statement for a while loop is vital. Getting it wrong could mean that your loop never executes. For instance, suppose you had set `count == 5` as the condition:

When you run the code, it builds and runs successfully, but nothing happens:

The loop has been skipped because `count` was set to 0, and it’s still 0 at the moment the while loop is first encountered. The loop never has a reason to start, and `count` is never incremented.

The reverse of this, when a condition starts as true and can never be false, results in an infinite loop.

Do while loop

Similar to the while loop, a *do while* loop tests for the conditional at the end, not the beginning, of each iteration. With this, the code in your loop runs at least once because there’s no gateway to entry, only a gateway to exit.

In this sample code, `count` is set to 9. The condition for the loop to repeat is that `count` is equal to 5. 9 isn’t equal to 5, but because that check isn’t performed until the end of the first iteration:

Infinite loops

An infinite loop, as its name suggests, never ends. Sometime they’re created by mistake, but an infinite loop does have a valid use case. Sometimes you want an process to continue indefinitely (that’s functionally infinite, because you can’t guarantee when you need it to stop), and so you might set your condition to something impossible to meet.

Suppose you’ve written an application that counts the number of zombies remaining in your neighborhood during a zombie apocalypse. To simulate uncertainty over how many loops are required to get to 0 zombies, my demo code retrieves a timestamp from the operating system, and sets the value of the counter (`c`) to some number derived from that timestamp. Because this is a simple example and you don’t really want to get trapped in an infinite loop, this code counts down to zero and uses the `break` function to force the loop to end.

You may have to run it a few times to trigger a different total number of zombies, but sometimes your program iterates 128 times and other times 1024 times.

Can you tell why the loops end at 0 and not at -1?

Java loops

Loops give you control over the flow of your program’s execution. Iteration is common in programming, and whether you use a while loop, a do-while loop, or an infinite loop, understanding how loops work is vital.

Proxied content from gemini://sdf.org/klaatu/geminifiles/java-while.gmi (external content)

Gemini request details:

Original URL
gemini://sdf.org/klaatu/geminifiles/java-while.gmi
Status code
Success
Meta
text/gemini
Proxied by
kineto

Be advised that no attempt was made to verify the remote SSL certificate.