Search This Blog

If you have any problem or question please write in the comments. We will try to solve and send you the solution. Not just that we will also post your problems solution on our blog.

Tuesday, 9 August 2011

The for loop

For loop:
                For loop is a powerful and versatile construct. Its general form is:
for (initialization;condition;iteration)
{
//body
}
If you have only one statement to repeat then you can do this without curly braces. The for loop operates as follows:
  • When the loop starts first time, the initialization portion of the loop is executed. Generally, this is an expression that sets the value of the loop control variable, which acts as a counter that controls the loop. It is important to understand that the initialization expression is only executed once.
  • Next, condition is evaluated. This must be a Boolean expression. If this expression is true then the body of the loop is executed. If it is false the loop terminates.
  • Next, the iteration portion of the loop is executed. This is usually an expression that increments or decrements the loop control variables.
This process will continue till condition is true. See a simple java program explaining all this.

Monday, 8 August 2011

A simple java program using for loop

class simplefor
{
public static void main (String arg[])
{
int a;
for (a=5;a>=0;--a)
{
System.out.print ("\n\t a = "+a);
}

}
}

Note:
        In this program, body of the loop will be executed till a is greater than or equals to 0 and value of a will be printed.