Lessons

Java tutorial

Java While Loop

A while loop repeats its body as long as the condition stays true. Something inside the loop has to change that condition, or the loop will not end.

Example

Edit the code and run it again. This runner stays in your browser and covers the syntax used in these lessons.

Output

Run the example to see output here.

What this code does

count starts at 1. The body prints it and adds 1. After 3 is printed, count becomes 4 and the condition ends the loop.