Java has three basic loop statements. A for loop repeats code a known number of times, a while loop repeats as long as a condition is true and checks it before each pass, and a do-while loop checks the condition after each pass, so its body always runs at least once.
This article explains loops in java with complete examples, shows when to choose each form, and covers break, continue, the enhanced for loop and the infinite-loop mistakes beginners meet. You should know variables, relational operators and if statements.
A for loop puts initialisation, condition and update on one line, which makes it the natural choice when you know how many times to repeat.
The three parts run in this order:
int i = 1) runs once, before the first pass.i <= 5) is tested before every pass; when it is false the loop ends.i++) runs after each pass of the body.The variable i is scoped to the loop, so it cannot be used after it. Any of the three parts may be left empty, and for (;;) is an infinite loop.
When you only need each element and not its index, use the enhanced for loop (also called for-each).
It works on arrays and anything that implements Iterable. You cannot change which element the loop variable points to in the array itself, and you cannot remove elements from a list inside it without risking a ConcurrentModificationException.
A while loop tests the condition first. If it is false at the start, the body never runs. Use it when the number of repetitions is not known in advance.
Each pass removes the last digit, and the loop stops when number reaches 0. Something inside the body must change the state that the condition depends on, otherwise the loop never ends.
A do-while loop runs the body first and tests the condition afterwards, so the body executes at least once. Note the semicolon after the condition.
This is the classic use: ask for input at least once, and ask again while the answer is invalid.
| Loop | Condition checked | Minimum runs | Typical use |
|---|---|---|---|
for | Before each pass | 0 | Counting, indexing arrays |
Enhanced for | Per element | 0 | Visiting every element |
while | Before each pass | 0 | Repeat until something happens |
do-while | After each pass | 1 | Menus, input validation |
All three are interchangeable in power; choose the one that makes the intent obvious.
break leaves the loop immediately. continue skips the rest of the current pass and moves to the next one.
In nested loops, break only leaves the innermost loop. To leave an outer loop, put a label on it, such as outer: for (...), and write break outer;.
i++, or using a condition that can never become false.i <= arr.length instead of i < arr.length throws ArrayIndexOutOfBoundsException.for (int i = 0; i < 3; i++);, which makes the body empty.for loop, which makes the flow hard to follow.float or double as a loop counter, where rounding can skip the end value.Start with for for counting and the enhanced for for arrays. Learn while for repeat-until logic and do-while for input checks.
While testing, stop the program from the terminal or IDE. In code, make sure the body changes the condition, or add an explicit break on a real exit condition.
while may run zero times because it tests first. do-while always runs once because it tests last.
Yes. Separate declarations and updates with commas, for example for (int i = 0, j = 9; i < j; i++, j--).
Use for when you can count the passes, the enhanced for when you only need the elements, while when you repeat until a condition changes, and do-while when the body must run at least once. Use break and continue sparingly, watch for off-by-one errors, and always make sure something in the loop moves it toward its exit.