Using the while loop to calculate sum : While Loop « Statement Control « Java Tutorial






public class MainClass {
  public static void main(String[] args) {
    int limit = 20;
    int sum = 0;
    int i = 1;

    while (i <= limit) {
      sum += i++;
    }
    System.out.println("sum = " + sum);
  }
}
sum = 210








4.4.While Loop
4.4.1.The while Statement
4.4.2.Using the while loop to calculate sum
4.4.3.While loop with double value
4.4.4.Java's 'labeled while' loop