You are here: Home / Topics / While Loop Example in Java

While Loop Example in Java

Filed under: Java on 2023-08-18 06:54:25

// simple while loop example

public class WhileLoop{

   public static void main(String[] args) {

       //learning
       int a = 1;
       while (a <= 5) {
           System.out.println(" "+a);
           a++;
       }

       //exploring
       int sum = 0, b = 1;
       while (b <= 5) {
           sum += b;
           b++;
       }
       System.out.println("\nSum of 1-5 is " + sum);

   }
}


Output:

1  2  3  4  5

Sum of 1-5 is 15

About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.