GoBasics

Java Loops Explained: With Syntax, Examples & Outputs

When you're writing code in Java, there are times when you want to repeat something. Maybe you want to print numbers from 1 to 10. Or maybe you want to go through each item in an array. You might even want to keep running a block of code until a certain condition is no longer true.

Instead of writing the same code again and again, Java gives us something better. It gives us loops. Loops let us run a piece of code multiple times without repeating ourselves. They're simple, useful, and make your programs a lot more efficient.

Types of Loops in Java

Java has three main types of loops:

Let’s go through each one step by step.

for Loop

The for loop is used when we know in advance how many times we want to run a piece of code.

Syntax:

for (initialization; condition; update) {
    // code to run
}

Initialization runs once at the start, usually to set a counter variable.

Condition is checked before each repetition. If it’s true, the loop continues. If false, the loop stops.

Update runs after each repetition, usually to change the counter.

Example:

public class ForLoopExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            System.out.println("Number: " + i);
        }
    }
}
Output: Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

What’s happening?

while Loop

The while loop is used when we don’t know how many times we need to repeat something. The loop keeps running as long as the condition is true.

Syntax:

while (condition) {
    // code to run
}

The condition is checked before each repetition. If the condition is false right from the start, the code inside the loop won’t run at all.

Example:

public class WhileLoopExample {
    public static void main(String[] args) {
        int i = 1;
        while (i <= 5) {
            System.out.println("Count: " + i);
            i++;
        }
    }
}
Output: Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

What’s happening?

do-while Loop

This loop is similar to the while loop, but there's one big difference: the code runs at least once, even if the condition is false.

Syntax:

do {
    // code to run
} while (condition);

Here, the code inside the do block runs first, then the condition is checked. If the condition is true, the loop repeats. If not, it stops.

Example:

public class DoWhileExample {
    public static void main(String[] args) {
        int i = 1;
        do {
            System.out.println("Value: " + i);
            i++;
        } while (i <= 5);
    }
}
Output: Value: 1
Value: 2
Value: 3
Value: 4
Value: 5

What’s the difference?

Common Errors in Java Loops

Common Mistakes in for loop

Forgetting to update the loop variable

Every loop needs to move forward — otherwise, it just keeps running forever. If you forget to update the loop variable, like missing i++, your condition might never become false. And when that happens, your loop goes on and on. This is called an infinite loop, and it can freeze your program. So always check that your loop variable is changing somewhere in your code.

Error Code:

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

Fixed Code:

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

Off-by-one errors

This is one of those mistakes that almost every beginner makes at least once. Sometimes your loop runs one time too many or one time too few. This usually happens because of how you write the condition. For example, i < 5 runs the loop from 1 to 4, but i <= 5 runs from 1 to 5. Just that little = can make a big difference. So it’s always good to double-check your start and end values.

Error Code:

for (int i = 1; i < 5; i++) {
        System.out.println("Number: " + i); // Runs 1 to 4
    }

Fixed Code:

for (int i = 1; i <= 5; i++) {
    System.out.println("Number: " + i); // Runs 1 to 5
}

Common Mistakes in while loop

Not updating the loop variable inside the loop

While loops are a bit different from for loops because they don’t have everything in one line. That means you have to remember to update your loop variable manually — inside the loop body. If you forget to do that, the condition may always stay true, and your loop could run forever. So make sure you're changing the variable that the condition depends on.

Error Code:

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

Fixed Code:

int i = 1;
while (i <= 5) {
    System.out.println("Count: " + i);
    i++; // Proper update
}

Starting with a condition that’s already false

Let’s say you write while(i > 5) but set i = 1. Right from the beginning, the condition is false. So what happens? The loop doesn’t run at all. This might not be a bug, but it could be unexpected — especially if you thought the loop would run at least once. Always look at your starting values and your condition together to make sure they make sense.

Error Code:

int i = 1;
    while (i > 5) { // Condition false at start
        System.out.println("Count: " + i);
        i++;
    }

Fixed Code:

int i = 1;
while (i <= 5) { // Correct condition
    System.out.println("Count: " + i);
    i++;
}

Common Mistakes in do-while loop

Assuming the do-while loop won’t run if the condition is false

This is where the do-while loop behaves a little differently. Even if the condition is false, it still runs the code once. That’s how it's designed. So don’t be surprised if you see some output from a do-while loop, even when you expected it to skip. It’s just doing what it’s supposed to do: run first, then check the condition.

Error Code:

int i = 10;
    do {
        System.out.println("Value: " + i);
        i++;
    } while (i < 5); // Runs once, even though i < 5 is false at start

Fixed Code:

int i = 10;
if (i < 5) { // Optional pre-check
    do {
        System.out.println("Value: " + i);
        i++;
    } while (i < 5);
}

Forgetting the semicolon in a do-while loop

Here's a tiny mistake that can lead to a frustrating error. After writing while (condition) in a do-while loop, you have to end it with a semicolon. Many learners miss this part. But in Java, that semicolon is important. Without it, the compiler won’t understand what you're trying to do, and you’ll get a syntax error.

Error Code:

do {
        System.out.println("Value");
    } while (condition) // Missing semicolon

Fixed Code:

do {
    System.out.println("Value");
} while (condition); // Proper semicolon