🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In this chapter, we will explore the do-while loop in TypeScript. The do-while loop is a control flow statement that allows you to execute a block of code at least once, and then repeatedly execute the block as long as a specified condition is true. Understanding how to use the do-while loop is essential for managing loops that need to execute at least once before checking a condition in TypeScript programs.
Table of Contents
- Definition
- Do-While Loop Syntax
- Basic Do-While Loop
- Using Do-While Loop with Arrays
- Nested Do-While Loop
- Complete Example with Output
- Conclusion
Definition
The do-while loop repeats a block of code at least once, and then continues to repeat as long as a specified condition is true. This loop is useful when you need the code to run at least once regardless of the condition.
Do-While Loop Syntax
Syntax
do {
// code to be executed
} while (condition);
Example
This example demonstrates a basic do-while loop that prints numbers from 0 to 4.
let i: number = 0;
do {
console.log(i);
i++;
} while (i < 5);
Output
0
1
2
3
4
Basic Do-While Loop
The basic do-while loop ensures that the code block is executed at least once before the condition is tested.
Example
This example prints the first five even numbers using a do-while loop.
let num: number = 0;
do {
console.log(num);
num += 2;
} while (num < 10);
Output
0
2
4
6
8
Using Do-While Loop with Arrays
The do-while loop can be used to iterate over the elements of an array.
Example
This example iterates over an array of colors and prints each one using a do-while loop.
let colors: string[] = ["red", "green", "blue"];
let index: number = 0;
do {
console.log(colors[index]);
index++;
} while (index < colors.length);
Output
red
green
blue
Nested Do-While Loop
A nested do-while loop is a do-while loop inside another do-while loop. It allows for more complex iterations.
Example
This example prints a multiplication table for numbers 1 through 3 using nested do-while loops.
let i: number = 1;
do {
let j: number = 1;
do {
console.log(`${i} * ${j} = ${i * j}`);
j++;
} while (j <= 3);
i++;
} while (i <= 3);
Output
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
Complete Example with Output
In this section, we will combine all the examples into a single TypeScript file, compile it to JavaScript, and run it to see the output.
TypeScript Code
You can test the following code in the TypeScript Playground:
// Basic Do-While Loop
let i: number = 0;
do {
console.log(i); // Output: 0, 1, 2, 3, 4
i++;
} while (i < 5);
// Using Do-While Loop with Arrays
let colors: string[] = ["red", "green", "blue"];
let index: number = 0;
do {
console.log(colors[index]); // Output: red, green, blue
index++;
} while (index < colors.length);
// Nested Do-While Loop
i = 1;
do {
let j: number = 1;
do {
console.log(`${i} * ${j} = ${i * j}`); // Output: 1 * 1 = 1, 1 * 2 = 2, ... , 3 * 3 = 9
j++;
} while (j <= 3);
i++;
} while (i <= 3);
Conclusion
In this chapter, we covered the do-while loop in TypeScript, including the basic do-while loop, using do-while loop with arrays, and nested do-while loops. We provided a complete example with its output to illustrate how these loops work in TypeScript. Understanding the do-while loop is essential for managing loops that need to execute at least once before checking a condition in TypeScript programs.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment