Monday, April 22, 2013

For loop in java

For loop in java


In for loop initialization, condition check
and increment is done in one loop
format of for loop

for(initialization : condition : increment)

We can take the same example of table for
better understanding
class table
{
 public static void main(String[] args)
{
      int num=5,table=0,j;
      for(j=1;j<11;j++)
      {
          table=j*5;
         
          System.out.println(+table);
      }
}
}

In above program
            for(j=1;j<11;j++)
initialization is done
j=1
than condition is check
j<11
if it is true then body of loop is executed
after execution of body of loop
increment is done
j++
now j=2
then again condition check
this procedure is repeated until the condition is false
When condition gets false then execution of program goes out of the loop

No comments:

Post a Comment