Control flow statements – while and do-while loop

The while loop statement executes one or more lines of code as long as a certain expression evaluates to true. Here’s an example of a while loop:

When this method is executed, the output will be:

A while loop evaluates the condition before it executes any code in it. There’s an alternative to the while loop called the do-while loop. The only difference between these is that the do-while loop executes any code in it and then checks the condition to see if it should continue looping.

An example of a do-while loop with the same logic as the previous example:

As we can see, in this case the output was exactly the same.

Search for more Java info on this site here:
Custom Search

Any thoughts or questions?