TypeScript do-while Loop

A TypeScript do...while loop executes a block of statements and then checks whether it should repeat. Because the condition is evaluated after the loop body, the statements always run at least once.

This loop is useful when the first operation must happen before its continuation condition can be tested. Examples include displaying a menu, requesting input, retrying an operation, or processing an initial value before deciding whether to continue.

TypeScript do-while Loop Syntax

Following is the syntax of do-while loop :

The set of statement are enclosed in brackets after do keyword. while keyword then follows with the condition that controls the looping mechanism.

</>
Copy
 do {
     // set of statements 
 } while(condition)

The set of statements are executed and then the condition after while keyword is evaluated. If it is true, the set of statements are executed once again and the condition is checked once again, and the looping continues. The loop breaks only when the condition is evaluated to false.

In conventional TypeScript syntax, a semicolon is written after the closing parenthesis: } while (condition);. The loop body should normally change a value involved in the condition so that the loop can eventually stop.

How a TypeScript do-while Loop Executes

  1. Program control enters the do block without checking a condition.
  2. The statements inside the block execute.
  3. The condition following while is evaluated.
  4. If the condition is true, execution returns to the beginning of the do block.
  5. If the condition is false, execution continues with the statement after the loop.

This post-test execution order is the main difference between do...while and a regular while loop.

TypeScript do-while Loop Example with a Counter

Following is an simple do-while loop example.

example.ts

</>
Copy
var N = 6
var i = 1

do {
    console.log(i)
    i++ // updating control variable
} while(i<N)

Output

1
2
3
4
5

The counter begins at 1. Each iteration prints the current value and increments it. After 5 is printed, i becomes 6, so the condition i < N evaluates to false and the loop ends.

TypeScript do-while Loop Executes at Least Once

Following is an example with condition evaluating to false for the very first evaluation.

example.ts

</>
Copy
var N = 0
var i = 1

do {
    console.log(i)
    i++ // updating control variable
} while(i<N)

Output

1

The loop has executed atleast for once even if the condition is straight away false.

The value 1 is printed before the condition is checked. After the body increments i to 2, the expression 2 < 0 is false, preventing a second iteration.

Input Validation with a TypeScript do-while Loop

A validation routine often needs to examine at least one value before it can decide whether another attempt is necessary. The following example processes values from a simulated input sequence until it finds a number within the permitted range.

</>
Copy
const inputs: number[] = [-4, 0, 7];
let inputIndex: number = 0;
let selectedNumber: number;

do {
    selectedNumber = inputs[inputIndex];
    console.log(`Checking ${selectedNumber}`);
    inputIndex++;
} while (
    (selectedNumber < 1 || selectedNumber > 10) &&
    inputIndex < inputs.length
);

console.log(`Accepted: ${selectedNumber}`);

Output

Checking -4
Checking 0
Checking 7
Accepted: 7

The loop checks each available value and stops when it reaches 7, which is within the range from 1 through 10. The second part of the condition also prevents the example from reading beyond the end of the array.

Using break in a TypeScript do-while Loop

The break statement immediately exits the nearest loop. It can provide an additional stopping rule that is easier to express inside the loop body.

</>
Copy
let attempt: number = 0;

do {
    attempt++;
    console.log(`Attempt ${attempt}`);

    if (attempt === 3) {
        break;
    }
} while (attempt < 10);

Output

Attempt 1
Attempt 2
Attempt 3

Although the loop condition would allow as many as ten attempts, break ends the loop after the third one.

Using continue in a TypeScript do-while Loop

The continue statement skips the remaining statements in the current iteration. In a do...while loop, program control then proceeds to the condition check.

</>
Copy
let value: number = 0;

do {
    value++;

    if (value === 3) {
        continue;
    }

    console.log(value);
} while (value < 5);

Output

1
2
4
5

The counter is updated before continue can run. This placement ensures that the loop advances when the value is 3.

TypeScript do-while Loop vs while Loop and for Loop

Loop typeWhen the condition is checkedMinimum executionsTypical use
do...whileAfter the loop bodyOneWork that must occur before the continuation test
whileBefore the loop bodyZeroRepetition controlled by a condition when the iteration count is unknown
forBefore the loop bodyZeroCounter-based iteration with initialization, condition, and update together

Choose do...while only when executing the body once before the first test matches the program’s requirements. If the body should be skipped when the initial condition is false, use while or for.

Common TypeScript do-while Loop Errors

  • Forgetting that the body runs once: Code with side effects executes even when the initial condition would be false.
  • Not updating the control value: A condition that remains true can create an infinite loop.
  • Using the wrong boundary: Confusing < with <= can produce an extra iteration or omit the final value.
  • Updating after continue: If continue skips a required counter update, the loop may repeatedly test the same value.
  • Adding a stray semicolon after do: The loop body must immediately follow the do keyword as a statement or block.

TypeScript do-while Loop FAQs

Does a TypeScript do-while loop always run once?

Yes. The loop executes its body before evaluating the condition, so the first iteration occurs regardless of whether the condition is true or false.

What is the difference between while and do-while in TypeScript?

A while loop checks its condition before the body and may execute zero times. A do...while loop checks after the body and therefore executes at least once.

Is a semicolon required after a TypeScript do-while loop?

The standard form ends with a semicolon after the condition: } while (condition);. Including it makes the end of the statement explicit and avoids relying on automatic semicolon insertion.

How can I prevent an infinite TypeScript do-while loop?

Ensure that the body changes the state used by the condition, or provide a reachable break or return path. Also check that continue cannot skip an update needed to make the condition false.

TypeScript do-while Loop Editorial QA Checklist

  • Confirm that executing the loop body once before checking the condition is intentional.
  • Verify that every true loop condition can eventually become false or reach an explicit exit.
  • Check < and <= boundaries for an extra or missing iteration.
  • Ensure that continue cannot bypass a required counter or state update.
  • Run every TypeScript example and compare the console results with its displayed output.

TypeScript do-while Loop Summary

With this TypeScript TutorialDo-While loop, that should be a wrap to looping statements in TypeScript.