4.1.3.4.2 Unconditional Repetition: for-Loop


The general format of a for-loop is as follows:
for (initialization; termination; increment) { 
  statements 
}
where The above for-loop is equivalent to
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