Java While Loop – Tutorial With Programming Examples

In this tutorial, we will discuss  Java While Loop, comparison between For Loop vs While Loop, etc with programming examples:

All the necessary details related to Java while loop will be explained in this tutorial. It includes the syntax, description, and some relevant programming examples along with the logic of the programs.

Also, this tutorial will include some frequently-asked questions or the trending questions that are being asked on the topic.

=> Explore The Simple Java Training Series Here

Java while loop

Java While Loop

We will cover the below topics as a part of this tutorial.

  • While loop
  • Infinitive while loop

Apart from the above-mentioned sub-topics, we will also discuss a brief comparison between Java for-loop and while loop through the programs so that you can accomplish the same task using two different, yet common iteration statements.

As we know, we have three types of iteration statements in Java i.e. for, while, and do-while. These statements are known as loops that are used to execute a particular instruction repeatedly until it finds a termination condition.

Java while loop is a fundamental loop statement that executes a particular instruction until the condition specified is true.

Syntax:

while(condition) { 

// instructions or body of the loop to be executed
}

Flowchart

Java While Loop Flowchart

Printing The First 10 Natural Numbers

Given below is the program where we have printed the first 10 natural numbers using a while loop. Here, we have initialized the count variable with 1 as the natural numbers start from 1.

Then, we have started a while loop with the condition “i<=10”, hence it will keep on printing ‘i’ until the value reaches 11. After each iteration, the value of ‘i’ will be incremented by 1.

public class example {

	public static void main(String[] args) {
		
		// natural number starts from 1
		int i=1;
		
		/*
		 * while loop starts here in which we have specified
		 * a condition i <=10, so it will keep on printing i until
		 * the value reaches 11. After each iteration, the value
		 * of i will be incremented by 1.
		 */
		
		while (i <=10){
			System.out.println(i);
			i++;
		}
	}
}

Output

Printing the first 10 natural numbers - Output

Java For Loop Vs While Loop

In Java, the for loop and while loop is the most common statements that are used for iteration. In this section, we will see how we can accomplish the same result through both these iteration statements.

Given below is the program where we have initialized an ArrayList with three elements and then tried to iterate the ArrayList using for loop and while loop respectively.

import java.util.*;

public class example {
	public static void main(String[] args) {
		ArrayList list = new ArrayList();
		list.add("Saket's");
		list.add("Loop");
		list.add("example");
		
		// getting the size of the arraylist
		System.out.println(list.size());
		
		/*
		 * In while loop, iterating through the arraylist
		 * using the inbuilt methods hasNext() and next().
		 * The hasNext() method will check if there is
		 * an element exists and next() method will
		 * print that element.
		 */
		
		System.out.println("While Loop starts here:");
		Iterator itr = list.iterator();
		while(itr.hasNext()) {
			System.out.println(itr.next());
		}
		
		/*
		 * In for loop, iterating with a count variable initialized
		 * with 0 and it will be less than the size of the arraylist
		 * which is 3. The count variable will be incremented by 1
		 * after each iteration.
		 */
		
		System.out.println();
		System.out.println("For Loop starts here:");
		for(int i=0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
	}
}

Output

Java for loop vs while loop - output

Infinite While Loop

As the name suggests, an infinite while loop is a loop that will go on forever i.e. if you pass “true” in the condition or specify any condition that will satisfy the loop forever (even after each iteration/increment/decrement), then the loop will become an infinite loop that will execute until the user halts the execution.

Given below is an example of an infinite while loop.

Note: We have externally halted the execution while capturing the output of the below program after a few seconds of its execution.

public class example {

	public static void main(String[] args) {
		
		while (true){
			System.out.println("infinite while loop");
			
		}
	}
}

Output

Infinitive while loop - output

Frequently Asked Questions

Q #1) What is While Loop in Java?

Answer: Java while loop is the most fundamental loop or iteration statement. This statement or loop is used to execute a piece of code several times until it finds a termination condition.

Q #2) How do you use a While Loop in Java?

Answer: Given below is a sample program where we have used the while loop. This is one of the simplest programs on a while loop to see how we can use a while loop.

public class example {

	public static void main(String[] args) {
		
		// initialized the counter variable with 0
		int i=0;
		
		/*
		 *  specified condition i < 5 and based on this
		 *  condition, 'i' will be printed. After each 
		 *  iteration, the value if 'i' will be incremented
		 *  by 1.
		 */
		
		while (i < 5){
			System.out.println(i);
			i++;
		}
	}
}

Output

Use of While Loop in Java

Q #3) What are the 3 types of Loops in Java?

Answer: The 3 Types of Loops in Java include for loop, while loop, and do-while loop.

Q #4) How do you end a While Loop?

Answer: We can end a while loop using a break statement.

Given below is the program (just like FAQ2), where we have put a break statement after the increment.

This will cause the loop to end and return the result immediately after one iteration.

public class example {

	public static void main(String[] args) {
		
		int i=2;
		
		while (i < 10){
			System.out.println(i);
			i++;
			break;
		}
	}
}

Output

End of While loop - output

Q #5) Which loop is faster in Java?

Answer: Java for loop is faster than the while loop or do-while loop.

Conclusion

In this tutorial, we have discussed Java While Loop in detail along with the syntax and example. Apart from this, we had an insight into the control flow of the while loop. We saw a programmatic comparison between Java for loop and while loop.

Infinitive while loop where you can specify conditions that will be met even after the increment/decrement of the loop have also been explained. This will make your while loop to execute infinite times.

In our next tutorial, we will cover its other variation i.e. Java do-while loop. Then, we will provide a detailed comparison between for loop, while loop, and do-while loop.

=> Visit Here To Learn Java From Scratch