Iteration

Now lets have a quick look at the iteration statements which have the ability to loop through a set of values to solve real-world problems.

Iteration

Now lets have a quick look at the iteration statements which have the ability to loop through a set of values to solve real-world problems.

Iteration

Iteration

     

The concept of Iteration has made our life much more easier. Repetition of similar tasks is what Iteration is and that too without making any errors. Until now we have learnt how to use selection statements to perform repetition. Now lets have a quick look at the iteration statements which have the ability to loop through a set of values to solve real-world problems. 

The for Statement

In the world of Java programming, the for loop has made the life much more easier. It is used to execute a block of code continuously to accomplish a particular condition. For statement consists of tree parts i.e. initialization, condition, and iteration.
 initialization : It is an expression that sets the value of the loop control variable. It executes only once.
 condition : 
This must be a boolean expression. It tests the loop control variable against a target value and hence works as a loop terminator. 
 iteration :
It is an expression that increments or decrements the loop control variable.
 Here is the form of the for loop:

for(initialization; condition; iteration){
//body of the loop
}

For example, a sample for loop may appear as follows: 
int i;
for (i=0; i<10; i++)
System.out.println("i = " +i); 

In the above example, we have initialized the for loop by assigning the '0' value to i. The test expression, i < 100, indicates that the loop should continue as long as i is less than 100. Finally, the increment statement increments the value of i by one. The statement following the for loop will be executed as long as the test expression is true as follows:
System.out.println("i = " + i); 

Well, we can add more things inside a loop. To do so we can use curly braces to indicate the scope of the for loop. Like,

 int i;
for (i=0; i<10; i++) {
MyMethod(i);
System.out.println("i = " + i);


There is a simpler way to declare and initialize the variable used in the loop. For example, in the following code, the variable i is declared directly within the for loop: 
for (int i=0; i<100; i++) 
System.out.println("i = " +i); 

Lets see a simple example which will help you to understand for loop very easily. In this example we will print 'Hello World' ten times using for loop.

class printDemo{
  public static void main(String[] args){
  for (int i = 0; i<10; i++){
  System.out.println("Hello World!");
  }
  }
}

Here is the output:

C:\javac>javac printDemo.java

C:\javac>java printDemo
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

After learning how to use a for loop, I would like to introduce another form of for loop to be used for iteration through collection and arrays. This form has enhanced the working of for loop. This is the more compact way to use a for loop. 
Here we will take an array of 10 numbers.

int[] numbers = {1,2,3,4,5,6,7,8,9,10};

The following program, arrayDemo,displays the usage of for loop through arrays. It shows the variable item that holds the the current value from the array.

class arrayDemo{
  public static void main(String[] args){
  int[] numbers = {1,2,3,4,5,6,7,8,9,10};
  for (int item : numbers) {
  System.out.println("Count is: " + item);
  }
  }
}

Here is the output of the program 

C:\javac>javac arrayDemo.java

C:\javac>java arrayDemo
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

We would like to suggest to use this form of for loop if possible.