Java Part 2: Syntax - The while loop
(Page 6 of 8 )
The while loop is similar in action to the for loop, but different in grammar. They can always be interchanged. For example, the following while loop will iterate through the values of i until the truth test fails.
int i = 0;
while( i < 100)
{
// perform some action;
i++;
}as with the for loop, we can construct an infinite loop. Such a loop would look like this:
while( TRUE )
{
// perform some action
} Notice the indentation between the braces.
The do...while loopThe do...while loop is the same as a while loop, except that the body of the code will always be executed at least once.
do
{
// perform some action
} while( truth statement );Next: Putting it all together >>
More Java Articles
More By Chris Noack