How do I use the while loop statement?

Category: fundamental, viewed: 779 time(s).

The code below demonstrate how to use the while loop statement. The while statement will check if its expression evaluates to true and execute the while statement block.

The program below will executes while the countDown value is bigger or equals to zero.

package org.kodejava.example.lang;

public class WhileDemo {
    public static void main(String[] args) {
        //
        // Start the count down from 10
        //
        int countDown = 10;

        //
        // Do the count down process while the value of
        // countDown is bigger or equals to zero.
        //
        while (countDown >= 0) {
            System.out.println(countDown);
            countDown--;

            try {
                //
                // Adds one second delay.
                //
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats