Java while loop

While loop almost similar to the for loop but slight difference is, if you don't know how many iterations should go. In this case while loop is suggested to use, the best example for the while loop is iterating through collections as iterator hasNext.

while loop example

public class WhileLoopExample {

	public static void main(String[] args) {

		int i=1;
			while(i<=5)
			{
			System.out.println("Printing number of iteration count : "+i);
			i++;
			}

	}
}
output
Printing number of iteration count : 1
Printing number of iteration count : 2
Printing number of iteration count : 3
Printing number of iteration count : 4
Printing number of iteration count : 5

While loop almost similar to the for loop but slight difference is, if you don't know how many iterations should go. In this case while loop is suggested to use.