SCJP : Flow control – Loops & iterators


Naomu

There are four forms of loops in Java: two for loops (a three-part for & a foreach for), while & do.

while Loops

Code within a while loop is executed if, and then while, a provided boolean expression evaluates to true.

while (aBooleanExpression) {

//do some stuff

}

After each execution of the code in the while loop’s body, the while condition is re-tested to determine if it should be run again.

Initialisation cannot happen in the condition of a while loop (for a start, nothing about that value would ever change…)

do Loops

Also known as do while loops. Any code within a do block is guaranteed to execute at least once. A while condition is then added after the do block for repetitions.

do {

//some stuff

}

while (aBooleanCondition);

Basic for Loops

A traditional Java three-part for loop.

for (int i = 0; i < 5; i++)…

View original post 413 more words