Java   View all facts   Glossary   Help
syntactic unit > statement > control flow statement > loop statement
Next control flow statementreturn statement    Upcontrol flow statement    Previous control flow statementlabel statement   

loop statement comparison table
Subject have example have syntax have disadvantage have purpose has part interchange with have advantage
do-while loop
int c;
Reader in;
. . .
do {
c = in.read();
. . .
} while (c != -1);
for (entry expression; boolean expression; continuation expression) {
//statements to keep executing while boolean expression is true
}
 to execute a section of code more than once   
for loop
a is an array of some kind 
. . .
int i;
int length = a.length;
for (i = 0; i < length; i++) {
. . .
// do something to the i th element of a
. . .
}
for(initializer; condition; incrementer)
{
// statements to keep executing while condition is true
}
it can be slightly harder to read the code of a for loop than a while loopto execute a section of code more than onceinitializera while loopall the information about controlling the loop is kept in one place
while loop
//Decrement n by 1 until n is 0
while (n != 0) {
n = n - 1;
}
while(condition)
{
// statements to keep executing while condition is true
}
 to repeat a computation by looping through that computation until a test has been satisfied a for loop 

Next control flow statementreturn statement    Upcontrol flow statement    Previous control flow statementlabel statement