-
Notifications
You must be signed in to change notification settings - Fork 0
Loops – Doing Things Over and Over Again
Imagine you have a bag of gumballs, and you want to chew one at a time until the bag is empty.
Instead of writing:
- Take one gumball.
- Take one gumball.
- Take one gumball.
(…and repeating forever)
We can use loops to make this easier!
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. |
A for loop is used when you already know how many times you want to repeat something.
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:
-
int i = 1;→ Start with gumball #1. -
i <= 5;→ Keep going while we have 5 or fewer gumballs. -
i++→ Increase the count by 1 each time.
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.
A while loop is used when you don’t know in advance how many times something should happen, but you have a condition.
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:
- We start with
5gumballs. -
As long as
gumballs > 0, we keep chewing. - Each time,
gumballs--decreases the count.
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.
A do-while loop works just like a while loop, but it always runs at least once, no matter what.
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!");
}
}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.
| 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.