do-while
The do-while loop works like this:
Run the body.
Check the condition.
If true, run the body again.
If false, stop.
Because the check comes after the body, you guarantee at least one run โ no matter what.
This is perfect when you need to:
Prompt the user at least once,
Show a menu before deciding whether to repeat,
Run a block that generates output or setup work before validation.
Compared to while, the do-while is less common but exactly right when you must do something once before checking.
Many developers skip it and use while instead โ but then add extra flags or duplicate code to get the first run.
The do-while solves that cleanly.
Mini Lab
A textbook do-while example is a user menu.
When you build console apps, you often show a menu, get a choice, then repeat until the user says "Exit".
You canโt check the condition first โ you need to show the menu at least once.
The do-while is perfect for this.
It keeps your code clean: the menu display, input, and repeat check stay together in one tidy block.
This pattern appears in:
Admin dashboards,
Simple CLI tools,
Small games or quiz apps.
Whenever you hear โshow options โ handle input โ repeat,โ think do-while.
Mini Lab
Another smart way to use do-while is when you want the user to confirm something.
For example, after entering data, you might ask: โDo you want to enter more? Y/Nโ
This works great because the user must always see the first prompt at least once.
This pattern avoids redundant checks and keeps the logic simple.
Mini Lab