How to Use the Do-While Loop in TypeScript?

As a TypeScript developer, you should know how to work with loops in TypeScript. In this tutorial, I will explain how to use the do-while loop in TypeScript.

What is a Do-While Loop in TypeScript?

A do-while loop in TypeScript is a control flow statement that executes a block of code at least once, and then repeatedly executes the block as long as a specified condition is true. Unlike the while loop, the do-while loop checks its condition after the loop has been executed, ensuring that the code block runs at least once.

Syntax of TypeScript Do-While Loop

The basic syntax of a do-while loop in TypeScript is as follows:

do {
    // code block to be executed
} while (condition);

Check out For Loop in TypeScript

Key Features of Do-While Loop

  • Guaranteed Execution: The code block inside the do-while loop will always execute at least once.
  • Post-Condition Check: The condition is evaluated after the execution of the code block.

Example 1: Basic Do-While Loop

Let’s start with a simple example. Suppose we want to print numbers from 1 to 5; then you can write the below TypeScript code.

let number: number = 1;

do {
    console.log(number);
    number++;
} while (number <= 5);

In this example, the loop will print numbers 1 through 5. The condition number <= 5 is checked after the code block executes, ensuring that the number is printed at least once.

Here is the exact output in the screenshot below:

Do-While Loop in TypeScript

Read for…in Loops in TypeScript

Example 2: Do-While Loop in TypeScript

Now, let’s consider a more practical example. Suppose we have a list of city names in the USA, and we want to print each city name until a certain condition is met:

let cities: string[] = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"];
let index: number = 0;

do {
    console.log(cities[index]);
    index++;
} while (index < cities.length);

In this example, the loop will print each city name in the list until all cities have been printed.

Example 3: User Input Validation

Consider an example where we need to validate user input. Let’s say we want to prompt the user to enter a valid age (between 1 and 120):

let age: number;

do {
    age = parseInt(prompt("Please enter your age (between 1 and 120):") || "0", 10);
} while (age < 1 || age > 120);

console.log("Thank you! Your age is " + age);

In this example, the prompt will continue to appear until the user enters a valid age between 1 and 120.

Conclusion

The do-while loop in TypeScript is used for scenarios where you need to ensure that a block of code runs at least once. In this tutorial, I explained how to work with the do-while loop in TypeScript with examples. Also, I have covered three examples.

You may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.