Java looping statements

What are looping statements?

In some scenarios we need to execute same set of statements repeatedly. If we write all these statements repeatedly in our program we will get very large file. With this approach we will have multiple issues such as

  • Maintenance of program will be very difficult
  • Our program will not work when a number of times to repeat is unknown at the time of writing program.

To overcome these problems Java offers looping statements. By using these statements we can write all our statements to repeat only one time and execute them multiple times based on our requirement. There are four types of looping statements in Java.

  • while
  • do while
  • for
  • for each

while loop:

while loop is the simplest looping statement available in Java. This loop first checks for the given condition, if the condition is true then executes statements inside while block and again checks for the condition. This process continues until the condition is false. This way it is easy to execute set of statements repeatedly by writing only once.

Syntax:

while (condition)
{
   Statement 1 ;
   Statement 2 ;
   ------------
}
/*This is a simple Java program about while.
Call this file KH_while.java.*/
import java.util.*;

public class KH_while {
	// A Java program begins with a call to main().
	public static void main(String args[]) 	{
		int i;		
		i = 1;		
		while(i <= 10){
			System.out.println(i);
			i++;
		}
	}
}

Download the code  Run the code

Output:

1
2
3
4
5
6
7
8
9
10

do while loop:

As we have seen in the above while loop our statements will execute after condition is true. If the condition is false for the first time itself then our statements will not get executed. But in some cases we need to execute our statements at least once irrespective of condition result. To achieve this behaviour we can use do while loop. As the names suggests it first does the action and then check for condition. So in this loop our statements are guaranteed to execute at least once.

Syntax:

do 
{
   Statement 1 ;
   Statement 2 ;
   ------------
}while(condition);
/*
This is a simple Java program about do while.
Call this file KH_dowhile.java.
*/
import java.util.*;

public class KH_dowhile {
	// A Java program begins with a call to main().
	public static void main(String args[]) {
		int i;		
		i = 1;		
		do
		{
			System.out.println(i);
			i++;
		}while(i <= 10);
	}
}

Download the code  Run the code

Output:

1
2
3
4
5
6
7
8
9
10

for loop:

for loop is another java looping statement which is a simplified version of while loop. In for loop, we use iterator to control how many times the loop should repeat. At first iterator will be initialised and then it checks for the condition if condition is true then executes statements. After that it executes operation on iterator and then checks for condition. This process repeats until condition is false.

Syntax:

for (initialization; condition; operation)
{
   Statement 1 ;
   Statement 2 ;
   ------------
}
/*
This is a simple Java program about for.
Call this file KH_for.java.
*/
import java.util.*;

public class KH_for {
	// A Java program begins with a call to main().
	public static void main(String args[]) {
		for(int i = 1; i <= 10; i++){
			System.out.println(i);
		}
	}
}

Download the code  Run the code

Output:

1
2
3
4
5
6
7
8
9
10

for each loop:

As our program’s complexity is increasing day by day, we are using more and more arrays and collections. An array or collection simply is a set of elements referenced by single variable. To traverse each element multiple times, if we use any of the above mentioned loops, the readability of the program will be poor and prone to programing errors. To overcome this issue Java has introduced for each loop in Java 5. The variable that we define within for each syntax traverses through each element of array or collection.

Syntax:

for (data_type variable:array | collection)
{
   Statement 1 ;
   Statement 2 ;
   ------------
}
/*
This is a simple Java program about foreach.
Call this file KH_foreach.java.
*/
import java.util.*;

public class KH_foreach {
	// A Java program begins with a call to main().
	public static void main(String args[]) 
	{
		int array[] = {1, 2, 3, 4, 5};
		for(int i : array){
			System.out.println(i);
		}
	}
}

Download the code  Run the code

Output:

1
2
3
4
5

break and continue

We have discussed break while learning switch. In loops we can also use break in the same way, to come out of loop in the middle of execution. In the same way, continue also can be used, only difference is that continue does not exit the loop but it forces to skip any statements after continue and moves to the next iteration.

Below example gives more clarity on these two keywords.

/*
This is a simple Java program about break and continue in loops.
Call this file KH_Loop_break_continue.java.
*/
import java.util.*;

public class KH_Loop_break_continue {
	// A Java program begins with a call to main().
	public static void main(String args[]) {
		int i = 0;		
		while(i <= 20)	{
			i++;
			if(i == 13){
				break;
			}			
			if( i <= 5 ){
				continue;
			}			
			System.out.println(i);			
		}
	}
}

Download the code  Run the code

Output:

6
7
8
9
10
11
12

By looking at this output we can understand how these two keywords are working. Till the value of i is 5 continue keyword is forcing to skip println statement and moving to next iteration. That’s why we did not get output from 1 to 5. And at i value = 13,  break is executed and it is forcing to come out of loop, hence we did not get output after 12.

<<< Control Statements in JavaJava arrays >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .