Java do-while loop


The do-while loop in Java is a control structure that allows you to execute a block of code repeatedly, with the key difference being that it guarantees at least one execution of the loop body, regardless of whether the condition is true or false. This is because the condition is checked after the execution of the loop body.

Syntax of the do-while Loop

do { // Block of code to be executed } while (condition);
  • Block of Code: The statements within the do block are executed once before the condition is checked.
  • Condition: After executing the loop body, the condition is evaluated. If it evaluates to true, the loop body will execute again. If it evaluates to false, the loop terminates.

Example of a do-while Loop

Here’s a simple example demonstrating how to use a do-while loop to print the numbers from 0 to 4:

public class DoWhileLoopExample { public static void main(String[] args) { int i = 0; // Initialization // Using a do-while loop to iterate from 0 to 4 do { System.out.println("Iteration: " + i); // Loop body i++; // Update } while (i < 5); // Condition } }

Explanation

  1. Initialization: int i = 0; initializes the loop control variable i to 0.
  2. Loop Body: System.out.println("Iteration: " + i); prints the current value of i.
  3. Update: i++; increments the value of i by 1 at the end of each iteration.
  4. Condition: After executing the loop body, while (i < 5); checks if i is less than 5. If true, the loop body will execute again.

Iteration Breakdown

  • 1st Iteration: The loop body executes, printing Iteration: 0, then increments i to 1.
  • 2nd Iteration: The loop body executes, printing Iteration: 1, then increments i to 2.
  • 3rd Iteration: The loop body executes, printing Iteration: 2, then increments i to 3.
  • 4th Iteration: The loop body executes, printing Iteration: 3, then increments i to 4.
  • 5th Iteration: The loop body executes, printing Iteration: 4, then increments i to 5.
  • 6th Iteration: The condition 5 < 5 is false, so the loop terminates.

Output

The output of the above program will be:

Iteration: 0 Iteration: 1 Iteration: 2 Iteration: 3 Iteration: 4

Important Points

  • Guaranteed Execution: The do-while loop is particularly useful when you need to ensure that the loop body runs at least once, such as in cases where user input is involved, and you want to prompt the user at least once.

  • Infinite Loop: Similar to other loops, if the condition never becomes false, the loop can become infinite. For example, if you had a condition that is always true, the loop would continue indefinitely.

Example of an Infinite Loop (with correction)

To illustrate the infinite loop and how to correct it:

public class InfiniteDoWhileExample { public static void main(String[] args) { int i = 0; // Infinite loop example (uncomment to see the effect) do { System.out.println("Iteration: " + i); // i++; // Uncommenting this line will prevent infinite loop } while (i < 5); } }

If you run this code without the increment statement (i++), it will produce an infinite loop, continuously printing Iteration: 0.

Summary

The do-while loop is a unique control structure in Java that allows for repeated execution of a code block with the guarantee of at least one execution. Understanding how to use do-while loops effectively is essential for writing robust Java programs, particularly in scenarios where you want to ensure that a certain block of code is executed before checking a condition.