|
for (initialization; termination; increment) {
statements
}
where
initialization is a statement that initialize the loop and
is executed once at the beginning of loop;termination is a statement that evaluates to a boolean
value (i.e. true or false) and determines whether the
loop is terminated or continued;increment is a statement that gets invoked at each
iteration step.
initialization;
while (termination) {
statements;
increment;
}
Any (or all of these components can be empty statements (a single semicolon
by itself).
for (;;) {
statements
}
represents an infinite loop that can only be terminated by an explicit
break statement.
A common example is the following loop in which the arguments of a Java application are handled one-by-one and printed in reverse format.
public class ReverseWords {
public static void main(String args[]) {
for (int i=0; i<args.length; i++) {
System.out.println(
(new StringBuffer(args[i])).reverse()
);
}
}
}
When you run this application with the arguments "madam
is a palindrome", then you will see on you terminal screen the lines
madam si a emordnilap