for ( initialization ; test ; increment ) statementThe initialization expression sets the loop variable to its first value (a loop variable can be declared here if required). The test expression is evaluated at the beginning of the loop after initialization expression. If the test expression is true the loop body statement is executed, at this point the increment expression is computed. If the test expression remains true the loop body statement is re-executed. This process of test, loop execution, incrementation continues until the test expression becomes false. The loop body statement can be either a simple statement or a compound statement, that is, a group of statements separated by the ; character and enclosed in curly brackets {}.
for (type loop-variable-identifier : expression ) statementHere "type" is the data type of the loop-variable-identifier, so that
type loop-variable-identifierforms a declaration. The expression after the : character must evaluate to an object or array the elements of with are commensurate with loop-variable-identifier. At each iteration of the loop the successive elements of the expression are assigned to the loop-variable-identifier and this variable is available for use in the loop body statement. The loop iterates through all of the elements of the expression. As before, the loop body statement can be a simple statement or a compound statement.
while (test) statementThe test expression is evaluated at the beginning of the loop. If the test expression is true the loop body statement is executed. If the test expression remains true the loop is re-executed. This process continues until the test expression becomes false. The loop body statement can be either a simple statement or a compound statement. When using a while loop any loop variables must be declare and initialized before the while statement. In addition, any loop variable incrementation must be performed in the loop body statement.
do statement while (test);The do loop first executes the loop body statement then evaluates the test expression. If the test expression is true the loop body statement is re-executed. This loop repartition ends when the test expression evaluates to false. In contrast to the other three types of loop the do loop always executes the loop body statement at least once. As is similar to the while loop, any loop variables must be declare and initialized before do statement and any loop variable incrementation must be performed in the loop body statement.
/**
Example of Java flow control.
We demonstrate four loop types in Java the Java if statement ,integer variables, String[] args, array access and the println method.
*/
class JavaLoops
{
public static void main(String[] args)
{
int i; //This integer is used in the for while and do loops.
System.out.println("Example of the for loop");
for(i=0;i<args.length;i++)
{
System.out.println("Argument number " + i);
System.out.println("is " + args[i]);
}
System.out.println("Example of the foreach loop");
for(String strLoop: args)
{
System.out.println("Argument is "+strLoop);
}
System.out.println("Example of the while loop");
i=0;
while(i<args.length)
{
System.out.println("Argument number " + i);
System.out.println("is " + args[i]);
i++;
}
System.out.println("Example of the do loop");
if (args.length>0)
{
i=0;
do
{
System.out.println("Argument number " + i);
System.out.println("is " + args[i]);
i++;
}while(i<args.length);
}
}
}
$ javac JavaLoops.java $ java JavaLoops Felix Krull Example of the for loop Argument number 0 is Felix Argument number 1 is Krull Example of the foreach loop Argument is Felix Argument is Krull Example of the while loop Argument number 0 is Felix Argument number 1 is Krull Example of the do loop Argument number 0 is Felix Argument number 1 is Krull $Note