Loops in Java: for, while and do-while

Every looping construct in Java, when each one fits, and how break, continue and labels change the flow.

Why loops exist

A loop repeats a block while a condition holds. Choosing the right one is mostly a question of what you know before the loop starts.

The for loop

for (int i = 1; i <= 5; i++) {
    System.out.println("Row " + i);
}

The three parts run in a fixed order: the initialiser once, then the condition before every pass, then the update after every pass. Use a for loop when the number of iterations is known or the index itself is needed.

The enhanced for loop

int[] marks = {45, 78, 62, 91};
int total = 0;

for (int mark : marks) {
    total += mark;
}
System.out.println("Total: " + total);

It reads over arrays and anything implementing Iterable. There is no index, no bounds arithmetic and therefore no off by one error. Prefer it whenever the index is not needed.

The loop variable is a copy. Assigning to mark inside the loop changes nothing in the array. To modify elements you need an indexed for loop.

The while loop

int balance = 1000;
int months = 0;

while (balance > 0) {
    balance -= 250;
    months++;
}
System.out.println("Months: " + months);

Use while when the number of repetitions depends on something discovered during the loop, such as reading until the input runs out.

The do-while loop

Scanner in = new Scanner(System.in);
int choice;

do {
    System.out.print("Choose 1 to 3: ");
    choice = Integer.parseInt(in.nextLine().trim());
} while (choice < 1 || choice > 3);

The condition is tested after the body, so the body always runs at least once. Menus and prompt loops are the natural fit. Note the required semicolon after the condition.

Comparing the loops

LoopCondition testedMinimum passesUse when
forBefore the body0The count is known, or the index is needed
enhanced forBefore the body0Reading every element in order
whileBefore the body0Repeating until something becomes true
do-whileAfter the body1The body must run at least once

break and continue

for (int n = 1; n <= 10; n++) {
    if (n % 2 == 0) {
        continue;      // skip the rest of this pass
    }
    if (n > 7) {
        break;         // leave the loop entirely
    }
    System.out.print(n + " ");   // 1 3 5 7
}

Labelled break

int[][] grid = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int target = 5;

search:
for (int row = 0; row < grid.length; row++) {
    for (int col = 0; col < grid[row].length; col++) {
        if (grid[row][col] == target) {
            System.out.println("Found at " + row + "," + col);
            break search;      // exits both loops
        }
    }
}

An unlabelled break leaves only the innermost loop. A label is the clean way out of nested loops, and it is far better than a flag variable.

return inside a loop

return leaves the whole method immediately, loops included. Extracting a search into its own method and returning the result directly is usually clearer than break plus a result variable.

Common mistakes

  • Using <= with array.length and running past the last index.
  • Forgetting to change the condition variable, producing an infinite loop.
  • Placing a semicolon straight after for (...), which makes an empty body.
  • Removing from a collection inside an enhanced for loop, which throws ConcurrentModificationException. Use an explicit Iterator or removeIf.
  • Declaring the loop counter outside the loop when it is not needed afterwards.

Best practices

  • Prefer the enhanced for loop unless the index is genuinely required.
  • Keep loop bodies short; extract the work into a method with a name.
  • Avoid changing the loop counter inside the body.
  • For transforming or filtering a collection, consider the Stream API instead, which states the intent rather than the mechanics.

Practice

  1. Print the multiplication table of 7 using each of the three loop forms and compare readability.
  2. What does for (int i = 0; i < 5; i++); followed by a block actually do?
  3. Write a loop that finds the first number above 1000 divisible by both 13 and 17.
  4. Explain why removing an element inside an enhanced for loop fails, and show two correct alternatives.
  5. Rewrite a labelled break search as a separate method that returns the position.

Conclusion

Pick the loop that matches what you know in advance, prefer the enhanced form when you only need the elements, and reach for a label rather than a flag when leaving nested loops.

Topics #Beginner #Java
Written by Lorens Mishra

Default administrator account created by the installer.

Continue reading

All Java notes →
Java

Introduction to Java

Java is a statically typed, object oriented language that compiles to bytecode and runs on a virtual machine, which is what makes it portable.

Read more

Discussion

0 comments
Sign in to join the discussion.

No comments yet. Be the first to say something.