Saturday, February 23, 2013

While loops



A while loop is a very simple loop type of loop. For loops allow you to easily iterate over a certain range of numbers, while loops iterate while a condition is true.

Having different kinds of loops in a program is really a matter of convenience rather than necessity. There really aren't any tasks that are not possible in either one, but there are some tasks that are much easier in one over the other.

Let's take a look at the anatomy of a while loop:

while(<condition>) {    <code to run>}

Condition

The loop will run while this condition is true. Common examples are i < 10, iterator.hasNext().

Code to run

Just like with a for loop, this is what you would like to run each iteration of the loop.

No comments:

Post a Comment