For Loop
Previous    Next    Home    Source    Package

  1. Syntax of a for expression:
       for (<intializer>; <condition>; <post-proc>) {
  2. The <initializer> is an expression that is executed once, before the loop begins. Variables that are declared in the <initializer> are recognized only within the body of the loop. In this example int i=0 means i is declared for this loop and is given the value 0 before the loop begins.
  3. The <condition> is an expression that evaluates to true or false. The loop repeats as long as the condition is true. In this example, the loop repeats as long as the value of variable i is less than the value of another variable called iter.
  4. The <post-proc> is an expression, or expressions that is executed after the loop completes an iteration and before the <condition> is checked for the next iteration. In this example, the <post-proc> is simply i++ which increments i by 1.
  5. The <body> is a sequence of expressions representing a program segment. Expressions in the <body> are executed until the end is reached or a break statement is executed (in that case, the loop terminates no matter what). In this case the body prints to the output window the values of i, then a newline, then the variable iter is incremented by 1.