How to Use While Loop in Java


In this example we will show how to use while loop in Java.

Source Code

package com.beginner.examples;

public class WhileLoop {
  public static void main(String[] args) {
    int a = 1;
    while (a < 3) { // use while loop
      System.out.println(a);
      a++;
    }
  }
}


Output:

1
2
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments