Loops in Java: for, while, and do-while


In Java, loops are used to execute a block of code repeatedly based on a condition. There are three main types of loops: for loop, while loop, and do-while loop. Let’s explore each loop type with examples.

1. The for Loop

The for loop is typically used when you know in advance how many times you want the loop to run.

Syntax:

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

The loop consists of three parts:

  • Initialization: This is where you set the starting point (typically a counter variable).
  • Condition: The loop continues as long as this condition is true.
  • Update: This updates the counter variable (or condition) after each iteration.

Example 1: Basic for Loop

Let's print numbers from 1 to 5 using a for loop:

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

Output:

    1
    2
    3
    4
    5
        

In this example, the loop starts with i = 1, checks if i <= 5, and prints the value of i. After each iteration, i is incremented by 1, until the condition becomes false.

2. The while Loop

The while loop is used when you want the loop to continue as long as a specific condition is true. It checks the condition before executing the loop body.

Syntax:

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

The loop will continue executing as long as the condition evaluates to true. If the condition is false at the beginning, the loop will not run at all.

Example 2: Basic while Loop

Let's print numbers from 1 to 5 using a while loop:

    public class WhileLoopExample {
        public static void main(String[] args) {
            int i = 1;
            while (i <= 5) {
                System.out.println(i);
                i++; // Increment the counter
            }
        }
    }
        

Output:

    1
    2
    3
    4
    5
        

In this example, the while loop runs as long as the condition i <= 5 is true. After printing the value of i, we increment i by 1.

3. The do-while Loop

The do-while loop is similar to the while loop, but it checks the condition after executing the loop body. This ensures that the loop runs at least once.

Syntax:

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

In a do-while loop, the code inside the loop executes first, and then the condition is checked. If the condition is true, the loop continues; otherwise, it exits.

Example 3: Basic do-while Loop

Let's print numbers from 1 to 5 using a do-while loop:

    public class DoWhileLoopExample {
        public static void main(String[] args) {
            int i = 1;
            do {
                System.out.println(i);
                i++; // Increment the counter
            } while (i <= 5);
        }
    }
        

Output:

    1
    2
    3
    4
    5
        

In this example, the loop executes at least once, even if the condition is initially false. After printing the value of i, i is incremented by 1, and the condition i <= 5 is checked.

Comparing the Three Loops

The three loops differ in how they control the flow:

  • for loop: Best when you know the number of iterations in advance.
  • while loop: Best when you don't know the number of iterations but have a condition to check before each iteration.
  • do-while loop: Best when you need to ensure that the loop body runs at least once, regardless of the condition.

Conclusion

Loops are fundamental in programming as they allow repetitive execution of code. In Java, the for, while, and do-while loops are used to iterate over code blocks based on conditions. Choose the type of loop based on your specific needs for iteration.





Advertisement