Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

How to Use the Do-While Loop in TypeScript

A do...while loop runs its body once before checking the condition. Here is the syntax, examples, and when to prefer while.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A do...while loop in TypeScript runs its body first and checks the condition afterward, so the body always executes at least once. Use it when one pass is mandatory and the condition only decides whether to repeat. The syntax is the same as in JavaScript, as documented in MDN’s do…while reference.

Syntax

do {
  // statements to run at least once
} while (condition);

The closing semicolon is part of the documented syntax. Automatic semicolon insertion may sometimes supply it, but write it explicitly for clarity.

How execution proceeds

  1. Enter the body and run its statements.
  2. Evaluate the condition after the body finishes.
  3. If the condition is true, run the body again. If it is false, continue with the code after the loop.

Because the check comes after the first pass, an initially false condition does not prevent that first pass.

Example: retry until input is valid

let attempts = 0;
let isValid = false;

do {
  attempts += 1;
  isValid = checkInput();
} while (!isValid && attempts < 3);

checkInput() is always called once. The loop repeats only while the input is invalid and fewer than three attempts have been made. Both isValid and attempts are updated inside the body, so the condition can become false.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Exiting early or skipping ahead

Per MDN, break ends the loop immediately and moves control to the code after it. continue skips the rest of the current pass and goes to the condition check; if the condition is true, another iteration follows.

let value = 0;

do {
  value += 1;
  if (shouldStop(value)) {
    break;
  }
  if (shouldSkip(value)) {
    continue;
  }
  process(value);
} while (value < 10);

Note that continue does not bypass the condition. That is why value += 1 sits at the top: the counter changes even on skipped passes.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

do…while versus while

Aspect do…while while
Condition checked After the body Before the body
Minimum body runs One Zero

Choose do...while when the first operation is required, such as reading input before you can judge it. Choose while when zero runs is acceptable. As a rule of thumb (a style preference, not a TypeScript restriction), use for for plain counted loops and for...of to visit items when you don’t need an index.

Common mistakes

  • Expecting a false condition to skip the body. It will not; the body runs once regardless.
  • Leaving out the semicolon. Write } while (condition);.
  • No path to termination. If nothing in the body can make the condition false and there is no break, the loop runs forever. Confirm that the body updates the state the condition reads, or add a deliberate exit.
  • Using it where zero iterations should be possible. Use while instead.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.