Java Tutorial

Java methods, java classes, java file handling, java how to, java reference, java examples, java while loop.

Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code more readable.

The while loop loops through a block of code as long as a specified condition is true :

In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:

Try it Yourself »

Note: Do not forget to increase the variable used in the condition, otherwise the loop will never end!

Advertisement

The Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:

Do not forget to increase the variable used in the condition, otherwise the loop will never end!

Test Yourself With Exercises

Print i as long as i is less than 6.

Start the Exercise

Get Certified

COLOR PICKER

colorpicker

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

[email protected]

Top Tutorials

Top references, top examples, get certified.

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

The while and do-while Statements

The while statement continually executes a block of statements while a particular condition is true . Its syntax can be expressed as:

The while statement evaluates expression , which must return a boolean value. If the expression evaluates to true , the while statement executes the statement (s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false . Using the while statement to print the values from 1 through 10 can be accomplished as in the following WhileDemo program:

You can implement an infinite loop using the while statement as follows:

The Java programming language also provides a do-while statement, which can be expressed as follows:

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once, as shown in the following DoWhileDemo program:

About Oracle | Contact Us | Legal Notices | Terms of Use | Your Privacy Rights

Copyright © 1995, 2022 Oracle and/or its affiliates. All rights reserved.

IMAGES

  1. While Loop in Java

    java assignment while loop

  2. Java do-while loop with Examples

    java assignment while loop

  3. Java while loop com exemplos

    java assignment while loop

  4. Java do while loop

    java assignment while loop

  5. The While Loop in Java

    java assignment while loop

  6. The Do While Loop in Java

    java assignment while loop

VIDEO

  1. Java Programming Session 08

  2. Loops in Java

  3. 11. Java While Loop, Example programs

  4. for loop java explanation #shorts

  5. JAVA

  6. JAVA

COMMENTS

  1. Java While Loop

    Syntax Get your own Java Server. while (condition) { // code block to be executed } In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:

  2. The while and do-while Statements (The Java™ Tutorials

    If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following WhileDemo program:

  3. Java while Loop (with Examples)

    The while loop in Java continually executes a block of statements until a particular condition evaluates to true. As soon as the condition becomes false, the while loop terminates. As a best practice, if the number of iterations is not known at the start, it is recommended to use the while loop. 1. Syntax. The syntax of while loop is: