Loops in java — A Comprehensive Guide to For and While Loops in Java

Arash Rostami
3 min readAug 12, 2023

--

Photo by Tine Ivanič on Unsplash

In the realm of programming, loops play a vital role in executing repetitive tasks with efficiency and precision. Java, a versatile programming language, offers two fundamental types of loops: the ‘for’ loop and the ‘while’ loop. In this guide, we will dive into the mechanics and usage of these loops, providing you with a solid foundation to wield them effectively in your coding endeavors.

1. The ‘for’ Loop: Unleashing Repetition with Precision :

Imagine you’re baking a batch of cookies. You have a recipe that needs to be followed for each cookie. The ‘for’ loop is your recipe in the coding kitchen, helping you perform a set of instructions a fixed number of times.

for (int i = 0; i < 5; i++) {
System.out.println("Mixing ingredients for cookie #" + (i + 1));
}

For loop follows (variable; condition; task) structure. so the above example will start from i = 0 so the first print line will be :

Mixing ingredients for cookie #0

and it will add 1 every time until it is less than 5.

2. The ‘while’ Loop: Flexibility in Action:

Let’s say you’re tracking the number of steps you take each day to reach a fitness goal. You’re not sure how many days it will take to reach the goal, but you know you’ll keep walking until you do. The ‘while’ loop is your walking buddy, accompanying you until a certain condition is met.

int steps = 0;
int goal = 10000;
while (steps < goal) {
steps += 1500; // Walking an extra 1500 steps
System.out.println("Steps taken: " + steps);
}

In this scenario, the loop keeps counting steps until you achieve your goal of 10000 steps, providing the flexibility needed for dynamic tasks.

3. The ‘do-while’ Loop: Ensuring Action at Least Once:

Consider a scenario where you’re trying to choose the right-sized shoe. You’ll keep trying different sizes until one fits perfectly. The ‘do-while’ loop ensures that you try at least once, even if the first attempt doesn’t yield the desired result.

int shoeSize = 30;
do {
System.out.println("Trying shoe size: " + shoeSize);
shoeSize++;
} while (shoeSize <= 40);

Here, the loop will try sizes from 30 to 40, providing assurance that at least one size will be tested before moving on.

4. The ‘for-each’ Loop: Simplifying Collection Iteration:

Now, picture yourself in an art gallery with a collection of paintings. You want to appreciate each painting individually. The ‘for-each’ loop, introduced in Java 5, simplifies iterating over collections and arrays.

String[] paintings = {"Mona Lisa", "Starry Night", "The Persistence of Memory"};
for (String painting : paintings) {
System.out.println("Admiring: " + painting);
}

In this case, the loop will gracefully go through each painting in the array, allowing you to admire them one by one :)

Conclusion:

The ‘for’, ‘while’, and ‘for-each’ loops in Java are versatile tools in your coding toolkit, ready to tackle various challenges. The ‘for’ loop offers precision for tasks with a predetermined number of repetitions, much like following a recipe. The ‘while’ loop offers flexibility, just like walking towards a fitness goal, adapting to changing conditions. The ‘do-while’ loop ensures that action is taken at least once, providing reassurance. Lastly, the ‘for-each’ loop simplifies the iteration process over collections, making it easier to work with arrays and collections.

until next time. take care :)

--

--

Arash Rostami

Helping developers to become best version of themselves.