Loops


General For Loop:

Syntax:
for ( <init> ; <boolean-expression> ; <step> )
{
    <the statement(s) to repeat>
}
  1. <init>: initialization to be done before the loop starts
  2. <boolean-expression>: condition to stay in the loop (true), or leave (false)
  3. <the statement(s) to repeat>
  4. <step> takes us closer to stopping. (Then go back to Step 2 and check condition.)

Examples

    for ( int i = 0; i < 20; i++ )
    {
        // Print a random phrase.
    }

    for ( int i = 1; i < 101; i = i + 5 )
    {
        // Print numbers, counting by 5's.
    }

    for ( int i = 1; i < 100; i = i * 2 )
    {
        // Print powers of 2 less than 100.
    }

    for ( ; clock.getHour() < 10; clock.changeTime() )
    {
        // Do something until 10:00.
    }

Alyce Brady, Kalamazoo College