Java while loop is used to execute a block of statements continuously till the given condition is true. Earlier we looked into java for loop.
Java while loop
While loop in java syntax is as follows.
1 2 3 4 5 |
while (expression) { // statements } |
The expression
for while loop must return boolean value, otherwise it will throw compile time error.
While loop java flow diagram
Java while loop example
Here is a simple java while loop example to print numbers from 5 to 10.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.journaldev.javawhileloop; public class JavaWhileLoop { public static void main(String[] args) { int i = 5; while (i <= 10) { System.out.println(i); i++; } } } <img class="alignnone wp-image-30651 size-full" src="https://all-learning.com/wp-content/uploads/2017/10/java-while-loop-examples.png" alt="java-while-loop-example" width="1200" height="792" /> |
Notice that I am increasing value of i inside while loop, otherwise the while loop will never terminate and we will have an infinite loop. The only way to terminate the program running in infinite loop is to manually quit it or when the JVM runs out of memory.
Also note that if boolean expression returns false then statements inside while loop won’t execute. So there is a chance that statement inside while loop will never execute.
Java while loop with Iterator
Java while loop is used a lot with iterator in java. Let’s see a short example of iterating over an ArrayList using while loop.
1 2 3 4 5 6 7 8 9 10 |
List<String> veggies = new ArrayList<>(); veggies.add("Spinach"); veggies.add("Potato"); veggies.add("Tomato"); Iterator<String> it = veggies.iterator(); while(it.hasNext()) { System.out.println(it.next()); } |
It will print output as below.
1 2 3 4 5 |
<span style="color: #008000;"><strong><code> Spinach Potato Tomato </code></strong></span> |
while true java
Sometimes we intentionally want our while loop to run infinitely. In that case we can use while true loop. An example could be looking for a file at specific location continuously and if found then process it. Below is a pseudo code example of while true loop in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.journaldev.javawhileloop; public class WhileTrueJava { public static void main(String[] args) { while(true) { System.out.println("Start Processing"); // look for a file at specific directory // if found then process it, say insert rows into database System.out.println("End Processing"); // wait for 10 seconds and look again try { Thread.sleep(10*1000); } catch (InterruptedException e) { System.out.println("Thread Interrupted, exit now"); System.exit(0); } } } } |
If we run above program, we will have to manually quit it using Ctrl+C
on terminal. If you are using Eclipse then there is a red button to terminate the current running program.
Let’s say you have written some code inside a class and it’s work in progress so you don’t want to get it executed. Can we just wrap it inside while false loop so that it will not execute at all?
Well java compiler is smart enough to give us compilation error that code is unreachable. Below are images from Eclipse editor as well as when we are trying to compile the program from terminal.
But there is another way to achieve this, use a dummy boolean variable and the compiler error will be gone.
1 2 3 4 5 6 |
boolean mutex = false; while(mutex) { System.out.println("incomplete code"); } |
That’s all about java while loop, I hope you get clear idea how and when to use while loop in java.
Reference: Oracle Documentation