do-while Loop
The do-while loop is a control flow statement in Java that executes a block of code at least once, and then repeatedly executes it as long as the condition remains true.
Key Points:
- Executes at least once (even if the condition is false initially).
- Condition is checked after executing the loop body.
- Useful when you want the loop body to run at least one time.
- The loop body runs first, then condition is checked. So even if the condition is false at the beginning, the loop executes one time.
Syntax:
do {
// code to run
} while (condition);
- do { } block contains the code that will run first, at least once.
- while (condition); checks the condition after the code runs.
- If the condition is true, the loop repeats, otherwise it stops.
Example:
class ShikshaSanchar {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Hello from Shiksha Sanchar " + i);
i++;
} while (i <= 5);
}
}
Output:
Hello from Shiksha Sanchar 1
Hello from Shiksha Sanchar 2
Hello from Shiksha Sanchar 3
Hello from Shiksha Sanchar 4
Hello from Shiksha Sanchar 5
Explanation:
- Initialization:
variable i is initialized to 1.int i = 1; - do Block Execution: The code inside the do block runs before checking the condition.
- Printing & Incrementing:
- First, it prints: "Hello from Shiksha Sanchar 1".
- Then i++ increases i to 2.
- Condition Check:
— As long as this is true, it keeps repeating the loop.while (i <= 5) - Loop Runs 5 Times: Runs with i = 1 to i = 5. When i becomes 6, the condition i <= 5 becomes false, so the loop stops.
Flowchart : do-while Loop in Java
This diagram visually shows how a do-while loop works — where the loop body is executed at least once, and condition is checked afterward.
- Start
- The program execution begins here.
- Control moves to the initialization step.
- Initialization: i = 1
- Variable i is initialized with value 1.
- This step happens only once, before the loop starts.
- Loop Body: Print Statement
- Statement inside the loop:
System.out.println("Hello from Shiksha Sanchar " + i); - This line prints the current value of i with the message.
- This part is always executed at least once, even if the condition is false.
- Statement inside the loop:
- Update Step: i = i + 1
- After printing, the value of i is incremented by 1.
- Prepares i for the next iteration.
- Condition Check: Is i <= 5?
- After updating i, the loop checks if the condition is still true.
- If YES: The loop goes back to step 3 (print).
- If NO: The loop terminates and control moves to the end.
- End
- The condition is false.
- The loop exits, and the program ends.
Use Case:
When you want to ensure at least one execution, such as displaying a menu to the user and asking for input, regardless of the condition.
Summary:
- Executes loop body at least once, even if condition is false.
- Condition is checked after the loop body runs.
- Useful when you want guaranteed one-time execution.
- Syntax ends with a semicolon after while(condition);
- Best when you want to take user input or display a menu at least one time.
- Loop terminates when condition becomes false.