Lesson 6 – Iteration Statements

Iteration statements, which are also known as loops, are used to repeatedly execute a particular piece of code. There are three types of iteration statements (loops) in Java

  • For Loop
  • While Loop
  • Do Loop

For Loop

The for loop is used to repeatedly execute a piece of code, n number of times where n is any integer. In the following script, the for loop is used to print the first 5 integers.

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub


for(int i = 1; i<=10; i++)
{
System.out.println(i);
}
}
}

The output of the above script is as follows:

1
2
3
4
5
6
7
8
9
10

The for loop can be used to traverse an array as shown below:

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub


int [] student_age = new int [5];
student_age[0] = 10;
student_age[1] = 8;
student_age[2] = 11;
student_age[3] = 13;
student_age[4] = 9;

for (int i = 0; i <5 ; i++)
{
System.out.println(student_age[i]);
}
}
}

In the script above, we create “student_age” array with 5 elements and print its elements via the for loop. The output looks like this:

10
8
11
13
9

While Loop

The while loop is repeatedly execute a piece of code unless a certain condition becomes false. Let’s see how the while loop can be used to print the first 10 integers on the console.

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub


int k = 1;
while(k <=10)
{
System.out.println(k);
k++;
}
}
}

You can see that in the above script we initialized a variable k with value 1. The while loop will keep executing until k is less than or equal to 10. Inside the while loop, we print the value of k and then increment its value. After 10 iterations, the value of k becomes 11, hence the while loop condition becomes false, terminating the loop. In the output, you will again see the first 10 positive integers.

Do-While Loop

The do-while loop is similar to a while loop in the sense that it also terminates when a certain condition becomes false. However unlike while loop where the condition is evaluated at the beginning of the loop, the do-while loop evaluates the test condition at the end of the loop, which means that the do-while loop executes at least once. The following example prints the 5 items in the “student_age” array.

 

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub


int [] student_age = new int [5];
student_age[0] = 10;
student_age[1] = 8;
student_age[2] = 11;
student_age[3] = 13;
student_age[4] = 9;

int j = 0;
do {
System.out.println(student_age[j]);
j++;
}
while(j < 5);

}

}

You can see from the above code that the “while” condition is checked at the end of the do loop. In the output, you should see the five items from the “student_age” array as shown below:

10
8
11
13
9

<h3>What’s Next?</h3>

In this lesson you studied iteration statement, in the next lesson you will see functions, also known as methods, in Java.