Imagine you want to print "Hello" 100 times. You could write System.out.println("Hello"); one hundred times, but that would be a terrible way to write a program. 😄
Instead, we can tell Java to repeat a piece of code for us. This is where loops come in.
A loop allows us to execute the same block of code repeatedly as long as a particular condition is satisfied.
For example, instead of writing:
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
we can use a loop to do the same thing much more efficiently.
Why Do We Need Loops?
Loops are useful whenever we need to perform the same task multiple times.
For example, a program might need to:
Print numbers from 1 to 100
Process every item in a list
Ask the user for input repeatedly
Repeat a game action until the player quits
Perform an operation for every element in an array
Instead of writing the same code again and again, we write it once inside a loop.
Types of Loops in Java
Java provides three main types of loops:
forloopwhileloopdo-whileloop
There is also an enhanced for loop, commonly called the for-each loop, which is especially useful when working with arrays and collections.
Let's understand the basic loops first.
The for Loop
The for loop is commonly used when we know how many times we want to repeat something.
For example, suppose we want to print numbers from 1 to 5:
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
Output:
1
2
3
4
5
Let's understand what is happening here.
for (int i = 1; i <= 5; i++)
There are three important parts:
int i = 1→ starting valuei <= 5→ conditioni++→ increaseiafter each iteration
The loop continues as long as i <= 5 is true.
So the loop runs with i equal to 1, 2, 3, 4, and 5.
The while Loop
A while loop repeats code as long as a condition is true.
For example:
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
The output is:
1
2
3
4
5
Here, Java first checks whether i <= 5 is true. If it is, the code inside the loop runs.
After printing the number, i++ increases its value. This continues until the condition becomes false.
A while loop is particularly useful when we don't necessarily know in advance how many times the loop needs to run.
The do-while Loop
The do-while loop is similar to the while loop, but there is one important difference.
A do-while loop always executes at least once because the condition is checked after the code runs.
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
Output:
1
2
3
4
5
Now consider this example:
int i = 10;
do {
System.out.println(i);
} while (i < 5);
Even though i < 5 is false, the program still prints:
10
Why? Because the code inside do runs before Java checks the condition.
That's the key difference:
while → checks the condition first
do-while → runs once, then checks the condition
What is an Iteration?
Each time the code inside a loop runs is called an iteration.
For example:
for (int i = 1; i <= 3; i++) {
System.out.println(i);
}
This loop has three iterations:
Iteration 1 → i = 1
Iteration 2 → i = 2
Iteration 3 → i = 3
You will hear the word "iteration" frequently when working with loops, so it's good to get familiar with it.
An Infinite Loop
We need to be careful when writing loops because a loop can accidentally run forever.
For example:
int i = 1;
while (i <= 5) {
System.out.println(i);
}
There is a problem here. We never increase i, so its value remains 1. The condition i <= 5 will always be true, and the loop will never stop.
We can fix it by updating i:
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
Now the value eventually becomes 6, the condition becomes false, and the loop stops.
A Simple Real-World Example
Imagine a teacher wants to print the names of five students. Instead of writing the same code repeatedly, we could use a loop when working with a collection of student names.
For now, let's use a simple counter:
for (int student = 1; student <= 5; student++) {
System.out.println("Student " + student);
}
Output:
Student 1
Student 2
Student 3
Student 4
Student 5
The loop handles the repetition for us.
Which Loop Should You Use?
A simple way to choose a loop is:
Use for when you generally know how many times you want to repeat something.
Use while when the number of repetitions depends mainly on a condition.
Use do-while when the code should execute at least once before checking the condition.
For example, if you want to print numbers from 1 to 100, a for loop is a natural choice. If you want to keep asking a user for input until they enter a valid value, a while loop may be more appropriate.
Loops are one of the most important concepts in programming. Once you understand them, you can write programs that handle repetitive tasks without writing the same code over and over again.