ProgramGuru

do...while Loop

Learn do...while Loop

A do...while loop executes its body once before testing whether it should repeat.

In this lesson, you will study the rule, run a complete TypeScript example, explain its output, and test a small change.

What you’ll learn

  • Explain the main rule behind do...while loop.
  • Predict the result of the complete runnable TypeScript example.
  • Recognize one common mistake and correct it.

Understand the concept

Because the condition is evaluated after the body, a do...while loop always runs at least one iteration if reached. It is useful for an operation that must happen once, followed by a repeat decision.

See the concept visually

Use the diagram to locate the checks, values or branches involved in the example.

do...while Loop: illustrated flowExecute body first; Increment attempts; Check condition; Repeat if truerepeatExecute body firstIncrement attemptsCheck conditionRepeat if trueThe condition determines whether the next iteration runs.
do...while Loop: illustrated flow. Execute body first; Increment attempts; Check condition; Repeat if true.

Run a complete TypeScript example

Save this code as main.ts or paste it into the TypeScript Playground. The example uses fixed values so it runs without prompting for input.

TypeScript code

let attempts: number = 0;
 do {
   attempts++;
   console.log(`Attempt ${attempts}`);
 } while (attempts < 2);

Verified output

Attempt 1
Attempt 2

How to run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. When using a project configuration, run the matching project build instead.

Trace the execution

The first iteration runs with attempts 0 and prints Attempt 1. The condition is true, so a second iteration prints Attempt 2. The next condition is false.

Predict the output before you run the sample. Compare each line with the source expression that produced it, and distinguish the type checker’s work from what happens in JavaScript at runtime.

Common mistakes and how to avoid them

Remember the trailing semicolon after the while (...) condition. A do...while loop is not interchangeable with while when the initial condition is false.

Debugging habit: when a change is rejected, read the exact compiler diagnostic and inspect the operand types. When it compiles but behaves unexpectedly, follow the values through the JavaScript expressions.

Practice it yourself

Change attempts to 5 before the loop. Explain why one message still prints.

Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.

Check your understanding

What guarantees at least one execution of the body?
Choose one answer. You can try again.

Answer and explanation

do...while

A do...while tests only after the body has already executed.

Review the key idea

do...while Loop: Because the condition is evaluated after the body, a do.

A do...while tests only after the body has already executed.

Before continuing, be able to explain what is checked before execution and what actually changes at runtime in the example above.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: for...of Loop →