Google+

58. 'for' iterative statements






For all the iterative statement types, 'for' loop is the most versatile (i.e. capable of doing many things).

'for' loop syntax                                                                                                                              
Lets first understand the 'for' loop syntax and then go to the example programs.
Syntax:
for(initialization;condition;iteration)
{
   Statement 1;
   Statement 2;
   .
   .
   Statement n;
 }
Initialization portion of the loop sets a loop control variable to an initial value.
for(i=0;condition;iteration)
{
     Statement 1;
     Statement 2;
   .
   .
     Statement n;
 }
Condition is a Boolean expression that tests the loop control variable. If the outcome of this Boolean expression is true, then the for loop continues to iterates else it gets terminated.
for(initialization;i<10;iteration)
{
    Statement 1;
    Statement 2;
    .
    .
    Statement n;
 }
Iteration expression determines how the loop control variable is changed each time the loop iterates.
for(initialization;condition;i++)
{
    Statement 1;
    Statement 2;
    .
    .
    Statement n;
 }

The flow of execution of for loop will be as shown below -

First, the intialization will be done
Second. the condition will be verified
Third, the statements inside the for loop will be executed if the above condition is true
Fourth, the value of variable will be incremented.
Fifth, the conditon will be verified
Sixth, the statements inside the for loop will be executed if the above condition is true
and so on ....

Program to demonstrate the 'for' loop                                                                                                                 

class ForLoopDemo
{
   public static void main(String args[])
   {

      int i;    //declaring the variable 'i'
 
      for(i=1;i<6;i++)
      {
         System.out.println("The value of i is "+i);
      }
   }
}

Output of this program:

The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
The value of i is 5

The for loop operates as follows. First the variable 'i' is initialized by a value '1'. Then the condition i<6 is evaluated i.e. 1<6 is pass, hence the program enters the loop and executes the statements inside the for loop block, i.e. It prints 'The value of i is 1'. After this the control goes to iteration expression i.e i++ i.e. The value of i got incremented by 1 i.e. i is 2 now. Now the condition expression i<6 i.e. 2<6 is evaluated and result is pass, hence the program enters the for loop block and prints 'The value of i is 2'. In this way the loop repeats until the condition express i<6 results true. Finally when the i is incremented from 5 to 6 and when i<6 is evaluated i.e. 6<6 fails. Then the for loop terminates.

Program to demonstrate that the variable can be declared during its initialization in for loop

class ForLoopDemo2
{
   public static void main(String args[])
   {
      for(int i=1;i<6;i++)  //Observe that the variable 'i' got declared during initialization itself i.e. int i=1
      {
         System.out.println("The value of i is "+i);
      }
   }
}

Output of this program:

The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
The value of i is 5

Program to demonstrate the flower braces can be ignored if the 'for' loop has only one statement

class ForLoopDemo3
{
   public static void main(String args[])
   {
      for(int i=1;i<6;i++)
        System.out.print(i+" ");   //As this is the only statement under the 'for' loop, we've ignored the flower braces i.e. { }
   }
}

Output of this program:

1 2 3 4 5

Observer the we've not used flower braces for the 'for' loop as we have only one statement

Program to demonstrate the scope of the variable that is declared during 'for' loop's variable initialization

class ForLoopDemo4
{
   public static void main(String args[])
   {

      for(int i=1;i<6;i++)
       i++;

      System.out.println("The value of i is "+i);

   }
}

Output of this program:

Error "cannot find symbol 'i' in System.out.println("The value of is i is "+i) "

This error is displayed because the 'i' is declared inside the 'for' loop block. Hence it wont be accessible outside the 'for' loop block.

Program to demonstrate that more than one variable can used in the for loop initialization  condition and iteration expressions

class ForLoopDemo5
{
   public static void main(String args[])
   {

      for(int i=1,j=3;i<j;i++,j--)            //Initialized more than one variablie i.e initialized i and j variables, iterated them and used in conditional expression
      {
        System.out.println("i = "+i);
        System.out.println("j = "+j);
      }
  }
}

Output of this program:

i=1
j=3

Program to demonstrate that initialization and iteration parts of the for loop can be empty

class ForLoopDemo6
{
   public static void main(String args[])
   {
  int i=0;

      for(;i<2;)            //Initialization part and Iterative expression parts are left blank in the 'for' statement
      {
        System.out.println("i = "+i);
        i++;
      }
  }
}

Output of the program:

i=0
i=1

Observe that the program still works even though we've left the initialization part and iterative expression parts of the 'for' statement blank.

Program to demonstrate the nested 'for' loops

class NestedForLoops
{
   public static void main(String args[])
   {

      for(int i=0;i<5;i++)
      {
           for(int j=0;j<5;j++)
             System.out.print(".");
          System.out.println();
       }

   }
}

Output of this program:
.....
.....
.....
.....
.....

Another program to demonstrate the nested for loop

class NestedForLoops2
{
   public static void main(String args[])
   {

      for(int i=0;i<5;i++)
     {
         for(int j=i;j<5;j++)
           System.out.print(".");
         System.out.println();
       }

   }
}

Output of this program:

.....
....
...
..
.





Please comment below to feedback or ask questions.

How to use the break statements will be explained in the next post.




4 comments:

MS said...

Hello Arun,

As a good practice which statements should be used in programs for looping from "while" and "for" ?

Also could you please explain at what situations these statements should be used?

Arun Motoori said...

@MS - As a good practice, we will be using for loop mostly.

Unknown said...

Your examples are very easy and impressive.
Thumps up !

Raveender Reddy Yalaka said...

Hi Arun,

**First of all hats off to your great effort in presenting this stuff.


I observed a small typo in this page , in last two examples.


the statement " System.out.println(); " should be out side the inner for loop. please update if i am not wrong.