The example below demonstrates a while loop.

// While loop

public class WhileLoop {
    public static void main(String [] args) {
	int x = 1;
	int y = 2;
	while (x < 100) {
	    x = x + y;
	    y = y * 2;
	    System.out.println("x = " + x + "  y = " + y);
	}
	System.out.println("After the loop: x = " + x + "  y = " + y);
    }
}

This is an example from CSci 2101 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.