PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

Java Do-While Loop

← All stacks

Theory

21/270

Java Do-While Loop

do-while runs the body first, then checks. So it always runs at least once. Shape: do { body } while (condition); — the semicolon at the end is required.

Order: body → check → maybe body again. Use it when you must do something once before you can decide: show a menu, ask for input once.

while can skip the body completely. do-while cannot. Freshers forget the semicolon after while (condition) and get a compile error.

Let's take this on the board with one tiny Main class — no extra files. Java Do-While Loop — output: 1 then 2 then 3. do-while runs the body first, then checks. Even if i started at 9, one print would still happen. Start from main, go line by line, and stop at each print. That output is the proof for Do-While Loop.

Diagram
body first
         │
         ▼
   check ── true → body again
         │ false
         ▼
        stop
Exam tip

One line: do-while guarantees one execution. Show a menu loop.

Example

// Java Do-While — prints 1, 2, 3
public class Main {
  public static void main(String[] args) {
    int i = 1;
    do {
      System.out.println(i);
      i++;
    } while (i <= 3);
  }
}

Java Do-While Loop — output: 1 then 2 then 3. do-while runs the body first, then checks. Even if i started at 9, one print would still happen.

Short notes

  • Defdo-while runs the body first, then checks. Runs at least once.
  • Ruledo { ... } while (condition); — semicolon at the end.
  • UseMenus and “ask at least once” loops.
  • Diffwhile can run 0 times. do-while ≥ 1.

Questions

1

How many times does do-while run at minimum?

2

What is the order?

3

What do people forget?

Previous← Java While LoopNextJava Break →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.