The for statement

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 :

  1. 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.
  2. 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.
  3. 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);

}

}

}

 

Leave Comment

Important Topics

Title
OOPS
Data Types
JAVA
JVM
Command Line Args
Machine Neutral
Scope of Variables
Operators :
Generic Type Casting
IF Else
Switch Statement
The while statement
The do statement
The for statement
Classes, objects and methods
Constructors
Methods Overloading
Static Members
Nesting of methods
Inheritance : Extending a Class
Overriding methods
Overriding methods
Final variable and methods
Abstract class in Java
Visibility control