Java while loop example

Java while loop FAQ: Can you show me an example of a Java while loop?

Sure, here's a Java while loop example:

package com.devdaily.javasamples;

public class WhileTest
{
  public static void main(String[] args)
  {
    int i = 0;
    while (i < 5)
    {
      System.out.println("i = " + i);
      i++;
    }
  }
}

This example prints out the value of the variable i until i gets to 5, at which point the loop stops.

Note that there are several other ways to increment the variable i, but I've shown it as a separate line as it's the most obvious way.