Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Java while loops

    Java Programming Tutorials


    Java while loops statement allows to repeatedly run the same block of code until a condition is met.

    while loop is the most basic loop in Java. It has one control condition and executes as long the condition is true. The condition of the loop is tested before the body of the loop is executed; hence it is called an entry-controlled loop.

    The basic format of while loop statement is:

    Syntax:

    While (condition)
    {
       statement(s);
       Incrementation;
    }

    Figure - Flowchart of while loop:

    java-while

    Example of a Java Program to Demonstrate while loop

    Example:

    public class Sample {
    
        public static void main(String args[]) {
            /* local variable Initialization */
            int n = 1, times = 5;
    
            /* while loops execution */
            while (n <= times) {
                System.out.println("Java while loops:" + n);
                n++;
            }
        }
    }

    Program Output:

    java-while-loop



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.