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:

  1. Executes at least once (even if the condition is false initially).
  2. Condition is checked after executing the loop body.
  3. Useful when you want the loop body to run at least one time.
  4. 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:

  1. Initialization:
    int i = 1;
    variable i is initialized to 1.
  2. do Block Execution: The code inside the do block runs before checking the condition.
  3. Printing & Incrementing:
    • First, it prints: "Hello from Shiksha Sanchar 1".
    • Then i++ increases i to 2.
  4. Condition Check:
    while (i <= 5)
    — As long as this is true, it keeps repeating the loop.
  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

Java do-while loop Diagram

This diagram visually shows how a do-while loop works — where the loop body is executed at least once, and condition is checked afterward.

  1. Start
    • The program execution begins here.
    • Control moves to the initialization step.
  2. Initialization: i = 1
    • Variable i is initialized with value 1.
    • This step happens only once, before the loop starts.
  3. 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.
  4. Update Step: i = i + 1
    • After printing, the value of i is incremented by 1.
    • Prepares i for the next iteration.
  5. 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.
  6. 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:

  1. Executes loop body at least once, even if condition is false.
  2. Condition is checked after the loop body runs.
  3. Useful when you want guaranteed one-time execution.
  4. Syntax ends with a semicolon after while(condition);
  5. Best when you want to take user input or display a menu at least one time.
  6. Loop terminates when condition becomes false.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar