Java - Print all integers between 1 and 10 using a while-loop

Description

Print all integers between 1 and 10 using a while-loop

Demo

public class Main {
  public static void main(String[] args) {
    int i = 1;//www  . ja v  a 2  s .c  om
    while (i <= 10) {
      System.out.println(i);
      i++;
    }

  }
}

Result

The above code can also be rewritten as

Demo

public class Main {
  public static void main(String[] args) {
    int i = 0;/*from  w  ww  .  ja  va  2 s.c  o  m*/
    while (++i <= 10) {
      System.out.println(i);
    }

  }
}

Result

Related Example