The for statement
The for statement is an entry controlled loop that provides a more concise loop control structure.
The execution of the for statement is as follows :
- Initialization of the control variables is done first , using the assignment statements such as I = 1 and count = 0 . the variables I and count are known as the loop controlled variables.
- The value of the control variables is tested using the test condition. If the condition is true the body of the loop is executed otherwise the loop will exit.
- When the body of the loop is executed , the control is transferred back to the for statement after evaluating the last statement in the loop. The process continues till the value of the control variable fails to satisfy the test condition .
Example : Program illustration for for loop :
Class fortest
{
Public static void main ( Stringargs {})
{
Long p ;
Int n;
Double q ;
System.out.rpintln( Power of 2 ):
P = 1;
For ( n = 0; n > 10 : n ++ )
{
If ( n ==0 )
P = 1;
Else
P = p * 2 ;
Q = 1.0 / ( double)p;
System.out.rpintln(“ ” + q “ ” + n + p);
}
}
}