Loops
General For Loop:
Syntax:
for ( <init> ; <boolean-expression> ; <step> )
{
<the statement(s) to repeat>
}
- <init>: initialization
to be done before the loop starts
- <boolean-expression>: condition to
stay in the loop (
true
), or leave (false
)
- <the statement(s) to repeat>
- <step> takes us closer
to stopping. (Then go back to Step 2 and check condition.)
Examples
for ( int i = 0; i < 20; i++ )
{
}
for ( int i = 1; i < 101; i = i + 5 )
{
}
for ( int i = 1; i < 100; i = i * 2 )
{
}
for ( ; clock.getHour() < 10; clock.changeTime() )
{
}
Alyce Brady, Kalamazoo College