Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Loops in Java: For, While, and Do-While

Coding Pirate

Ahoy, matey! Let’s embark on a thrilling adventure to explore the treasure trove of loops in the world of Java programming. Like a pirate navigating the treacherous seas, you’ll use loops to conquer repetitive tasks with ease. So hoist the Jolly Roger and set sail on this journey through the three main types of loops in Java: for, while, and do-while.

The For Loop: A Pirate’s Steady Crew

Picture a for loop as the loyal crew aboard a pirate ship, each member assigned a specific task for a set number of iterations. For loops are perfect for situations where you know the exact number of times a task must be performed.

Here’s the basic syntax for a for loop:

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

Imagine you’re the captain of a pirate ship, and you have a treasure chest with 10 gold coins. You want to distribute the coins equally among your four crew members. A for loop can help you accomplish this task:

int goldCoins = 10;
int crewMembers = 4;

for (int i = 0; i < crewMembers; i++) {
    int coinsPerCrewMember = goldCoins / crewMembers;
    System.out.println("Crew member " + (i + 1) + " gets " + coinsPerCrewMember + " gold coins.");
}

In this example, the for loop iterates four times, ensuring that each crew member receives their fair share of the loot.

The While Loop: A Pirate’s Trusty Compass

A while loop is like a pirate’s trusty compass, guiding them through unknown waters as long as a specific condition is met. While loops are useful when you don’t know how many times a task should be executed, but you have a condition that must be satisfied.

Here’s the basic syntax for a while loop:

while (condition) {
    // code to be executed
}

Suppose you’re a pirate searching for buried treasure on a deserted island. You’ve been given a riddle: “Continue digging until ye find a chest that contains more than 10 doubloons.” A while loop will help you solve this riddle:

int doubloons = 0;

while (doubloons <= 10) {
    System.out.println("Digging for treasure...");
    doubloons = findTreasure();
}

System.out.println("Arr, I found a chest with " + doubloons + " doubloons!");

In this example, the while loop continues to dig for treasure until the condition (finding more than 10 doubloons) is met.

The Do-While Loop: A Pirate’s Persistent Parrot

A do-while loop resembles a persistent parrot perched on a pirate’s shoulder, always squawking at least once before checking if it should continue. Do-while loops are similar to while loops, but they execute the code block at least once before evaluating the condition.

Here’s the basic syntax for a do-while loop:

do {
    // code to be executed
} while (condition);

Imagine you’re a pirate captain with a picky parrot who only eats a specific type of cracker. You’re unsure how many crackers it’ll take to find the right one, but you know the parrot will try at least one. A do-while loop is perfect for this situation:

int crackersTried = 0;
boolean parrotIsSatisfied = false;

do {
    System.out.println("Offering cracker #" + (crackersTried + 1));
    crackersTried++;
    parrotIsSatisfied = checkParrotSatisfaction(crackersTried);
} while (!parrotIsSatisfied);

System.out.println("Arr, the parrot is finally satisfied after trying " + crackersTried + " crackers!");

In this example, the do-while loop ensures the parrot tries at least one cracker, and continues offering crackers until it finds the one the parrot likes.

In Conclusion

From the steady for loop to the trusty while loop, and the persistent do-while loop, Java offers a variety of loop structures to help you navigate the high seas of programming. Like a skilled pirate, practice using these loops to unlock the hidden treasures of Java and become a true master of the code.

Now that you’re familiar with loops in Java, it’s time to hoist the Jolly Roger and set sail for more programming adventures! Remember, the more you practice, the better you’ll become at conquering repetitive tasks and taming the wild seas of code. Happy sailing, matey!