Skip to content

Loops – Doing Things Over and Over Again

Seanti edited this page Mar 24, 2025 · 1 revision

Imagine you have a bag of gumballs, and you want to chew one at a time until the bag is empty.

Instead of writing:

  1. Take one gumball.
  2. Take one gumball.
  3. Take one gumball.
    (…and repeating forever)

We can use loops to make this easier!


What is a Loop?

A loop is like a routine—it repeats the same action until a condition is met.

There are three main types of loops in Java:

Loop Type Think of it like... When to Use It
for loop Counting gumballs one by one When you know how many times to repeat something.
while loop Chewing gumballs until the bag is empty When you don’t know how many times yet but you have a condition.
do-while loop Taking at least one gumball before checking When you always want to do something at least once before checking the condition.

1. for Loop – When You Know How Many Times

A for loop is used when you already know how many times you want to repeat something.

Example: Taking 5 Gumballs One by One

public class GumballMachine {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            System.out.println("Taking gumball #" + i);
        }
    }
}

How It Works:

  1. int i = 1; → Start with gumball #1.
  2. i <= 5; → Keep going while we have 5 or fewer gumballs.
  3. i++ → Increase the count by 1 each time.

Output:

Taking gumball #1  
Taking gumball #2  
Taking gumball #3  
Taking gumball #4  
Taking gumball #5  

Use a for loop when you already know how many times to repeat.


2. while Loop – When You Don’t Know How Many Times

A while loop is used when you don’t know in advance how many times something should happen, but you have a condition.

Example: Chewing Gumballs Until the Bag is Empty

public class GumballChewer {
    public static void main(String[] args) {
        int gumballs = 5;
        
        while (gumballs > 0) {
            System.out.println("Chewing a gumball... " + gumballs + " left.");
            gumballs--; // Reduce the number of gumballs
        }
        
        System.out.println("No more gumballs left!");
    }
}

How It Works:

  1. We start with 5 gumballs.
  2. As long as gumballs > 0, we keep chewing.
  3. Each time, gumballs-- decreases the count.

Output:

Chewing a gumball... 5 left.  
Chewing a gumball... 4 left.  
Chewing a gumball... 3 left.  
Chewing a gumball... 2 left.  
Chewing a gumball... 1 left.  
No more gumballs left!  

Use a while loop when you don’t know in advance how many times to repeat.


3. do-while Loop – When You Always Do Something At Least Once

A do-while loop works just like a while loop, but it always runs at least once, no matter what.

Example: Taking One Gumball Before Checking If There’s More

public class GumballTester {
    public static void main(String[] args) {
        int gumballs = 0; // Oops! No gumballs left.

        do {
            System.out.println("Taking a gumball... But wait, do we even have any?");
        } while (gumballs > 0);
        
        System.out.println("No gumballs left!");
    }
}

Output:

Taking a gumball... But wait, do we even have any?  
No gumballs left!  

Use a do-while loop when you always want to run the code at least once.


Final Takeaway: When to Use Each Loop

Loop Type Use When... Example
for loop You know how many times to loop Counting gumballs
while loop You don’t know how many times, but have a condition Chewing gumballs until they’re gone
do-while loop You always want the action to happen at least once Taking a gumball before checking

That’s the basics of loops! You’ll see them everywhere in Java, and they’re super useful for repeating tasks.

Clone this wiki locally