About Us Contact Us Write for Us Advertise
Home > Java > Java Loops Explained: for, while and do-while (with Examples)
Java

Java Loops Explained: for, while and do-while (with Examples)

Learn Java loops the easy way — for, while, and do-while with clear examples, break and continue, the for-each loop, and how to avoid infinite loops.

Shiv Pandey
Shiv Pandey
Sep 19, 2026 | 67 views
Java Loops Explained: for, while and do-while (with Examples)

Imagine you need to print "Hello" 100 times. You wouldn't write 100 lines — you'd tell the computer to repeat one line 100 times. That's exactly what loops do: they run a block of code again and again until a condition says stop. Loops are one of the most powerful ideas in programming, and Java gives you three flavours: for, while, and do-while.

The for loop

Use a for loop when you know how many times you want to repeat. It packs three parts into one tidy line:

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

Those three parts, separated by semicolons, are:

  1. int i = 1 — the start: create a counter and set its starting value (runs once, at the beginning).
  2. i <= 5 — the condition: keep looping while this is true (checked before each round).
  3. i++ — the update: change the counter after each round (here, add 1).

So this prints Count: 1, 2, 3, 4, 5 and then stops (because when i becomes 6, 6 <= 5 is false). Trace it slowly once by hand — the moment this "click" happens, loops make sense forever.

The while loop

Use a while loop when you don't know the exact count — you just want to keep going as long as a condition holds:

int n = 1;

while (n <= 5) {
    System.out.println("Number: " + n);
    n++;   // don't forget to update, or it loops forever!
}

The while checks the condition first, then runs the body. Notice you have to update n yourself inside the loop.

Beware the infinite loop! If the condition never becomes false — usually because you forgot to update the counter — the loop runs forever and your program freezes. If a program ever "hangs," a runaway loop is the first suspect. Press Ctrl+C in the terminal to stop it.

The do-while loop

A do-while is like while, but it checks the condition at the end. That means the body always runs at least once, even if the condition is false from the start:

int i = 10;

do {
    System.out.println("This runs once, guaranteed.");
    i++;
} while (i < 5);   // false immediately, but body already ran once

It's perfect for things like menus — "show the menu, then ask again if they didn't choose quit."

break and continue

Two keywords give you extra control inside any loop:

  • break — exit the loop immediately, right now.
  • continue — skip the rest of this round and jump to the next iteration.
for (int i = 1; i <= 10; i++) {
    if (i == 6) break;        // stop completely at 6
    if (i % 2 != 0) continue;  // skip odd numbers
    System.out.println(i);          // prints 2, 4
}

The enhanced for loop (for-each)

There's also a simpler for for going through every item in a collection or array — you'll use this constantly once you learn arrays in the next lesson:

int[] scores = {85, 90, 78};

for (int score : scores) {   // "for each score in scores"
    System.out.println(score);
}

Which loop should I use?

  • for — you know the number of repetitions (counting 1 to 100, going through a list).
  • while — you loop until some condition changes and don't know the count in advance.
  • do-while — same as while, but the body must run at least once (menus, prompts).

My most common beginner mistake with loops? Forgetting the i++ in a while loop and freezing the whole program. If yours ever hangs, check that the counter is actually changing — nine times out of ten, that's the culprit.

Key takeaways

  • Loops repeat a block of code so you don't have to write it out many times.
  • for = known count (start; condition; update). while = loop while a condition holds. do-while = runs at least once.
  • Always make sure the loop can end — an unchanging condition means an infinite loop.
  • break exits a loop; continue skips to the next round.

← Previous: Lesson 7 — Decisions: if, else & switch
Next: Lesson 9 — Methods (Functions) in Java →

Related Articles

Operators in Java Explained (with Examples)
Java

Operators in Java Explained (with Examples)

== vs .equals() in Java: The Difference Explained (Beginner Guide)
Java

== vs .equals() in Java: The Difference Explained (Beginner Guide)

DSA in Java: Graphs — BFS & DFS Explained (Beginner Guide)
Java

DSA in Java: Graphs — BFS & DFS Explained (Beginner Guide)

DSA in Java: Stacks & Queues Explained (Beginner Guide)
Java

DSA in Java: Stacks & Queues Explained (Beginner Guide)